fix(bedrock): deliver streaming chunks individually instead of in batches#1759
Open
shivaber wants to merge 1 commit intostrands-agents:mainfrom
Open
fix(bedrock): deliver streaming chunks individually instead of in batches#1759shivaber wants to merge 1 commit intostrands-agents:mainfrom
shivaber wants to merge 1 commit intostrands-agents:mainfrom
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
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.
Description
Fix BedrockModel.stream() delivering chunks in batches instead of individually.
Replace
call_soon_threadsafe(queue.put_nowait)withrun_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.Root cause: When Bedrock sends chunks in a burst (e.g., multiple chunks in one TCP segment),
call_soon_threadsafescheduled allput_nowaitcallbacks at once. The event loop processed them all before thequeue.get()waiter ran, causing queue depths of 5-6 items and delivering all chunks simultaneously.After fix: Queue depth is always 0 after each
get(), ensuring chunks are yielded one at a time for proper real-time streaming.Related Issues
Closes #1523
Type of Change
Bug fix
Testing
Added
test_stream_delivers_chunks_individuallyregression test that verifies queue depth is 0 after everyget()when chunks arrive in a burstAll 110 existing bedrock tests pass
Verified with reproduction script: before fix queue depth reached 5-6, after fix always 0
I ran
hatch run prepareChecklist
⠋- [x] I have added an appropriate example to the documentation to outline the feature, o r no new docs are needed
Thinking...- [x] My changes generate no new warnings
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.