gateway, provider: send no prompt_cache_options to Codex, and ask again without it when an upstream refuses it (#2154)

pi-ai sends {"prompt_cache_options":{"mode":"explicit"}} on the Responses
API. The ChatGPT backend behind a Codex subscription answers 400:

  {"error":{"message":"prompt_cache_options is not supported on this
  model","type":"invalid_request_error","param":"prompt_cache_options",
  "code":"invalid_parameter"}}

Probed on 2026-10-14 with a read-only use of the owner's Codex token
(never refreshed or written): gpt-5.5, gpt-6-sol, gpt-6-astra,
gpt-6-luna and gpt-5.6-sol all refuse it, and the same requests without
the field return 200.

- codexBody drops prompt_cache_options with the other fields the backend
  turns away. prompt_cache_key stays; the backend follows it (#1473).
- prompt_cache_options joins the gateway's optional fields. Its refusal
  names the field bare, at the start of the message, which none of the
  existing patterns read, so unsupportedOptionalField also takes
  "<field> is not supported/unsupported" when the message starts with
  it. Only at the start, so a field named inside an echo of the input is
  not taken for it. As for the other optional fields, the refusal is
  remembered per provider, though OpenAI's wording says "on this model";
  the cost of that is a cache hint left off, never a failed request.

Run: go test -tags nogui for internal/gateway (40m timeout; load ~380
from other sessions), provider, catalog, codexcat and agent; gofmt -l;
go vet darwin, GOOS=linux -tags nogui, GOOS=windows. With each fix taken
out by hand, TestRefusedOptionalNamesOnlyUnsupportedFields,
TestPromptCacheOptionsRefusedRetried and
TestCodexBodyDropsPromptCacheOptions go red.
Not tried: pi-ai itself against a Codex subscription.
This commit is contained in:
yetone
2026-10-14 19:06:51 +08:00
parent f51863b3da
commit 331c27703a
4 changed files with 70 additions and 4 deletions
+10 -1
View File
@@ -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) {
+36 -1
View File
@@ -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)
}
}
}
+8 -2
View File
@@ -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
+16
View File
@@ -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)
}
}