mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(anthropic): stop injecting empty cache_control on translated thinking blocks
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
parent
f74bc9427b
commit
0b49acea78
2 changed files with 52 additions and 2 deletions
|
|
@ -528,15 +528,15 @@ class LiteLLMAnthropicMessagesAdapter:
|
|||
type="thinking",
|
||||
thinking=content.get("thinking") or "",
|
||||
signature=content.get("signature") or "",
|
||||
cache_control=content.get("cache_control", {}),
|
||||
)
|
||||
self._add_cache_control_if_applicable(content, thinking_block, model)
|
||||
thinking_blocks.append(thinking_block)
|
||||
elif content.get("type") == "redacted_thinking":
|
||||
redacted_thinking_block = ChatCompletionRedactedThinkingBlock(
|
||||
type="redacted_thinking",
|
||||
data=content.get("data") or "",
|
||||
cache_control=content.get("cache_control", {}),
|
||||
)
|
||||
self._add_cache_control_if_applicable(content, redacted_thinking_block, model)
|
||||
thinking_blocks.append(redacted_thinking_block)
|
||||
|
||||
if (
|
||||
|
|
|
|||
|
|
@ -362,6 +362,56 @@ def test_translate_anthropic_messages_to_openai_thinking_blocks():
|
|||
assert result[1]["tool_calls"][0]["id"] == "toolu_01234"
|
||||
|
||||
|
||||
def test_translate_anthropic_to_openai_thinking_blocks_only_preserve_cache_control():
|
||||
adapter = LiteLLMAnthropicMessagesAdapter()
|
||||
request_without_cache_control = {
|
||||
"model": "claude-sonnet-5",
|
||||
"max_tokens": 10,
|
||||
"messages": [
|
||||
{"role": "user", "content": "hi"},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": [
|
||||
{"type": "thinking", "thinking": "hmm", "signature": "sig"},
|
||||
{"type": "redacted_thinking", "data": "redacted"},
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
translated_without_cache_control, _ = adapter.translate_anthropic_to_openai(request_without_cache_control)
|
||||
thinking_blocks_without_cache_control = translated_without_cache_control["messages"][1]["thinking_blocks"]
|
||||
|
||||
assert len(thinking_blocks_without_cache_control) == 2
|
||||
assert "cache_control" not in thinking_blocks_without_cache_control[0]
|
||||
assert "cache_control" not in thinking_blocks_without_cache_control[1]
|
||||
|
||||
request_with_cache_control = {
|
||||
"model": "claude-sonnet-5",
|
||||
"max_tokens": 10,
|
||||
"messages": [
|
||||
{"role": "user", "content": "hi"},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": [
|
||||
{
|
||||
"type": "thinking",
|
||||
"thinking": "hmm",
|
||||
"signature": "sig",
|
||||
"cache_control": {"type": "ephemeral"},
|
||||
},
|
||||
{"type": "redacted_thinking", "data": "redacted"},
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
translated_with_cache_control, _ = adapter.translate_anthropic_to_openai(request_with_cache_control)
|
||||
thinking_blocks_with_cache_control = translated_with_cache_control["messages"][1]["thinking_blocks"]
|
||||
|
||||
assert thinking_blocks_with_cache_control[0]["cache_control"] == {"type": "ephemeral"}
|
||||
|
||||
|
||||
def test_translate_anthropic_messages_to_openai_sets_reasoning_content():
|
||||
"""Reasoning-aware chat providers read reasoning_content, so thinking text must land there.
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue