Skip to content

feat: FallbackChatGenerator - add fallback for filtered or incomplete responses - #14044

Open
anakin87 wants to merge 4 commits into
mainfrom
fallback-finish-reason
Open

anakin87 wants to merge 4 commits into
mainfrom
fallback-finish-reason

Conversation

@anakin87

@anakin87 anakin87 commented Dec 2, 2026

Copy link
Copy Markdown
Member

Related Issues

Proposed Changes:

  • FallbackChatGenerator can now switch to the next Chat Generator also if the current response is content filtered or incomplete (these are not errors but finish reasons)

How did you test it?

CI, new tests

Notes for the reviewer

Haven't added this new feature to docs because I realized that the page needs a complete refresh. Tracked in https://github.com/deepset-ai/haystack-private/issues/674

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.

@vercel

vercel Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

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

Project Deployment Actions Updated
haystack-docs Ready Ready Preview Dec 2, 2026 12:27pm UTC

Request Review

@github-actions github-actions Bot added topic:tests type:documentation Improvements on the docs labels Dec 2, 2026
@anakin87
anakin87 marked this pull request as ready for review December 2, 2026 12:26
@anakin87
anakin87 requested a review from a team as a code owner December 2, 2026 12:26
@anakin87
anakin87 requested review from julian-risch and removed request for a team December 2, 2026 12:26
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/generators/chat
  fallback.py
  haystack/core/pipeline
  pipeline.py
Project Total  

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

@sjrl
sjrl requested review from sjrl and removed request for julian-risch December 2, 2026 13:21
try:
result = self._run_single_sync(gen, messages, generation_kwargs, tools, streaming_callback)
replies = result.get("replies", [])
self._check_finish_reasons(replies)

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.

I believe when the last generator in the list returns a filtered or length reply, the reply is discarded and run raises. With a single generator and fallback_on_incomplete_response=True:

RuntimeError: All 1 chat generators failed. Last error: Reply has finish_reason='length'. Failed chat generators: [Gen]

I'm not sure this is intended. A truncated reply is probably more useful than an exception when there is nothing left to fall back to. Do you think we should return the last generators reply in that case?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

let me think about this

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

While I see your point, I'm not entirely convinced we should return the last reply

  • This would be different from the general behavior of this component: if all generators fail, we raise. It seems reasonable to me that the new fallback conditions stick to this rule.
  • The user can still configure an appropriate fallback, e.g. a generator with a higher max_token setting
  • For content filtering, a reply with no text is often returned

Happy to discuss further...

try:
result = self._run_single_sync(gen, messages, generation_kwargs, tools, streaming_callback)
replies = result.get("replies", [])
self._check_finish_reasons(replies)

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.

When streaming, the first generator's whole reply has already gone to the callback before we fall back, so the user sees both replies back to back (['truncated answ', 'full answer']). Could be worth mentioning in the param docstrings.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done in 39926ec

@@ -49,22 +49,36 @@ class FallbackChatGenerator:
- Any other exception

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.

Lets update the class docstring. It still says "Falls back on any exception raised by a generator" and the list here only covers exceptions.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done in 39926ec

This branch was successfully deployed

1 active (outdated) deployment
Preview — 7aed30af Deployed Dec 2, 2026 by vercel[bot]
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