Skip to content

fix(upstream): suppress Accept-Encoding to fix OpenRouter SSE streaming batching - #115

Merged
lidge-jun merged 1 commit into
lidge-jun:mainfrom
Wibias:codex/fix-openrouter-sse-buffering
Jul 13, 2026
Merged

lidge-jun merged 1 commit into
lidge-jun:mainfrom
Wibias:codex/fix-openrouter-sse-buffering

Conversation

@Wibias

@Wibias Wibias commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

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:

  • SSE (text/event-stream) is already plain text; per-token compression gives negligible size savings and actively hurts streaming latency.
  • Non-streaming JSON responses are small enough that the bandwidth difference is immaterial.
  • Any caller-supplied Accept-Encoding header still takes precedence via the spread order ({ 'Accept-Encoding': 'identity', ...init.headers }), so providers that genuinely require compression can still opt in via the adapter's header map.

Change

Single line change in src/server/responses.ts - fetchWithHeaderTimeout now prepends Accept-Encoding: identity before spreading init.headers.

Testing

  • All 41 openai-chat focused tests pass (EOF, hardening, model suffix, parallel tool call assembly).
  • All 14 upstream-retry tests pass.
  • Verified no other test files import or stub fetchWithHeaderTimeout in a way that would be sensitive to the added header.

…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
Wibias marked this pull request as ready for review July 12, 2026 22:04
@lidge-jun
lidge-jun merged commit 150873e into lidge-jun:main Jul 13, 2026
6 checks passed
@Wibias
Wibias deleted the codex/fix-openrouter-sse-buffering branch July 13, 2026 19:18
agentHits pushed a commit to agentHits/opencodex that referenced this pull request Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenRouter (openai-chat adapter): streaming delivers tokens in batches, not live

2 participants