fix(OpenAI): optimize streamed response processing - #795
Merged
Conversation
Support streaming compaction output
iBotPeaches
approved these changes
Aug 11, 2026
Collaborator
|
thanks! |
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.
What:
Description:
StreamResponsepreviously read SSE bodies one byte at a time. Large streamed payloads such asencrypted_contentcould therefore trigger millions ofread(1)calls, whiletrim()and generator-local references kept extra copies of the raw JSON in memory.This change reads the stream in 64 KiB chunks and preserves unread data in an internal line buffer. It avoids trimming large JSON values, releases the raw payload before yielding the DTO, and retries temporary empty reads until EOF. The response schema is unchanged and
encrypted_contentis still returned normally.A 130 MiB
response.output_item.donecompaction event containingencrypted_contentshows the impact: the previous byte-at-a-time implementation would require roughly 136 million reads, while the updated reader performs 2,081 reads. The optimized path processed the event in 0.2058 seconds with an additional peak memory usage of 272,662,528 bytes (about 260 MiB).A direct before-and-after benchmark with an 8 MiB payload measured:
Regression tests cover large buffered events and an empty read before EOF.