fix(chat): expose stream_batch content for every response - #202
Open
latent-9 wants to merge 1 commit into
Open
Conversation
stream_batch(n) wraps all n options in Response objects sharing one proto, but only responses[0].process_chunk is called for each chunk. Content is buffered per output index in the first response and only materialized into the shared proto when responses[0].content is read or the telemetry span closes. Reading responses[i].content for i > 0 during streaming returns an empty string for the whole stream. Materialize the first response's buffers into the shared proto after each chunk so every response reflects streamed content regardless of read order.
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
stream_batch(n)wraps all n options inResponseobjects that share one underlying proto, but onlyresponses[0].process_chunk(chunk)is called for every incoming chunk. Chunk content is buffered per output index inside the first response, and only materialized into the shared proto whenresponses[0].contentis read, or when the telemetry span closes.A caller that reads
responses[i].contentfor i > 0 during streaming, or beforeresponses[0], gets an empty string for the whole stream. This is the natural way to stream per-option content:With this code
responses[1].contentis""on every iteration, even though the server streams the content. The fix materializes the buffered content into the shared proto after every chunk, so each response reflects the streamed content regardless of read order.Changes
src/xai_sdk/aio/chat.py: sync content buffers to the shared proto after each chunk instream_batchsrc/xai_sdk/sync/chat.py: same fix for the sync clienttests/aio/chat_test.pyandtests/sync/chat_test.pythat readresponses[1].contentduring the stream. They fail on the current code and pass with this change.Type of Change
Related Issue
N/A