Files
magpie/internal/provider/codex_strict_test.go
T
yetone 567ba3a277 codex: a function tool goes with a strict the backend can compile (Anotiaww on X)
Anotiaww on X: LobeHub's OpenAI provider, pointed at magpie, asked a
Codex model on Responses and got "Codex: Invalid schema for
'tools[3].parameters': oneOf constraints are not supported. Enable
'coerce_one_of' option to approximate oneOf with anyOf".

Cause: LobeHub sends its tools on Responses with no strict
(convertChatCompletionToolToResponseTool spreads the function, no
strict), and magpie passed them to the ChatGPT backend so. Left out,
the backend compiles a tool's parameters as a strict grammar; its
calculator's defintegrate bounds and limit's point are a oneOf of
string and number. Codex CLI itself says "strict": false on every
function tool (codex-rs/tools ResponsesApiTool). A Chat or Messages
client asking strict true with a schema strict mode refuses (calculate's
optional precision, an allOf, a lookaround) was turned away the same way.

Fix, in codexBody, so every route to a Codex account (Responses relayed,
Chat and Messages built as Responses) passes it: a function tool's
strict left out goes false, as Codex says it; strict true is kept when
the schema fits strict mode, with a oneOf asked as anyOf (the backend's
own suggestion); a schema strict mode still can't take goes as written
with strict false. Namespace tools, additional_tools and
tool_search_output are covered. coerce_one_of is not used: it is nowhere
in Codex's source or any docs found. Other vendors are unchanged.

