Skip to content

fix(proxy): keep the key order of forwarded OpenAI requests - #20

Open
aamsellem wants to merge 1 commit into
beclab:mainfrom
aamsellem:fix/proxy-keep-request-key-order
Open

aamsellem wants to merge 1 commit into
beclab:mainfrom
aamsellem:fix/proxy-keep-request-key-order

Conversation

@aamsellem

Copy link
Copy Markdown

What changes

To set model, rewriteJSONModel decoded the request body into map[string]any and re-encoded it. Go encodes map keys in sorted order, so every object in the request reached the engine with its keys in alphabetical order, including the properties of each tool schema.

This PR decodes only the top level, into map[string]json.RawMessage:

  • model is replaced as before;
  • every other value is forwarded as the client sent it.

Why it matters

Chat templates render tool parameters in the order their keys arrive, so the engine saw a different prompt from the one the client sent. Small models are sensitive to that.

LiquidAI LFM3 1.7B (Q8_0, llama.cpp b12352, thinking off) was asked for an edit whose arguments quote <parameter=…> markup, with the same request 8 times per row:

Path to llama-server Prompt tokens Result
Direct, keys as sent 115 8 correct
Direct, keys sorted (json.dumps(sort_keys=True)) 114 8 with a stray } appended to new_string
Through Model Console (llm-init v1.7.26) 114 8 with a stray } appended to new_string

Compatibility

  • The top-level keys are still re-encoded in sorted order; engines read them by name.
  • The JSON content is unchanged, apart from the model value.
  • Nested values are compacted, as encoding/json does for RawMessage.
  • Bodies that are not JSON objects are still forwarded untouched.

Verification

  • TestRewriteJSONModel_KeepsNestedKeyOrder is new. It fails on main ("tool properties reordered") and passes with this change.
  • gofmt -l internal/ is clean.
  • go vet ./... passes.
  • go test -race ./internal/... passes, 27 packages, on Go 1.26.11.
  • golangci-lint was not run.

🤖 Generated with Claude Code

https://claude.ai/code/session_013aCoWcBiA8g3FWfhBQBqKT

rewriteJSONModel decoded the whole request into map[string]any to set
"model" and re-encoded it, which sorted the keys of every object. Tool
schemas reached the engine with their properties in alphabetical order,
so the chat template rendered a different prompt than the client sent.

Decode only the top level into map[string]json.RawMessage: nested values
are forwarded as sent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013aCoWcBiA8g3FWfhBQBqKT
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