fix(apicompat): default json_schema strict=false when converting CC to Responses

Chat Completions treats a missing response_format.json_schema.strict as false,
while Responses validates text.format strictly and rejects schemas without
additionalProperties: false. Forwarding the format unchanged therefore turned
valid requests into upstream 400 invalid_json_schema.

Make the intended mode explicit by adding strict=false only when the field is
absent; explicit strict values are untouched.

Fixes #8760
This commit is contained in:
CooCoode committed 2026-11-13 20:51:06 +08:00
1 parent 530c5e3a4f
commit d085bb1a37
2 files changed
+37

No files matched your search

@@ -445,6 +445,38 @@ func TestChatCompletionsToResponses_ResponseFormatJsonSchema(t *testing.T) {
}`, string(resp.Text.Format))
}
func TestChatCompletionsToResponses_ResponseFormatJsonSchemaDefaultsStrictFalse(t *testing.T) {
req := &ChatCompletionsRequest{
Model: "gpt-4o",
Messages: []ChatMessage{{Role: "user", Content: json.RawMessage(`"Return structured JSON"`)}},
ResponseFormat: json.RawMessage(`{
"type":"json_schema",
"json_schema":{
"name":"answer",
"schema":{
"type":"object",
"properties":{"ok":{"type":"boolean"}},
"required":["ok"]
}
}
}`),
}
resp, err := ChatCompletionsToResponses(req)
require.NoError(t, err)
require.NotNil(t, resp.Text)
assert.JSONEq(t, `{
"type":"json_schema",
"name":"answer",
"schema":{
"type":"object",
"properties":{"ok":{"type":"boolean"}},
"required":["ok"]
},
"strict":false
}`, string(resp.Text.Format))
}
func TestChatCompletionsToResponses_ImageURL(t *testing.T) {
content := `[{"type":"text","text":"Describe this"},{"type":"image_url","image_url":{"url":"data:image/png;base64,abc123"}}]`
req := &ChatCompletionsRequest{
@@ -23,6 +23,11 @@ func chatResponseFormatToResponsesTextFormat(raw json.RawMessage) json.RawMessag
return raw
}
schema["type"] = rawJSONString("json_schema")
// Chat Completions defaults strict to false while Responses defaults to strict,
// so make the intended mode explicit to avoid the upstream rejecting the schema.
if _, ok := schema["strict"]; !ok {
schema["strict"] = json.RawMessage("false")
}
out, err := json.Marshal(schema)
if err != nil {