This commit is contained in:
prajwal-3-14159 2026-09-23 14:51:50 +00:00 • committed by GitHub
commit fb7809833b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 1 deletions

View file

@ -506,7 +506,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

@ -3606,3 +3606,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