Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions lm15/providers/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <major>.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:
Expand All @@ -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-<major>.<minor>`` 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-<major>.<minor>`` nor ``gpt-<major>`` 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:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_cache_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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"))))

Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down