fix(anthropic): correctly inject cache_control into tool_result content block

This commit is contained in:
prajwal-3-14159 2026-08-29 18:06:49 +09:00
parent ae7e50f096
commit ebe9ad0bc7
2 changed files with 26 additions and 1 deletions

View file

@ -374,7 +374,12 @@ class AnthropicCacheControlHook(CustomPromptManagement):
# 1. if string, insert cache control in the message
if isinstance(message_content, str):
message["cache_control"] = control
if message.get("role") == "tool":
message["content"] = [ # pyright: ignore[reportGeneralTypeIssues] # list conversion
{"type": "text", "text": message_content, "cache_control": control}
]
else:
message["cache_control"] = control # pyright: ignore[reportGeneralTypeIssues] # cache_control field
# 2. list of objects - only apply to last item per Anthropic spec
elif isinstance(message_content, list):
if len(message_content) > 0 and isinstance(message_content[-1], dict):

View file

@ -2909,3 +2909,23 @@ class TestRecordGatewayInjection:
custom_llm_provider="anthropic",
)
assert self.KEY not in kwargs["litellm_metadata"]
def test_tool_result_string_content_wrapped_in_list(self):
from litellm.integrations.anthropic_cache_control_hook import AnthropicCacheControlHook
from litellm.types.llms.openai import ChatCompletionCachedContent
message = {
"role": "tool",
"tool_call_id": "call_1",
"content": "Sunny"
}
control = ChatCompletionCachedContent(type="ephemeral")
modified_message = AnthropicCacheControlHook._safe_insert_cache_control_in_message(message, control, False)
# Should be converted to a list
assert isinstance(modified_message["content"], list)
assert len(modified_message["content"]) == 1
assert modified_message["content"][0]["type"] == "text"
assert modified_message["content"][0]["text"] == "Sunny"
assert modified_message["content"][0]["cache_control"] == control