diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 4a0c0d223..18cfa37e9 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -4705,7 +4705,7 @@ const tierField = "service_tier" // vendor, Kimi's and Qwen's thinking switches) take that a request does // without: an upstream that refuses one by name is asked again without it. var optionalFields = []string{"store", "metadata", tierField, cacheKeyField, "prompt_cache_retention", - "safety_identifier", "stream_options", "parallel_tool_calls", "verbosity", "thinking", "enable_thinking"} + "prompt_cache_options", "safety_identifier", "stream_options", "parallel_tool_calls", "verbosity", "thinking", "enable_thinking"} // refusedOptional lists optional fields an upstream explicitly rejects as // unsupported. An invalid value must reach the client without dropping the @@ -4738,6 +4738,12 @@ var unknownOptionalField = regexp.MustCompile("(?i)\\b(?:unknown (?:name|field|p var unquotedUnsupportedOptionalField = regexp.MustCompile("(?i)\\bunsupported (?:parameter|field|property|argument):\\s*([a-z_]+)\\b") var rejectedOptionalField = regexp.MustCompile("(?i)(?:^|\\b(?:parameter|field|property|argument)\\s+)['\"\\x60]([a-z_]+)['\"\\x60]\\s+(?:is\\s+)?(?:unsupported|not supported)\\b") +// leadingRejectedOptionalField is a message that starts with the field's +// bare name: OpenAI's "prompt_cache_options is not supported on this +// model" (#2154). Only at the start, so a field named further on, in an +// echo of the input, is not taken for it. +var leadingRejectedOptionalField = regexp.MustCompile("(?i)^\\s*([a-z_]+)\\s+is\\s+(?:unsupported|not supported)\\b") + func unsupportedOptionalField(fault any, field string) bool { switch v := fault.(type) { case string: @@ -4786,6 +4792,9 @@ func unsupportedOptionalField(fault any, field string) bool { return true } } + if match := leadingRejectedOptionalField.FindStringSubmatch(v); match != nil && match[1] == field { + return true + } case []any: for _, item := range v { if unsupportedOptionalField(item, field) { diff --git a/internal/gateway/optional_fields_test.go b/internal/gateway/optional_fields_test.go index fc059b158..c9a1b419f 100644 --- a/internal/gateway/optional_fields_test.go +++ b/internal/gateway/optional_fields_test.go @@ -148,7 +148,7 @@ func TestOptionalFieldMixedValidation(t *testing.T) { } func TestRefusedOptionalNamesOnlyUnsupportedFields(t *testing.T) { - request := []byte(`{"store":false,"metadata":{"session":"synthetic"},"service_tier":"default","prompt_cache_retention":"24h","thinking":{"type":"enabled"}}`) + request := []byte(`{"store":false,"metadata":{"session":"synthetic"},"service_tier":"default","prompt_cache_retention":"24h","prompt_cache_options":{"mode":"explicit"},"thinking":{"type":"enabled"}}`) for _, tt := range []struct { name, reply string status int @@ -175,6 +175,10 @@ func TestRefusedOptionalNamesOnlyUnsupportedFields(t *testing.T) { {"root-location", `{"detail":[{"type":"extra_forbidden","loc":["store"],"msg":"Extra inputs are not permitted"}]}`, 422, []string{"store"}}, {"unsupported-code", `{"error":{"message":"Not accepted here","code":"unsupported_parameter","param":"store"}}`, 400, []string{"store"}}, {"codex-unquoted-unsupported-parameter", `{"code":null,"message":"Codex: Unsupported parameter: prompt_cache_retention","param":null,"type":"invalid_request_error"}`, 400, []string{"prompt_cache_retention"}}, + // the ChatGPT backend's, as it gave it on 2026-10-14 (#2154) + {"openai-bare-name", `{"error":{"message":"prompt_cache_options is not supported on this model","type":"invalid_request_error","param":"prompt_cache_options","code":"invalid_parameter"}}`, 400, []string{"prompt_cache_options"}}, + {"bare-name-not-leading", `{"error":{"message":"The input said metadata is not supported here"}}`, 400, nil}, + {"bare-name-other-field", `{"error":{"message":"reasoning is not supported on this model"}}`, 400, nil}, {"plain-error", `Unknown name "store": Cannot find field.`, 400, []string{"store"}}, {"status-prefix", `[400] Unknown name "store": Cannot find field.`, 400, []string{"store"}}, {"text-status-prefix", `[HTTP 400] Unknown name "store": Cannot find field.`, 400, []string{"store"}}, @@ -239,3 +243,34 @@ func optionalFieldReply(proto provider.Protocol) string { `data: {"id":"c1","choices":[{"delta":{},"finish_reason":"stop"}]}`, `data: [DONE]`) } + +// #2154 (neavo): pi-ai sends prompt_cache_options, which OpenAI's Responses +// API turns away by its bare name: asked again without it, and not sent it +// again, while the cache key stays. +func TestPromptCacheOptionsRefusedRetried(t *testing.T) { + f := &fake{t: t, reply: optionalFieldReply(provider.Responses)} + f.refuse = func(b []byte) (int, string) { + var v map[string]json.RawMessage + json.Unmarshal(b, &v) + if _, present := v["prompt_cache_options"]; present { + return 400, `{"error":{"message":"prompt_cache_options is not supported on this model","type":"invalid_request_error","param":"prompt_cache_options","code":"invalid_parameter"}}` + } + return 0, "" + } + setup(t, provider.Responses, f) + srv := New() + path, body := optionalFieldAsk(provider.Responses) + request := string(withFields([]byte(body), map[string]any{"prompt_cache_options": map[string]string{"mode": "explicit"}, "prompt_cache_key": "k1"})) + for turn, want := range []int{2, 1} { + calls := f.calls + code, reply := postTo(t, srv, path, request) + if code != 200 || f.calls-calls != want { + t.Fatalf("turn %d: %d after %d calls, want %d: %s", turn+1, code, f.calls-calls, want, reply) + } + var sent map[string]json.RawMessage + json.Unmarshal(f.got, &sent) + if sent["prompt_cache_options"] != nil || sent["prompt_cache_key"] == nil { + t.Errorf("turn %d: options kept or the key lost: %s", turn+1, f.got) + } + } +} diff --git a/internal/provider/codex_request.go b/internal/provider/codex_request.go index 55c793f2f..85657ba85 100644 --- a/internal/provider/codex_request.go +++ b/internal/provider/codex_request.go @@ -247,7 +247,8 @@ func codexInstructions(model string) string { // codexBody makes a Responses request the one Codex CLI would send: it // streams only, keeps nothing (so its items carry no ids), carries Codex's // instructions — the client's own riding first in the input — and rejects -// the sampling knobs and any service tier but Fast's and Ultrafast's. +// the sampling knobs, explicit cache options and any service tier but +// Fast's and Ultrafast's. func codexBody(body []byte) []byte { dec := json.NewDecoder(bytes.NewReader(body)) dec.UseNumber() @@ -255,7 +256,12 @@ func codexBody(body []byte) []byte { if dec.Decode(&m) != nil || m == nil { return body } - for _, k := range []string{"max_output_tokens", "max_completion_tokens", "temperature", "top_p", "previous_response_id", "user", "safety_identifier"} { + // prompt_cache_options, which pi-ai sends as {"mode":"explicit"}: the + // ChatGPT backend turns it away on every model it serves ("is not + // supported on this model", 400, tried 2026-10-14 on gpt-5.5, + // gpt-5.6-sol, gpt-6-sol, gpt-6-astra and gpt-6-luna, #2154); its + // cache follows the session-id header instead (#1473) + for _, k := range []string{"max_output_tokens", "max_completion_tokens", "temperature", "top_p", "previous_response_id", "user", "safety_identifier", "prompt_cache_options"} { delete(m, k) } // Codex's Fast mode sends "priority", its Ultrafast "ultrafast"; any diff --git a/internal/provider/codex_request_test.go b/internal/provider/codex_request_test.go index fc10eadca..62ab1b5d5 100644 --- a/internal/provider/codex_request_test.go +++ b/internal/provider/codex_request_test.go @@ -351,3 +351,19 @@ func TestDeclareSearch(t *testing.T) { t.Errorf("Lite: %+v", lite) } } + +// #2154 (neavo): the ChatGPT backend turns prompt_cache_options away on +// every model (400 "prompt_cache_options is not supported on this model"), +// so the Codex request goes without it; the cache key stays. +func TestCodexBodyDropsPromptCacheOptions(t *testing.T) { + t.Setenv("CODEX_HOME", t.TempDir()) + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + var m map[string]any + json.Unmarshal(codexBody([]byte(`{"model":"gpt-5.5","prompt_cache_key":"k","prompt_cache_options":{"mode":"explicit"},"input":"hi"}`)), &m) + if _, ok := m["prompt_cache_options"]; ok { + t.Errorf("prompt_cache_options sent: %v", m) + } + if m["prompt_cache_key"] != "k" { + t.Errorf("cache key lost: %v", m) + } +}