fix(anthropic): preserve tool use cache markers

This commit is contained in:
qWaitCrypto 2026-06-25 23:06:41 +08:00 committed by Teknium
parent a2d6f05d1b
commit 80d71e8d2e
2 changed files with 27 additions and 0 deletions

View file

@ -1970,6 +1970,11 @@ def _convert_assistant_message(m: Dict[str, Any]) -> Dict[str, Any]:
"name": fn.get("name", ""),
"input": parsed_args,
})
if isinstance(m.get("cache_control"), dict):
for block in reversed(blocks):
if isinstance(block, dict) and block.get("type") in {"text", "tool_use"}:
block.setdefault("cache_control", dict(m["cache_control"]))
break
# Kimi's /coding endpoint (Anthropic protocol) requires assistant
# tool-call messages to carry reasoning_content when thinking is
# enabled server-side. Preserve it as a thinking block so Kimi

View file

@ -1024,6 +1024,28 @@ class TestConvertMessages:
assert assistant_blocks[0]["text"] == "Hello from assistant"
assert assistant_blocks[0]["cache_control"] == {"type": "ephemeral"}
def test_assistant_tool_use_cache_control_is_preserved(self):
messages = apply_anthropic_cache_control([
{"role": "system", "content": "System prompt"},
{"role": "user", "content": "Run the tool"},
{
"role": "assistant",
"content": "",
"tool_calls": [
{"id": "tc_1", "function": {"name": "test_tool", "arguments": "{}"}},
],
},
{"role": "tool", "tool_call_id": "tc_1", "content": "result"},
], native_anthropic=True)
_, result = convert_messages_to_anthropic(messages)
assistant_msg = [m for m in result if m["role"] == "assistant"][0]
tool_use = assistant_msg["content"][-1]
assert tool_use["type"] == "tool_use"
assert tool_use["id"] == "tc_1"
assert tool_use["cache_control"] == {"type": "ephemeral"}
def test_tool_cache_control_is_preserved_on_tool_result_block(self):
messages = apply_anthropic_cache_control([
{"role": "system", "content": "System prompt"},