Skip to content

chore: support sync streaming callbacks in async contexts for Haystack 2.x/3.x compatibility#3534

Open
julian-risch wants to merge 1 commit into
mainfrom
test/streaming-callback-union-type
Open

chore: support sync streaming callbacks in async contexts for Haystack 2.x/3.x compatibility#3534
julian-risch wants to merge 1 commit into
mainfrom
test/streaming-callback-union-type

Conversation

@julian-risch

@julian-risch julian-risch commented Jul 2, 2026

Copy link
Copy Markdown
Member

Related Issues

  • Part of deepset-ai/haystack-private#446

Haystack 3.0 changes the semantics of select_streaming_callback(requires_async=True): instead of raising ValueError for a sync callback in an async context, it now accepts it with a warning (the callback runs inline on the event loop). Accordingly, its return type widened from AsyncStreamingCallbackT | None to StreamingCallbackT | None, which fails mypy in every integration that passes the result to an async-only-typed internal handler.

Proposed Changes:

Rather than casting the union away, this PR makes the nine affected chat generators actually support what 3.0 allows:

  • Widen the async streaming handlers' callback parameter from AsyncStreamingCallbackT to StreamingCallbackT in anthropic, cohere, google_genai, huggingface_api, litellm, ollama, transformers (_AsyncHFTokenStreamingHandler), vllm, watsonx.
  • Replace each await callback(chunk) with a call that awaits the result only if it is awaitable (the same pattern as haystack 3.0's internal _invoke_streaming_callback).

Haystack 2.x behavior is unchanged.

How did you test it?

  • Haystack 2.x tests pass.
  • Haystack v3 branch (installed git+https://github.com/deepset-ai/haystack.git@v3): hatch run test:types is clean.

Notes for the reviewer

Checklist

🤖 Generated with Claude Code

…enerators

Haystack 3.0 changes select_streaming_callback(requires_async=True) to
accept sync callbacks (they run inline on the event loop with a warning)
instead of raising, so its return type widened from
AsyncStreamingCallbackT | None to StreamingCallbackT | None. Widen the
async handlers' callback parameters accordingly and await the callback
result only when it is awaitable. On Haystack 2.x behavior is unchanged
because sync callbacks are still rejected before reaching these handlers.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Coverage report (google_genai)

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  integrations/google_genai/src/haystack_integrations/components/generators/google_genai/chat
  chat_generator.py
Project Total  

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

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Coverage report (cohere)

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  integrations/cohere/src/haystack_integrations/components/generators/cohere/chat
  chat_generator.py 376-383
Project Total  

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

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Coverage report (huggingface_api)

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  integrations/huggingface_api/src/haystack_integrations/components/generators/huggingface_api/chat
  chat_generator.py
Project Total  

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

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Coverage report (watsonx)

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  integrations/watsonx/src/haystack_integrations/components/generators/watsonx/chat
  chat_generator.py
Project Total  

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

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Coverage report (anthropic)

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  integrations/anthropic/src/haystack_integrations/components/generators/anthropic/chat
  chat_generator.py 443-451
Project Total  

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

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Coverage report (litellm)

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  integrations/litellm/src/haystack_integrations/components/generators/litellm/chat
  chat_generator.py
Project Total  

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

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Coverage report (ollama)

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  integrations/ollama/src/haystack_integrations/components/generators/ollama/chat
  chat_generator.py 501-503
Project Total  

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

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Coverage report (transformers)

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  integrations/transformers/src/haystack_integrations/common/transformers
  utils.py
Project Total  

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

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Coverage report (vllm)

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  integrations/vllm/src/haystack_integrations/components/generators/vllm/chat
  chat_generator.py 430-432
Project Total  

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

@julian-risch julian-risch changed the title fix: support sync streaming callbacks in async contexts for Haystack 2.x/3.x compatibility chore: support sync streaming callbacks in async contexts for Haystack 2.x/3.x compatibility Jul 2, 2026
@julian-risch julian-risch marked this pull request as ready for review July 2, 2026 15:18
@julian-risch julian-risch requested a review from a team as a code owner July 2, 2026 15:18
@julian-risch julian-risch requested review from bogdankostic and removed request for a team July 2, 2026 15:18

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

Looks good!

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