Repository navigation
feat(upstream): match the 2.x client's chat body byte for byte - #142
Merged
Merged
Conversation
The v2 gate fingerprints the body as well as the headers, and five details still differed from a captured 2.1.1 request: - The client sends max_completion_tokens: 32000 on every chat body. The gateway now adds it when the caller asked for no ceiling; an explicit max_tokens or max_completion_tokens still wins. - Every client tool carries "strict": false, so a tool that omits the field is byte-different from the client's on every request that carries tools. - The shell tool description names the shell the client actually has, so a Windows host advertises a different one than the captured Unix wording. - Chat bodies keep the client's key order, model first. Go writes map keys alphabetically, which puts max_completion_tokens at the front where a real client starts with model. - Bodies are encoded without HTML escaping. encoding/json writes \u003c, \u003e and \u0026 by default where JSON.stringify writes the characters, and tool descriptions are full of both. The ordered encoder falls back to plain encoding/json for every lane except the v2 chat one, and both re-encode points in the request path use it.
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
The v2 gate fingerprints the body as well as the headers. Five details still
differed from a captured 2.1.1 request:
max_completion_tokens32000strictfalsemax_completion_tokensfirst)model, messages, tools, stream, stream_options, max_completion_tokens\u003c,\u003e,\u00261.
max_completion_tokens: 32000The client sends it on every chat body. Added only when the caller asked for no
ceiling — an explicit
max_tokensormax_completion_tokensstill wins.2.
"strict": falseEvery client tool carries it. A tool that omits the field is byte-different
from the client's on every request that carries tools.
3. Shell description
The client describes the shell it actually has, so a Windows host advertises a
different one than the captured Unix wording. The captured definition is left
as-is and rewritten per host at injection time.
4. Key order
JSON.stringifyemitsmodelfirst; Go writes map keys alphabetically, whichputs
max_completion_tokensat the front. The ordered encoder writes theclient's skeleton first and sorts anything left after it.
5. HTML escaping
encoding/jsonescapes<,>and&by default;JSON.stringifyneverdoes. Tool descriptions and code snippets are full of both, so the re-encode
points in the request path now use a
SetEscapeHTML(false)encoder. This isthe one that a byte comparison catches immediately and a JSON comparison never
does.
Scope
The ordered encoder and the escaping change apply to the v2 chat lane. Every
other lane keeps plain
encoding/json; the escaping change is unconditionalbecause no real client escapes.
How it was verified
32000,strict,the platform wording and the absence of
\u003call match.special characters, long output, streaming, tool round-trip, sampling
params, malformed input, concurrent requests, key lane) — all pass.
go build ./...,go vet ./...,gofmtclean.