Skip to content

fix: prevent GeneratedAnswer.from_dict crash on empty all_messages#11626

Merged
davidsbatista merged 5 commits into
deepset-ai:mainfrom
i-anubhav-anand:fix/generated-answer-empty-all-messages
Jun 16, 2026
Merged

fix: prevent GeneratedAnswer.from_dict crash on empty all_messages#11626
davidsbatista merged 5 commits into
deepset-ai:mainfrom
i-anubhav-anand:fix/generated-answer-empty-all-messages

Conversation

@i-anubhav-anand

Copy link
Copy Markdown
Contributor

Related Issues

Self-found bug (no existing issue). GeneratedAnswer.from_dict crashes with IndexError when the serialized meta contains an empty all_messages list — which to_dict itself can emit, so a plain round-trip crashes.

Proposed Changes:

GeneratedAnswer.from_dict guarded the metadata branch with is not None:

if (all_messages := meta.get("all_messages")) is not None and isinstance(all_messages[0], dict):

An empty list [] is not None, so it passes the guard and the immediate all_messages[0] access raises IndexError: list index out of range. to_dict already uses the correct truthiness guard (if all_messages and isinstance(all_messages[0], ChatMessage)), which leaves an empty all_messages untouched in the output — so to_dictfrom_dict round-trips into the crash.

The fix aligns from_dict with to_dict by using the same truthiness guard, so an empty all_messages list round-trips correctly instead of crashing.

Reproduction (before the fix):

from haystack.dataclasses.answer import GeneratedAnswer
a = GeneratedAnswer(data="ans", query="q", documents=[], meta={"all_messages": []})
GeneratedAnswer.from_dict(a.to_dict())  # IndexError: list index out of range

How did you test it?

Added two unit tests in test/dataclasses/test_answer.py:

  • test_from_dict_with_empty_all_messages — deserializing meta={"all_messages": []} no longer raises.
  • test_to_dict_from_dict_round_trip_with_empty_all_messages — full round-trip equality.

Both fail on main (IndexError) and pass with this change. Ran hatch run test:unit test/dataclasses/test_answer.py (20 passed), hatch run fmt (clean), and hatch run test:types haystack/dataclasses/answer.py (mypy clean). Added a release note under releasenotes/notes.

Notes for the reviewer

One-line behavior fix; the existing non-empty / ChatMessage / string all_messages paths are unchanged and still covered by the existing tests.

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.

@i-anubhav-anand i-anubhav-anand requested a review from a team as a code owner June 14, 2026 19:28
@i-anubhav-anand i-anubhav-anand requested review from davidsbatista and removed request for a team June 14, 2026 19:28
@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.

@CLAassistant

CLAassistant commented Jun 14, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

GeneratedAnswer.from_dict guarded meta['all_messages'] with 'is not None',
so an empty list passed the check and the subsequent all_messages[0] access
raised IndexError. to_dict already uses a truthiness guard; align from_dict
with it so an empty all_messages list round-trips instead of crashing.
@i-anubhav-anand i-anubhav-anand force-pushed the fix/generated-answer-empty-all-messages branch from 32a814c to f983755 Compare June 15, 2026 14:27
@github-actions

github-actions Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/dataclasses
  answer.py
Project Total  

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

Comment thread releasenotes/notes/fix-generated-answer-empty-all-messages-ffccf61b751bc344.yaml Outdated
Comment thread releasenotes/notes/fix-generated-answer-empty-all-messages-ffccf61b751bc344.yaml Outdated
Comment thread releasenotes/notes/fix-generated-answer-empty-all-messages-ffccf61b751bc344.yaml Outdated
Comment thread releasenotes/notes/fix-generated-answer-empty-all-messages-ffccf61b751bc344.yaml Outdated
@vercel

vercel Bot commented Jun 16, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
haystack-docs Ignored Ignored Preview Jun 16, 2026 2:47pm

Request Review

@github-actions github-actions Bot added the type:documentation Improvements on the docs label Jun 16, 2026

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

Looks good! Thanks @i-anubhav-anand for the contribution

@davidsbatista davidsbatista enabled auto-merge (squash) June 16, 2026 14:52
@davidsbatista davidsbatista merged commit 5b35a05 into deepset-ai:main Jun 16, 2026
24 checks passed
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.

3 participants