mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-28 01:32:17 +00:00
fix(anthropic): correctly inject cache_control into tool_result content block
This commit is contained in:
parent
ae7e50f096
commit
ebe9ad0bc7
2 changed files with 26 additions and 1 deletions
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue