Merge pull request #8761 from CooCoode/fix/8760-cc-responses-strict-default

fix(apicompat): default json_schema strict=false when converting CC to Responses
This commit is contained in:
Wesley Liddick authored and GitHub committed 2026-11-16 16:32:51 +08:00
commit 4763569af2
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 {