Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ and the project adheres to [Semantic Versioning 2.0](https://semver.org/spec/v2.

### Fixed

- The OpenAI proxy keeps the key order of the request it forwards. Setting
`model` decoded the whole body into a Go map and re-encoded it, which
sorted every object's keys, so tool schemas reached the engine with their
properties in alphabetical order and the chat template rendered a
different prompt than the client sent. Only the top level is decoded now;
nested values are forwarded as sent.

- The model card's `extensions.capacity` now follows an engine whose pod an
upgrade replaced. The capacity reporter re-read the engine only on a
readiness dip or a relaunch this process asked for, and a replaced pod
Expand Down
35 changes: 35 additions & 0 deletions internal/adapter/proxy/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,41 @@ func TestRewriteJSONModel(t *testing.T) {
}
}

// The engine renders tool schemas in the order their keys arrive, so the
// rewrite must leave every value below the top level byte-for-byte in place.
func TestRewriteJSONModel_KeepsNestedKeyOrder(t *testing.T) {
t.Parallel()
tools := `[{"type":"function","function":{"name":"edit","parameters":{"type":"object","properties":{"path":{"type":"string"},"old_string":{"type":"string"},"new_string":{"type":"string"}},"required":["path","old_string","new_string"]}}}]`
messages := `[{"role":"user","content":"replace <parameter=limit>40</parameter> & keep \u00e9"}]`
body := []byte(`{"model":"alias","tools":` + tools + `,"messages":` + messages + `,"temperature":0}`)
out, ok := rewriteJSONModel(body, "Owner/Repo:Q8_0")
if !ok {
t.Fatal("expected ok=true")
}
var got map[string]json.RawMessage
if err := json.Unmarshal(out, &got); err != nil {
t.Fatalf("rewritten body is not JSON: %v\n%s", err, out)
}
if string(got["model"]) != `"Owner/Repo:Q8_0"` {
t.Errorf("model = %s", got["model"])
}
props := string(got["tools"])
i, j, k := strings.Index(props, `"path"`), strings.Index(props, `"old_string"`), strings.Index(props, `"new_string"`)
if i < 0 || !(i < j && j < k) {
t.Errorf("tool properties reordered: %s", props)
}
var before, after []map[string]string
if err := json.Unmarshal([]byte(messages), &before); err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(got["messages"], &after); err != nil {
t.Fatal(err)
}
if before[0]["content"] != after[0]["content"] {
t.Errorf("message content changed: %q -> %q", before[0]["content"], after[0]["content"])
}
}

func TestStripAuthorization(t *testing.T) {
t.Parallel()
h := http.Header{}
Expand Down
16 changes: 14 additions & 2 deletions internal/adapter/proxy/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,16 +304,28 @@ func writeRewriteTooLarge(w http.ResponseWriter, req *http.Request, observed int

// rewriteJSONModel returns body with body["model"] set to modelName, or
// (body, false) if body is not a JSON object.
//
// Only the top level is decoded. Every other value is carried as
// json.RawMessage, so the order of keys inside it reaches the engine as the
// client sent it. Decoding into map[string]any re-encoded the whole body with
// Go's sorted map keys, which reordered the properties of every tool schema;
// the chat template renders them in that order, and small models answer
// differently when it changes (LFM3 1.7B appended a stray "}" to a string
// argument in 8 of 8 runs with sorted properties, 0 of 8 as sent).
func rewriteJSONModel(body []byte, modelName string) ([]byte, bool) {
trimmed := bytes.TrimLeft(body, " \t\r\n")
if len(trimmed) == 0 || trimmed[0] != '{' {
return body, false
}
var obj map[string]any
var obj map[string]json.RawMessage
if err := json.Unmarshal(body, &obj); err != nil {
return body, false
}
obj["model"] = modelName
name, err := json.Marshal(modelName)
if err != nil {
return body, false
}
obj["model"] = name
out, err := json.Marshal(obj)
if err != nil {
return body, false
Expand Down