From d085bb1a377bb42248da3bfa6d12cf205769fb56 Mon Sep 17 00:00:00 2001 From: CooCoode <5113047+CooCoode@users.noreply.github.com> Date: Fri, 13 Nov 2026 20:51:06 +0800 Subject: [PATCH] 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 --- .../chatcompletions_responses_test.go | 32 +++++++++++++++++++ .../internal/pkg/apicompat/response_format.go | 5 +++ 2 files changed, 37 insertions(+) diff --git a/backend/internal/pkg/apicompat/chatcompletions_responses_test.go b/backend/internal/pkg/apicompat/chatcompletions_responses_test.go index b94bc5c394..21a8c05d36 100644 --- a/backend/internal/pkg/apicompat/chatcompletions_responses_test.go +++ b/backend/internal/pkg/apicompat/chatcompletions_responses_test.go @@ -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{ diff --git a/backend/internal/pkg/apicompat/response_format.go b/backend/internal/pkg/apicompat/response_format.go index afb5c3e2fd..ce12311102 100644 --- a/backend/internal/pkg/apicompat/response_format.go +++ b/backend/internal/pkg/apicompat/response_format.go @@ -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 {