fix(streaming): accumulate response.reasoning_text.delta in ResponseStreamState#3397
Open
yen0304 wants to merge 4 commits into
Open
fix(streaming): accumulate response.reasoning_text.delta in ResponseStreamState#3397yen0304 wants to merge 4 commits into
yen0304 wants to merge 4 commits into
Conversation
send_raw() was missing the try/except that send() has — if the WebSocket disconnects mid-send, the data is silently lost instead of being enqueued for retry after reconnection.
send_raw() accepts bytes | str, but the reconnect/retry path decoded bytes to UTF-8 before enqueueing. That turned binary WebSocket frames into text frames and raised UnicodeDecodeError for arbitrary binary payloads (e.g. audio chunks containing 0xff) before the original connection failure could surface. SendQueue now stores bytes | str natively and counts byte length per type, so send_raw() can enqueue the original payload unchanged on both the reconnecting and send-failure paths (sync + async, realtime + responses). Addresses Codex review feedback on openai#3363.
Add an end-to-end test that drives the real _reconnect() + flush path: a send_raw() with non-UTF-8 bytes fails on a dropped socket, gets queued, and is replayed byte-for-byte to the new connection after reconnect. Verified this fails (UnicodeDecodeError) against the prior decode-to-UTF-8 implementation.
…treamState The Python SDK was silently ignoring `response.content_part.added` events for reasoning output items and `response.reasoning_text.delta` events, so reasoning text was never accumulated into the snapshot. The TypeScript SDK handles both correctly; this brings the Python SDK to parity.
d10e407 to
4d4b0e0
Compare
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.
Summary
When streaming a response that includes a reasoning output item (type
"reasoning"), the Python SDK was silently discarding two events:response.content_part.added— when areasoning_textpart is added to a reasoning output item, the SDK only handledoutput.type == "message"and fell through without appending anything foroutput.type == "reasoning".response.reasoning_text.delta— this event type was entirely unhandled inaccumulate_event, so text deltas were never accumulated.The result: after streaming a response with reasoning,
snapshot.output[i].contentremainsNoneand no text is ever built up — even though the wire events arrived correctly.The TypeScript SDK handles both cases correctly (see
_events.ts):response.content_part.addedappends aReasoningItemContentwhenoutput.type === "reasoning"response.reasoning_text.deltaaccumulatescontent.text += event.deltaThis PR mirrors that logic in the Python SDK.
Changes
src/openai/lib/streaming/responses/_responses.py: extendaccumulate_eventto handle both casestests/lib/responses/test_responses.py: addtest_stream_state_accumulates_reasoning_text_deltawhich exercises the full sequence (response.created→output_item.added→content_part.added→ threereasoning_text.deltaevents) and asserts the accumulated textTest