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
6 changes: 5 additions & 1 deletion app/prompt_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ def cache_key(
"response_format": response_format or None,
"seed": seed,
"max_tokens": max_tokens,
"stop": stop,
# Normalize stop to a list: "stop": "foo" and "stop": ["foo"]
# produce the same upstream behavior, so they must share a cache
# entry. Without this, a string-vs-list difference fragments the
# cache for semantically identical requests.
"stop": [stop] if isinstance(stop, str) else stop,
"tool_choice": tool_choice,
"top_p": top_p,
"n": n,
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/test_cache_stop_normalization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Regression: prompt cache key must normalize stop to a list.

OpenAI wire format accepts stop as either a string or a list of strings.
stop="foo" and stop=["foo"] produce identical upstream behavior, so
they must map to the same cache key. Without normalization, the two forms
produce different SHA-256 digests and the cache is fragmented."""

from app.prompt_cache import cache_key


def _base_kwargs(**overrides):
kw = {
"model": "gpt-4o",
"messages": [{"role": "user", "content": "hello"}],
"temperature": 0,
"tools": None,
"response_format": None,
"seed": 42,
}
kw.update(overrides)
return kw


def test_stop_string_vs_list_same_key():
k1 = cache_key(**_base_kwargs(stop="END"))
k2 = cache_key(**_base_kwargs(stop=["END"]))
assert k1 == k2


def test_stop_list_order_matters():
k1 = cache_key(**_base_kwargs(stop=["a", "b"]))
k2 = cache_key(**_base_kwargs(stop=["b", "a"]))
assert k1 != k2


def test_stop_multi_element_list_stable():
kw = _base_kwargs(stop=["a", "b", "c"])
k1 = cache_key(**kw)
k2 = cache_key(**kw)
assert k1 == k2