Skip to content

fix: strip content-length from upstream headers in non-streaming response (OQP-1)#5

Merged
TadMSTR merged 1 commit into
mainfrom
fix/oqp-1-content-length
Jun 16, 2026
Merged

fix: strip content-length from upstream headers in non-streaming response (OQP-1)#5
TadMSTR merged 1 commit into
mainfrom
fix/oqp-1-content-length

Conversation

@TadMSTR

@TadMSTR TadMSTR commented Jun 16, 2026

Copy link
Copy Markdown
Owner

Summary

  • Strip content-length and transfer-encoding from upstream headers before passing to JSONResponse in the non-streaming path
  • Ollama appends a trailing \n to non-streaming JSON responses; JSONResponse re-serialises without it, producing a body 1 byte shorter than the upstream content-length
  • Starlette only auto-computes content-length when the header is absent — the stale upstream value was winning

Root cause

dispatch_request passed headers={**dict(resp.headers), ...} directly to JSONResponse. Starlette skips length recomputation if content-length is present in the passed headers.

Changes

  • Added _STRIP_RESPONSE_HEADERS = {"content-length", "transfer-encoding"} module-level constant in proxy.py
  • Filter those headers before building the JSONResponse
  • Regression test test_non_streaming_response_content_length_correct in tests/test_proxy.py

Test plan

  • Regression test passes: mocks upstream with trailing \n, asserts Content-Length matches re-serialised body
  • Full suite: 138 passed, 0 failed

🤖 Co-authored with Claude Sonnet 4.6

…onse (OQP-1)

JSONResponse re-serialises the upstream JSON body using compact separators,
producing a body that differs from Ollama's trailing-newline-padded bytes.
Starlette only auto-computes content-length when the header is absent, so
passing upstream headers caused the stale (off-by-one) value to win.

Add _STRIP_RESPONSE_HEADERS constant and filter content-length and
transfer-encoding before building the JSONResponse. Add regression test
that asserts the returned content-length matches the re-serialised body,
not the upstream value.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@TadMSTR TadMSTR merged commit 3112b11 into main Jun 16, 2026
4 checks passed
@TadMSTR TadMSTR deleted the fix/oqp-1-content-length branch June 16, 2026 11:49
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.

1 participant