fix(cache_control) treat empty text like None to avoid anthropic api cache_control error
This commit is contained in:
parent
6733a9a538
commit
76efb0153a
2 changed files with 12 additions and 2 deletions
|
|
@ -21,12 +21,14 @@ def _apply_cache_marker(msg: dict, cache_marker: dict) -> None:
|
||||||
msg["cache_control"] = cache_marker
|
msg["cache_control"] = cache_marker
|
||||||
return
|
return
|
||||||
|
|
||||||
if content is None:
|
if content is None or content == "":
|
||||||
msg["cache_control"] = cache_marker
|
msg["cache_control"] = cache_marker
|
||||||
return
|
return
|
||||||
|
|
||||||
if isinstance(content, str):
|
if isinstance(content, str):
|
||||||
msg["content"] = [{"type": "text", "text": content, "cache_control": cache_marker}]
|
msg["content"] = [
|
||||||
|
{"type": "text", "text": content, "cache_control": cache_marker}
|
||||||
|
]
|
||||||
return
|
return
|
||||||
|
|
||||||
if isinstance(content, list) and content:
|
if isinstance(content, list) and content:
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,14 @@ class TestApplyCacheMarker:
|
||||||
_apply_cache_marker(msg, MARKER)
|
_apply_cache_marker(msg, MARKER)
|
||||||
assert msg["cache_control"] == MARKER
|
assert msg["cache_control"] == MARKER
|
||||||
|
|
||||||
|
def test_empty_string_content_gets_top_level_marker(self):
|
||||||
|
"""Empty text blocks cannot have cache_control (Anthropic rejects them)."""
|
||||||
|
msg = {"role": "assistant", "content": ""}
|
||||||
|
_apply_cache_marker(msg, MARKER)
|
||||||
|
assert msg["cache_control"] == MARKER
|
||||||
|
# Must NOT wrap into [{"type": "text", "text": "", "cache_control": ...}]
|
||||||
|
assert msg["content"] == ""
|
||||||
|
|
||||||
def test_string_content_wrapped_in_list(self):
|
def test_string_content_wrapped_in_list(self):
|
||||||
msg = {"role": "user", "content": "Hello"}
|
msg = {"role": "user", "content": "Hello"}
|
||||||
_apply_cache_marker(msg, MARKER)
|
_apply_cache_marker(msg, MARKER)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue