Skip to content

feat(upstream): match the 2.x client's chat body byte for byte - #142

Merged
jasonxu114514 merged 1 commit into
jasonxu114514:mainfrom
AiFLYF:pr-b-body
Oct 23, 2026
Merged

jasonxu114514 merged 1 commit into
jasonxu114514:mainfrom
AiFLYF:pr-b-body

Conversation

@AiFLYF

@AiFLYF AiFLYF commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

What

The v2 gate fingerprints the body as well as the headers. Five details still
differed from a captured 2.1.1 request:

# detail before after
1 max_completion_tokens absent 32000
2 tool strict absent false
3 shell description "on Linux using bash" matches the host platform
4 key order alphabetical (max_completion_tokens first) model, messages, tools, stream, stream_options, max_completion_tokens
5 HTML escaping \u003c, \u003e, \u0026 the literal characters

1. max_completion_tokens: 32000

The client sends it on every chat body. Added only when the caller asked for no
ceiling — an explicit max_tokens or max_completion_tokens still wins.

2. "strict": false

Every 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.stringify emits model first; Go writes map keys alphabetically, which
puts max_completion_tokens at the front. The ordered encoder writes the
client's skeleton first and sorts anything left after it.

5. HTML escaping

encoding/json escapes <, > and & by default; JSON.stringify never
does. 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 is
the 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 unconditional
because no real client escapes.

How it was verified

  • Byte comparison against the 2.1.1 capture: key order, 32000, strict,
    the platform wording and the absence of \u003c all match.
  • Live against the real upstream: 20 scenarios (multi-turn, system message,
    special characters, long output, streaming, tool round-trip, sampling
    params, malformed input, concurrent requests, key lane) — all pass.
  • go build ./..., go vet ./..., gofmt clean.

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.
@jasonxu114514
jasonxu114514 merged commit 5c5100c into jasonxu114514:main Oct 23, 2026
5 checks passed
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.

2 participants