fix(bedrock): deliver streaming chunks individually instead of in batches#2950
Open
chaynabors wants to merge 1 commit into
Open
fix(bedrock): deliver streaming chunks individually instead of in batches#2950chaynabors wants to merge 1 commit into
chaynabors wants to merge 1 commit into
Conversation
…ches Replace call_soon_threadsafe(queue.put_nowait) with run_coroutine_threadsafe(queue.put()).result() and use a bounded queue (maxsize=1). This blocks the producer thread until each event is delivered to the event loop, preventing callback batching where all chunks were processed in a single event loop iteration. Previously, when Bedrock sent chunks in a burst (e.g., multiple chunks in one TCP segment), call_soon_threadsafe scheduled all put_nowait callbacks at once. The event loop processed them all before the queue.get() waiter ran, causing queue depths of 5-6 items and delivering all chunks simultaneously to the consumer. With the fix, queue depth is always 0 after each get(), ensuring chunks are yielded one at a time for proper real-time streaming. Closes strands-agents#1523
5 tasks
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.
Supersedes #1759 with a clean rebase on current main after the monorepo restructure.
When Bedrock delivers a burst of chunks in a single TCP segment, call_soon_threadsafe schedules every put_nowait on the same event-loop tick, so the queue accumulates all chunks before any consumer await runs and the stream feels batched. Switching the producer callback to run_coroutine_threadsafe with a maxsize=1 queue forces real backpressure between the boto3 worker thread and the asyncio consumer, so each chunk is delivered before the next is enqueued. The pattern matches what mcp_client.py already does and the regression test asserts queue depth stays at zero after every consumer get.
Refs #1759, fixes #1523. Original commit by @shivaber, carried forward to the strands-py/ layout.