fix: propagate Content-Length header to upstream requests#2375
Open
octo-patch wants to merge 1 commit into
Open
fix: propagate Content-Length header to upstream requests#2375octo-patch wants to merge 1 commit into
octo-patch wants to merge 1 commit into
Conversation
…quanpeng#2366) When the request body is passed through unchanged (e.g., OpenAI-compatible pass-through mode), Go's http.NewRequest cannot determine the content length and sets ContentLength to -1, resulting in chunked transfer encoding. Some upstream APIs such as Azure's grok model require an explicit Content-Length header and reject chunked requests with a 400 error. Fix: after creating the upstream request, if ContentLength is still -1, inherit it from the original client request. This correctly handles the pass-through case. When the body has been re-serialized (e.g., after ConvertRequest), Go already computes the length from bytes.Buffer and this fallback is not applied.
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.
Fixes #2366
Problem
When the request body is passed through unchanged (OpenAI-compatible pass-through mode, e.g., for Azure channels with no format conversion needed), Go's
http.NewRequestreceives a genericio.Readerand cannot determine the content length. It setsContentLengthto-1, which causes the Go HTTP client to send the request withTransfer-Encoding: chunkedinstead of aContent-Lengthheader.Some upstream APIs — specifically Azure's grok model — require an explicit
Content-Lengthheader and reject chunked requests with:Solution
In
DoRequestHelper, after creating the upstream*http.Request, check ifContentLengthis still-1(unknown). If so, inherit it from the original client request (c.Request.ContentLength).This fallback applies only to the pass-through case. When the request body has been re-serialized via
ConvertRequest(into a*bytes.Buffer), Go'shttp.NewRequestalready computes the length automatically andContentLengthwill be non-negative — the fallback is not applied.This mirrors the existing pattern in
relay/controller/audio.gowherereq.ContentLength = c.Request.ContentLengthis set for Azure audio requests.Testing
ContentLengthis already set by Go).