Repository navigation
fix(upstream): suppress Accept-Encoding to fix OpenRouter SSE streaming batching - #115
Merged
lidge-jun merged 1 commit intoJul 13, 2026
Merged
Conversation
…Brotli/gzip Bun's fetch() automatically negotiates content-encoding (br, gzip, deflate, zstd). Providers like OpenRouter honour this and respond with Brotli or gzip compressed bodies. Bun then decompresses on the fly, but Brotli works in frames/blocks — so tokens are held until a full decompression block is ready rather than being forwarded token-by-token. The result is that the openai-chat adapter path appears to deliver responses in one big batch at the end even though the upstream SSE deltas are correct. Fix: inject Accept-Encoding: identity ahead of any caller-supplied headers in fetchWithHeaderTimeout so every upstream request opts out of content encoding. This is safe because: - SSE (text/event-stream) is already a plain-text format; compression gives no meaningful size benefit and actively hurts latency. - Non-streaming JSON responses are small enough that the savings are negligible. - Caller-supplied headers still take precedence (spread order). Closes #114
Wibias
marked this pull request as ready for review
July 12, 2026 22:04
agentHits
pushed a commit
to agentHits/opencodex
that referenced
this pull request
Sep 17, 2026
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.
Problem
Fixes #114.
When using the openai-chat adapter (e.g. OpenRouter models), responses are delivered in batches at the end rather than token-by-token, even though the debug log shows correct per-token SSE deltas being produced by the adapter.
Root cause: Bun's fetch() automatically sends Accept-Encoding: br, gzip, deflate, zstd. Providers like OpenRouter honour this and reply with a Brotli or gzip compressed body. Bun decompresses transparently, but Brotli works in block frames - it cannot yield a decompressed byte until a full frame is complete. This means SSE data: lines accumulate in the decompressor's internal buffer and only flush when a compression boundary is crossed, producing the observed batch behaviour.
The ChatGPT passthrough (openai-responses adapter) is unaffected because it bypasses fetchWithHeaderTimeout entirely and uses Bun's native SSE relay directly.
Fix
Inject Accept-Encoding: identity as the first entry in the headers passed to fetch() inside fetchWithHeaderTimeout. This tells every upstream provider not to compress the response body, so SSE frames are forwarded to the client immediately as they arrive.
Why this is safe:
Change
Single line change in src/server/responses.ts - fetchWithHeaderTimeout now prepends Accept-Encoding: identity before spreading init.headers.
Testing