What strict mode takes was tried on the backend (2026-11-19, the
owner's ChatGPT account, gpt-5.6-luna and gpt-6-sol among others):
refused with strict true: oneOf, allOf, not, if, uniqueItems,
minProperties, contains, propertyNames, dependentRequired, an optional
property, additionalProperties not false, a root anyOf, a lookaround, a
node with only a const ("schema must specify a type"); taken: anyOf
(nested, in items, in $defs), $ref, enum alone, a type list, const with
a type, min/max keywords, format, pattern, $schema, title, examples.
Left out, a lookaround is refused and false takes it.

Run:
- go test ./internal/provider -run TestCodexToolStrict,
  ./internal/gateway -run 'TestCodexToolSchemasTheBackendCompiles|
  TestToolStrictOnlyForCodex': ok on mica.
- go vet ./internal/provider ./internal/gateway for darwin, linux and
  windows: ok on mica. gofmt -l: clean.
- Real backend: LobeHub's 10 calculator tools as this change sends them
  (strict false on Responses; strict true from a Chat client, defintegrate
  kept strict as anyOf; calculate, evaluate, sort, limit and solve,
  which have optional properties, false)
  and a mixed set (oneOf nested in items and $defs, a const-only oneOf,
  an allOf, a lookaround): 200 on gpt-5.6-luna and gpt-6-sol. The old
  strict-true request was refused ("required must include declared
  property precision"). That run found the const-only case, fixed here.

Broken by hand: with the codexToolStrict call removed, the gateway
test is red with the reporter's message word for word (tools[5],
defintegrate). TestToolStrictOnlyForCodex passes on the old code too;
it guards scope (OpenAI's API, a relay and a Chat provider keep strict
left out and the oneOf).

Not tried: the reporter's exact 400 could not be reproduced on this
account, where a oneOf with strict left out was taken on every model
tried; the lookaround case shows the same rule. LobeHub itself was not
run. The full suite runs after the tag (mote load 28 on 18 cores, mica
in another session's suite); its result goes in a follow-up.
2026-11-19 12:10:09 +08:00

85 lines
5.5 KiB
Go

package provider
import (
"encoding/json"
"strings"
"testing"
)
func strictTool(t *testing.T, raw string) map[string]any {
t.Helper()
var m map[string]any
if err := json.Unmarshal([]byte(raw), &m); err != nil {
t.Fatal(err)
}
return m
}
func asJSON(v any) string { b, _ := json.Marshal(v); return string(b) }
// Each rule codexToolStrict holds a tool to, as the ChatGPT backend
// answered it on 2026-11-19 (codex_strict.go).
func TestCodexToolStrict(t *testing.T) {
obj := func(props, req string) string {
return `{"type":"object","additionalProperties":false,"properties":` + props + `,"required":` + req + `}`
}
for _, c := range []struct {
name, tool string
strict bool
params string // "" for as sent
}{
{"left out", `{"type":"function","name":"f","parameters":` + obj(`{"q":{"oneOf":[{"type":"string"},{"type":"number"}]}}`, `["q"]`) + `}`, false, ""},
{"not a bool", `{"type":"function","name":"f","strict":"yes","parameters":` + obj(`{"q":{"type":"string"}}`, `["q"]`) + `}`, false, ""},
{"false kept", `{"type":"function","name":"f","strict":false,"parameters":{"type":"object","properties":{"q":{"allOf":[{"type":"string"}]}}}}`, false, ""},
{"true fits", `{"type":"function","name":"f","strict":true,"parameters":` + obj(`{"q":{"anyOf":[{"type":"string"},{"type":"null"}]},"n":{"type":["string","number"]}}`, `["q","n"]`) + `}`, true, ""},
{"true, oneOf as anyOf", `{"type":"function","name":"f","strict":true,"parameters":` + obj(`{"q":{"oneOf":[{"type":"string"},{"type":"number"}]},"oneOf":{"type":"string"}}`, `["q","oneOf"]`) + `}`,
true, obj(`{"q":{"anyOf":[{"type":"string"},{"type":"number"}]},"oneOf":{"type":"string"}}`, `["q","oneOf"]`)},
{"true, oneOf in items and $defs", `{"type":"function","name":"f","strict":true,"parameters":{"type":"object","additionalProperties":false,"required":["a"],"properties":{"a":{"type":"array","items":{"oneOf":[{"$ref":"#/$defs/x"},{"type":"null"}]}}},"$defs":{"x":{"oneOf":[{"type":"integer","const":1},{"type":"integer","const":2}]}}}}`,
true, `{"type":"object","additionalProperties":false,"required":["a"],"properties":{"a":{"type":"array","items":{"anyOf":[{"$ref":"#/$defs/x"},{"type":"null"}]}}},"$defs":{"x":{"anyOf":[{"type":"integer","const":1},{"type":"integer","const":2}]}}}`},
{"true, oneOf and an optional property", `{"type":"function","name":"f","strict":true,"parameters":` + obj(`{"q":{"oneOf":[{"type":"string"},{"type":"number"}]},"p":{"type":"number"}}`, `["q"]`) + `}`, false, ""},
{"true, allOf", `{"type":"function","name":"f","strict":true,"parameters":` + obj(`{"q":{"allOf":[{"type":"string"}]}}`, `["q"]`) + `}`, false, ""},
{"true, if", `{"type":"function","name":"f","strict":true,"parameters":` + obj(`{"q":{"type":"string","if":{"const":"a"}}}`, `["q"]`) + `}`, false, ""},
{"true, not", `{"type":"function","name":"f","strict":true,"parameters":` + obj(`{"q":{"not":{"type":"null"}}}`, `["q"]`) + `}`, false, ""},
{"true, uniqueItems", `{"type":"function","name":"f","strict":true,"parameters":` + obj(`{"q":{"type":"array","items":{"type":"string"},"uniqueItems":true}}`, `["q"]`) + `}`, false, ""},
{"true, lookaround", `{"type":"function","name":"f","strict":true,"parameters":` + obj(`{"q":{"type":"string","pattern":"^(?!/etc)"}}`, `["q"]`) + `}`, false, ""},
{"true, more allowed", `{"type":"function","name":"f","strict":true,"parameters":{"type":"object","properties":{"q":{"type":"string"}},"required":["q"]}}`, false, ""},
{"true, open object inside", `{"type":"function","name":"f","strict":true,"parameters":` + obj(`{"q":{"type":"object"}}`, `["q"]`) + `}`, false, ""},
{"true, a const alone", `{"type":"function","name":"f","strict":true,"parameters":` + obj(`{"q":{"oneOf":[{"const":"a"},{"const":"b"}]}}`, `["q"]`) + `}`, false, ""},
{"true, an enum alone", `{"type":"function","name":"f","strict":true,"parameters":` + obj(`{"q":{"oneOf":[{"enum":["a"]},{"type":"number"}]}}`, `["q"]`) + `}`,
true, obj(`{"q":{"anyOf":[{"enum":["a"]},{"type":"number"}]}}`, `["q"]`)},
{"true, root anyOf", `{"type":"function","name":"f","strict":true,"parameters":{"anyOf":[` + obj(`{"q":{"type":"string"}}`, `["q"]`) + `]}}`, false, ""},
{"true, no parameters", `{"type":"function","name":"f","strict":true}`, false, ""},
} {
tool := strictTool(t, c.tool)
before := asJSON(tool["parameters"])
codexStrictTool(tool)
if tool["strict"] != c.strict {
t.Errorf("%s: strict %v, want %v", c.name, tool["strict"], c.strict)
}
want := before
if c.params != "" {
want = asJSON(strictTool(t, c.params))
}
if got := asJSON(tool["parameters"]); got != want {
t.Errorf("%s: parameters\n %s\nwant\n %s", c.name, got, want)
}
}
// every place a Codex request holds function tools
m := strictTool(t, `{"tools":[{"type":"function","name":"a"},{"type":"namespace","name":"ns","tools":[{"type":"function","name":"b"}]},{"type":"web_search"}],
"input":[{"type":"additional_tools","tools":[{"type":"function","name":"c"}]},{"type":"tool_search_output","tools":[{"type":"function","name":"d"}]},{"type":"message","role":"user","content":"x"}]}`)
if !codexToolStrict(m) {
t.Fatal("nothing changed")
}
got := asJSON(m)
for _, want := range []string{`{"name":"a","strict":false,"type":"function"}`, `{"name":"b","strict":false,"type":"function"}`,
`{"name":"c","strict":false,"type":"function"}`, `{"name":"d","strict":false,"type":"function"}`, `{"type":"web_search"}`} {
if !strings.Contains(got, want) {
t.Errorf("no %s in %s", want, got)
}
}
if codexToolStrict(m) {
t.Error("a second pass changed something")
}
}