Skip to content

fix: treat [0] document references as out of range in AnswerBuilder#12061

Merged
sjrl merged 2 commits into
deepset-ai:mainfrom
vidigoat:fix/answer-builder-zero-reference
Jul 20, 2026
Merged

fix: treat [0] document references as out of range in AnswerBuilder#12061
sjrl merged 2 commits into
deepset-ai:mainfrom
vidigoat:fix/answer-builder-zero-reference

Conversation

@vidigoat

Copy link
Copy Markdown
Contributor

Related Issues

  • None filed — bug found while reviewing the reference-extraction path; happy to open one if preferred.

Proposed Changes:

AnswerBuilder documents references as 1-based, so extracted indices are decremented by 1. When an LLM cites [0] (LLMs frequently 0-index), the extracted index becomes -1. The out-of-range guard relies on documents[idx] raising IndexError, but Python resolves documents[-1] to the last document — so the guard never fires and an unrelated document is silently attached to the answer, marked referenced=True with a bogus source_index=0:

docs = [Document(content="doc A"), Document(content="doc B"), Document(content="doc C")]
builder = AnswerBuilder(reference_pattern="\[(\d+)\]")
out = builder.run(query="q", replies=["The answer is X [0]"], documents=docs)
# before: doc C attached, referenced=True, source_index=0
# after:  no doc attached + warning "Document index '0' ... is out of range."

Expanded reference ranges starting at 0 ([0-1], expand_reference_ranges=True) hit the same hole via range(start - 1, end).

Fix: replace the try/except IndexError with an explicit bounds check (0 <= idx < len(documents)), keeping the exact same warning message and skip behaviour for all out-of-range references.

How did you test it?

  • Repro script run on main (wrong doc attached) and with the fix (warning + no doc) for both [0] and [0-1].
  • Two new unit tests ([0] and [0-1]-range cases), which fail on main and pass with the fix.
  • Full test/components/builders/test_answer_builder.py: 32 passed.

Notes for the reviewer

  • No behaviour change for valid or too-large references — only negative indices, which were previously impossible to reject.
  • Aware of open PR fix: AnswerBuilder returned referenced documents in scrambled order #11953 touching the same loop for referenced-doc ordering; this change is 3 lines and easy to rebase either way.
  • Disclaimer per contributing guidelines: this fix was AI-assisted; I reviewed it and ran the repro and tests locally.

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes.
  • I have added unit tests and updated the docstrings.
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I have documented my code.
  • I have added a release note file, following the contributors guidelines.
  • I have run pre-commit hooks and fixed any issue.

References are 1-based, so a [0] citation yields index -1, which Python
resolves to the last document instead of triggering the IndexError
guard. The unrelated last document was returned marked referenced=True
with source_index=0. Use an explicit bounds check so [0] (and expanded
ranges starting at 0) warn and attach nothing, like any other
out-of-range reference.
@vidigoat
vidigoat requested a review from a team as a code owner July 18, 2026 14:51
@vidigoat
vidigoat requested review from sjrl and removed request for a team July 18, 2026 14:51
@vercel

vercel Bot commented Jul 18, 2026

Copy link
Copy Markdown

@vidigoat is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/builders
  answer_builder.py
Project Total  

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

@sjrl sjrl 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.

Thanks!

@sjrl
sjrl merged commit 07b5ef4 into deepset-ai:main Jul 20, 2026
22 of 23 checks passed
@sjrl sjrl self-assigned this Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants