From 31531fb7c56572c231f0ec8854db9d845d2ed5c1 Mon Sep 17 00:00:00 2001 From: Mindfultime Date: Sun, 22 Nov 2026 13:48:31 +0100 Subject: [PATCH] OpenAI cache options for major-only GPT ids (MAP-6): gpt-6-luna reads as 6.0 openai_model_has_cache_options parsed only gpt-., so the gpt-6 family (gpt-6-luna, gpt-6-sol, gpt-6-astra) fell outside the 5.6+ class. A prefix="stable" or prefix_until_index request still carried the prompt_cache_breakpoint but not prompt_cache_options mode=explicit, so the provider also wrote the volatile suffix at 1.25x on every call, and mode="off" sent no off switch. Measured on gpt-6-luna (DSPy #10658): 14,780 tokens written instead of 9,899 up to the mark. A gpt- id followed by "-" or the end now reads as .0. One digit only, so Azure's gpt-35-turbo deployment names stay out of the class. The gpt-. parse is unchanged. Refs: stanfordnlp/dspy#10658 Co-Authored-By: Claude Opus 5.5 Signed-off-by: Mindfultime --- lm15/providers/openai.py | 19 +++++++++++++------ tests/test_cache_mapping.py | 11 +++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lm15/providers/openai.py b/lm15/providers/openai.py index 566c5249..528c4240 100644 --- a/lm15/providers/openai.py +++ b/lm15/providers/openai.py @@ -175,6 +175,9 @@ def _attach_unmapped(provider_data: dict[str, Any], unmapped: list[dict[str, str _GPT_VERSION_RE = re.compile(r"^gpt-(\d+)\.(\d+)") +# gpt-6-luna, gpt-6-sol: a major with no minor reads as .0. One +# digit, so Azure's gpt-35-turbo deployment names stay out of the class. +_GPT_MAJOR_RE = re.compile(r"^gpt-(\d)(?:-|$)") def openai_model_has_cache_options(model: str) -> bool: @@ -183,13 +186,17 @@ def openai_model_has_cache_options(model: str) -> bool: The off switch for cache WRITES is ``prompt_cache_options`` and only that class accepts it (older models reject it with HTTP 400, and their writes are free anyway). Deciding client-side needs a model check — - a table that rots. Stated trade-off: a future family whose name does - not match ``gpt-.`` keeps the provider's implicit writes - on ``mode="off"``; ``extensions={"prompt_cache_options": ...}`` is the - override. + a table that rots. Stated trade-off: a future family whose name + matches neither ``gpt-.`` nor ``gpt-`` keeps the + provider's implicit writes on ``mode="off"``; + ``extensions={"prompt_cache_options": ...}`` is the override. """ - m = _GPT_VERSION_RE.match(model.lower()) - return bool(m) and (int(m.group(1)), int(m.group(2))) >= (5, 6) + name = model.lower() + m = _GPT_VERSION_RE.match(name) + if m: + return (int(m.group(1)), int(m.group(2))) >= (5, 6) + m = _GPT_MAJOR_RE.match(name) + return bool(m) and (int(m.group(1)), 0) >= (5, 6) def _cache_breakpoint_index(request: Request, cache_control: str) -> int | None: diff --git a/tests/test_cache_mapping.py b/tests/test_cache_mapping.py index 79957570..1993241a 100644 --- a/tests/test_cache_mapping.py +++ b/tests/test_cache_mapping.py @@ -37,8 +37,15 @@ def test_openai_model_class_for_cache_options() -> None: assert openai_model_has_cache_options("gpt-5.6-sol") assert openai_model_has_cache_options("gpt-6.0") assert openai_model_has_cache_options("GPT-5.10") + assert openai_model_has_cache_options("gpt-6") + assert openai_model_has_cache_options("gpt-6-luna") # major only reads as 6.0 + assert openai_model_has_cache_options("gpt-6-sol") assert not openai_model_has_cache_options("gpt-5.5") assert not openai_model_has_cache_options("gpt-4.1-mini") + assert not openai_model_has_cache_options("gpt-5") + assert not openai_model_has_cache_options("gpt-5-mini") + assert not openai_model_has_cache_options("gpt-4o") + assert not openai_model_has_cache_options("gpt-35-turbo") # Azure's 3.5 deployment name, not 35 assert not openai_model_has_cache_options("o3") # unknown pattern -> no switch (stated trade-off) assert not openai_model_has_cache_options("chat-latest") @@ -48,6 +55,7 @@ def test_openai_model_class_for_cache_options() -> None: def test_openai_off_sends_the_real_switch_only_on_5_6_class() -> None: lm = OpenAILM(api_key="k", transport=FakeTransport([])) assert _body(lm, _req("gpt-5.6-sol", CacheConfig(mode="off")))["prompt_cache_options"] == {"mode": "explicit"} + assert _body(lm, _req("gpt-6-luna", CacheConfig(mode="off")))["prompt_cache_options"] == {"mode": "explicit"} assert "prompt_cache_options" not in _body(lm, _req("gpt-5.4-mini", CacheConfig(mode="off"))) assert "prompt_cache_breakpoint" not in json.dumps(_body(lm, _req("gpt-5.6-sol", CacheConfig(mode="off")))) @@ -69,6 +77,8 @@ def test_openai_stable_renders_system_as_marked_developer_message() -> None: # Pre-5.6 has no prompt_cache_options; the mark alone. old = _body(lm, _req("gpt-5.4-mini", CacheConfig(prefix="stable"))) assert "prompt_cache_options" not in old and "prompt_cache_breakpoint" in json.dumps(old) + # gpt-6 ids carry no minor; the mark still needs the mode (DSPy #10658). + assert _body(lm, _req("gpt-6-luna", CacheConfig(prefix="stable")))["prompt_cache_options"] == {"mode": "explicit"} def test_openai_history_sends_nothing_implicit_mode_already_trails() -> None: @@ -102,6 +112,7 @@ def test_chat_dialect_off_stable_and_compat_gate() -> None: assert body["messages"][0] == {"role": "system", "content": [ {"type": "text", "text": "SYS", "prompt_cache_breakpoint": {"mode": "explicit"}}]} assert body["prompt_cache_options"] == {"mode": "explicit"} + assert _body(lm, _req("gpt-6-luna", CacheConfig(prefix="stable")))["prompt_cache_options"] == {"mode": "explicit"} groq = OpenAIChatLM(api_key="k", transport=FakeTransport([]), compat="groq") body = _body(groq, _req("openai/gpt-oss-20b", CacheConfig(prefix="stable", key="k"))) assert body["messages"][0]["content"] == "SYS" and "prompt_cache_key" not in body # cache_control none -> nothing