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
Open
chore: support sync streaming callbacks in async contexts for Haystack 2.x/3.x compatibility#3534julian-risch wants to merge 1 commit into
julian-risch wants to merge 1 commit into
Conversation
…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>
Contributor
Coverage report (google_genai)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
Contributor
Coverage report (cohere)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
Contributor
Coverage report (huggingface_api)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
Contributor
Coverage report (watsonx)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
Contributor
Coverage report (anthropic)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
Contributor
Coverage report (litellm)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
Contributor
Coverage report (ollama)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
Contributor
Coverage report (transformers)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
Contributor
Coverage report (vllm)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related Issues
Haystack 3.0 changes the semantics of
select_streaming_callback(requires_async=True): instead of raisingValueErrorfor 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 fromAsyncStreamingCallbackT | NonetoStreamingCallbackT | 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:
AsyncStreamingCallbackTtoStreamingCallbackTin anthropic, cohere, google_genai, huggingface_api, litellm, ollama, transformers (_AsyncHFTokenStreamingHandler), vllm, watsonx.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?
git+https://github.com/deepset-ai/haystack.git@v3):hatch run test:typesis clean.Notes for the reviewer
Checklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:.🤖 Generated with Claude Code