Skip to content

fix: raise a clear error for zero-sum weights in DocumentJoiner#11629

Merged
bogdankostic merged 2 commits into
deepset-ai:mainfrom
i-anubhav-anand:fix/document-joiner-zero-sum-weights
Jun 19, 2026
Merged

fix: raise a clear error for zero-sum weights in DocumentJoiner#11629
bogdankostic merged 2 commits into
deepset-ai:mainfrom
i-anubhav-anand:fix/document-joiner-zero-sum-weights

Conversation

@i-anubhav-anand

Copy link
Copy Markdown
Contributor

Related Issues

Self-found bug (no existing issue). DocumentJoiner crashes with an opaque ZeroDivisionError at construction when the provided weights sum to zero.

Proposed Changes:

DocumentJoiner.__init__ normalized the weights with:

self.weights = [float(i) / sum(weights) for i in weights] if weights else None

If weights is non-empty but sums to zero (e.g. [0, 0, 0], or [1, -1]), sum(weights) is 0 and this raises a bare ZeroDivisionError: float division by zero with no indication of what went wrong. This change validates the sum and raises a clear ValueError instead.

Reproduction (before the fix):

from haystack.components.joiners import DocumentJoiner
DocumentJoiner(weights=[0, 0, 0])  # ZeroDivisionError: float division by zero

How did you test it?

Added test_init_with_zero_sum_weights_raises in test/components/joiners/test_document_joiner.py asserting a ValueError (matching "must not sum to zero") is raised. Ran hatch run test:unit test/components/joiners/test_document_joiner.py (33 passed), hatch run fmt (clean), and hatch run test:types haystack/components/joiners/document_joiner.py (mypy clean). Added a release note.

Notes for the reviewer

Tiny, isolated change to the constructor; normal weights are unaffected.

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • I have added unit tests and updated the docstrings.
  • I've used a conventional commit type for my PR title (fix:).
  • I have added a release note file.
  • I have run pre-commit hooks / hatch run fmt and fixed any issue.

This PR was generated with the help of an AI assistant. I have reviewed the changes, reproduced the bug, and run the relevant tests locally.

DocumentJoiner normalized weights via float(i) / sum(weights); when the
weights summed to zero this raised an opaque ZeroDivisionError at construction.
Validate the sum and raise a clear ValueError instead.
@i-anubhav-anand i-anubhav-anand requested a review from a team as a code owner June 14, 2026 20:04
@i-anubhav-anand i-anubhav-anand requested review from bogdankostic and removed request for a team June 14, 2026 20:04
@vercel

vercel Bot commented Jun 14, 2026

Copy link
Copy Markdown

@i-anubhav-anand is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added topic:tests type:documentation Improvements on the docs labels Jun 14, 2026
@github-actions

github-actions Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/joiners
  document_joiner.py
Project Total  

This report was generated by python-coverage-comment-action

@bogdankostic

Copy link
Copy Markdown
Contributor

Thanks for creating this PR @i-anubhav-anand! The release note check is currently failing, please fix it by using double backticks instead of single backticks for in-line code.

@bogdankostic bogdankostic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bogdankostic bogdankostic merged commit 2b21aa6 into deepset-ai:main Jun 19, 2026
21 of 22 checks passed
sjrl pushed a commit that referenced this pull request Jun 22, 2026
Co-authored-by: bogdankostic <bogdankostic@web.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants