Backtestslitellmrun-20261019-033420#25669
[Bug]: Anthropic multi-turn tool call history destroyed when messages use Anthropic content format (tool_use/tool_result in content blocks)
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Patch A fixes only two of the five points in the issue: it teaches _is_orphaned_tool_result to look at content tool_use blocks and adds tool_result to ValidUserMessageContentTypes. It leaves anthropic_messages_pt untouched, so assistant tool_use blocks and user tool_result blocks are still silently dropped (issue points 2 and 3), and the trailing-assistant problem (point 5) remains. The independent tests confirm this: with Patch A, every test moves from FAIL-EXC to FAIL-ASSERT with the outgoing request showing only the text blocks and no tool_use or tool_result, so the minimal repro from the issue still breaks at Anthropic. Patch B addresses all five points: it adds tool_use handling in the assistant content loop (with dedup against tool_calls via unique_tool_ids and cache_control preservation), tool_result passthrough in the user content loop (keeping content and is_error), extends _add_missing_tool_results and _is_orphaned_tool_result to understand content-block tool calls so dummy results are injected and conversations end with a user turn, and updates the validator. It also updates has_tool_call_blocks and the thinking-block check in utils.py so dummy-tool injection and thinking-drop logic recognise content-block tool_use; this is adjacent but directly needed for the content-block path to behave like the tool_calls path and is tested. All twelve independent tests pass under B and none under A. B's tests exercise anthropic_messages_pt end to end, including the transform_request path, both modify_params settings, is_error/cache_control, dedup, and the no-tools case; A's tests only cover the two helper changes it made. B is larger but the extra code is coherent, well-factored into small helpers, and consistent with surrounding conventions. B is clearly the better fix.
Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.
Solve
41m 22s
Wall-clock time in the container
Full test suite
No new failures
2,907 passed · 26 skipped · 2,934 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
422vs 128 merged
Changed lines, ours vs merged
The issue
View on GitHubCheck for existing issues
- I have searched the existing issues and checked that my issue is not a duplicate.
What happened?
When using LiteLLM's /v1/chat/completions endpoint with Anthropic models, multi-turn conversations involving tool calls break in several ways when the client sends messages in Anthropic content block format (i.e. tool_use in assistant content list, tool_result in user content list) rather than OpenAI tool_calls array format:
_is_orphaned_tool_result()insanitize_messages_for_tool_calling()only checkstool_callsarray for matching tool IDs. It never checkscontent[type=tool_use]blocks. Result: everytool_resultmessage is flagged as orphaned and stripped, destroying conversation history. The model never sees tool results and re-runs the same tools indefinitely in a loop.
anthropic_messages_pt()processes assistant messages with a content list but has no handler fortype=tool_useblocks. They are silently dropped. Same loop effect.
anthropic_messages_pt()processes user messages with a content list but has no handler fortype=tool_resultblocks. They are silently dropped. Anthropic then errors:tool_use ids were found without tool_result blocks immediately after.
ValidUserMessageContentTypesdoes not includetool_result, causingvalidate_chat_completion_user_messages()to reject these messages before they even reach the provider.
anthropic_messages_pt()can produce a trailing assistant message after transforming tool call history, causing Anthropic to reject withconversation must end with a user message.
Expected: Multi-turn conversations with tool calls should work correctly regardless of whether the client sends tool calls in OpenAI format (tool_calls array) or Anthropic format (content blocks).
Files affected:
litellm/litellm_core_utils/prompt_templates/factory.py—_is_orphaned_tool_result(),anthropic_messages_pt()litellm/types/llms/openai.py—ValidUserMessageContentTypes
Steps to Reproduce
- Configure LiteLLM proxy with an Anthropic model
- Send a multi-turn conversation where assistant messages use
content: [{type: "tool_use", id: "toolu_xxx", ...}]and user messages usecontent: [{type: "tool_result", tool_use_id: "toolu_xxx", ...}] - Observe that tool results are stripped, the model loops re-running the same tools, and eventually errors
This is reproducible using Cursor IDE with a custom LiteLLM base URL, which sends messages in Anthropic content block format.
Minimal reproduce:
import openai
client = openai.OpenAI(base_url="http://localhost:4000", api_key="sk-xxx")
client.chat.completions.create(
model="anthropic/claude-sonnet-4-6",
messages=[
{"role": "user", "content": "run ls"},
{"role": "assistant", "content": [
{"type": "text", "text": "Running ls"},
{"type": "tool_use", "id": "toolu_01xxx", "name": "Shell", "input": {"command": "ls"}}
]},
{"role": "user", "content": [
{"type": "tool_result", "tool_use_id": "toolu_01xxx", "content": [{"type": "text", "text": "file.txt"}]}
]}
],
tools=[{"type": "function", "function": {"name": "Shell", "description": "run shell", "parameters": {"type": "object", "properties": {"command": {"type": "string"}}}}}]
)
Relevant log output
litellm.BadRequestError: AnthropicException - tool_use ids were found without tool_result blocks immediately after: toolu_01xxx
litellm.APIConnectionError: Invalid user message at index 2 - invalid content type=tool_result
What part of LiteLLM is this about?
Proxy
What LiteLLM version are you on ?
1.82.3
Twitter / LinkedIn details
_No response_
The two patches
litellm/litellm_core_utils/prompt_templates/factory.pyindex a037360c87..2033c6032c 100644@@ -2090,6 +2090,45 @@ def _sanitize_empty_text_content( return message +def _get_content_tool_use_blocks(message: AllMessageValues) -> List[dict]:+ """+ Return Anthropic-format `tool_use` blocks embedded in an assistant message's+ content list (e.g. `{"type": "tool_use", "id": "toolu_...", ...}`).+ """+ if message.get("role") != "assistant":+ return []+ content = message.get("content")+ if not isinstance(content, list):+ return []+ return [+ block+ for block in content+ if isinstance(block, dict)+ and block.get("type") == "tool_use"+ and isinstance(block.get("id"), str)+ and block.get("id")+ ]+++def _get_content_tool_result_ids(message: AllMessageValues) -> Set[str]:+ """+ Return the `tool_use_id`s of Anthropic-format `tool_result` blocks embedded+ in a user message's content list.+ """+ if message.get("role") != "user":+ return set()+ content = message.get("content")+ if not isinstance(content, list):+ return set()+ result_ids: Set[str] = set()+ for block in content:+ if isinstance(block, dict) and block.get("type") == "tool_result":+ tool_use_id = block.get("tool_use_id")+ if isinstance(tool_use_id, str) and tool_use_id:+ result_ids.add(tool_use_id)+ return result_ids++ def _add_missing_tool_results( # noqa: PLR0915 current_message: AllMessageValues, messages: List[AllMessageValues],@@ -2097,8 +2136,9 @@ def _add_missing_tool_results( # noqa: PLR0915 ) -> Tuple[List[AllMessageValues], int]: """ Case A: Missing tool_result for tool_use (orphaned tool calls)- - If an assistant message has tool_calls but no corresponding tool result follows,- add a dummy tool result message indicating the user did not provide the result.+ - If an assistant message has tool_calls (or Anthropic-format `tool_use`+ content blocks) but no corresponding tool result follows, add a dummy+ tool result message indicating the user did not provide the result. Returns: A tuple of:@@ -2107,14 +2147,15 @@ def _add_missing_tool_results( # noqa: PLR0915 - Number of original messages consumed (to adjust iteration index) """ result_messages: List[AllMessageValues] = []- tool_calls = current_message.get("tool_calls")+ tool_calls = cast(list, current_message.get("tool_calls") or [])+ content_tool_use_blocks = _get_content_tool_use_blocks(current_message) - if not tool_calls or len(cast(list, tool_calls)) == 0:+ if len(tool_calls) == 0 and len(content_tool_use_blocks) == 0: return ([current_message], 0) # Collect all tool_call_ids from this assistant message expected_tool_call_ids = set()- for tool_call in cast(list, tool_calls):+ for tool_call in tool_calls: tool_call_id = None if isinstance(tool_call, dict): tool_call_id = tool_call.get("id")@@ -2122,6 +2163,8 @@ def _add_missing_tool_results( # noqa: PLR0915 tool_call_id = getattr(tool_call, "id", None) if tool_call_id: expected_tool_call_ids.add(tool_call_id)+ for block in content_tool_use_blocks:+ expected_tool_call_ids.add(block["id"]) # Collect actual tool result messages that follow this assistant message found_tool_call_ids = set()@@ -2140,6 +2183,12 @@ def _add_missing_tool_results( # noqa: PLR0915 if tool_call_id and tool_call_id in expected_tool_call_ids: found_tool_call_ids.add(tool_call_id) actual_tool_results.append(next_msg)+ elif next_role == "user":+ # Anthropic-format tool results live inside user content blocks.+ # They are kept in place (not consumed); just mark them as answered.+ found_tool_call_ids.update(+ _get_content_tool_result_ids(next_msg) & expected_tool_call_ids+ ) j += 1 @@ -2159,7 +2208,11 @@ def _add_missing_tool_results( # noqa: PLR0915 # Then add dummy tool results for missing ones for tool_call_id in missing_tool_call_ids: tool_name = "unknown_tool"- for tool_call in cast(list, tool_calls):+ for block in content_tool_use_blocks:+ if block["id"] == tool_call_id:+ tool_name = block.get("name") or "unknown_tool"+ break+ for tool_call in tool_calls: tc_id = None if isinstance(tool_call, dict): tc_id = tool_call.get("id")@@ -2199,7 +2252,7 @@ def _is_orphaned_tool_result( """ Case B: Orphaned tool_result (unexpected result) - Check if a tool message references a tool_call_id that doesn't exist in the previous- assistant message.+ assistant message (either in its `tool_calls` or its `tool_use` content blocks). Returns: True if this is an orphaned tool result that should be removed, False otherwise@@ -2230,6 +2283,12 @@ def _is_orphaned_tool_result( if tc_id == tool_call_id: found_matching_tool_call = True break+ # Anthropic-format clients put tool calls in content blocks+ if not found_matching_tool_call:+ for block in _get_content_tool_use_blocks(prev_msg):+ if block["id"] == tool_call_id:+ found_matching_tool_call = True+ break break @@ -2503,6 +2562,34 @@ def anthropic_messages_pt( # noqa: PLR0915 _file_content_element, ) )+ elif m.get("type", "") == "tool_result":+ # Anthropic-format tool result sent inside user content+ # (e.g. by Cursor) - pass through as a native block.+ _tool_result_block = cast(dict, m)+ _anthropic_tool_result = AnthropicMessagesToolResultParam(+ type="tool_result",+ tool_use_id=_tool_result_block["tool_use_id"],+ )+ if _tool_result_block.get("content") is not None:+ _anthropic_tool_result[+ "content"+ ] = _tool_result_block["content"]+ if _tool_result_block.get("is_error") is not None:+ _anthropic_tool_result[+ "is_error"+ ] = _tool_result_block["is_error"]+ _tool_result_element = add_cache_control_to_content(+ anthropic_content_element=cast(+ dict, _anthropic_tool_result+ ),+ original_content_element=dict(_tool_result_block),+ )+ user_content.append(+ cast(+ AnthropicMessagesToolResultParam,+ _tool_result_element,+ )+ ) elif isinstance(user_message_types_block["content"], str): _anthropic_content_text_element: AnthropicMessagesTextParam = { "type": "text",@@ -2788,6 +2875,26 @@ def anthropic_messages_pt( # noqa: PLR0915 # Pass through as-is since these are Anthropic-native content types elif m.get("type", "") == "server_tool_use": assistant_content.append(m) # type: ignore+ # handle Anthropic-format client tool calls sent in content+ elif m.get("type", "") == "tool_use":+ _tool_use_id = m.get("id")+ if _tool_use_id and _tool_use_id in unique_tool_ids:+ continue+ if _tool_use_id:+ unique_tool_ids.add(_tool_use_id)+ _anthropic_tool_use = AnthropicMessagesToolUseParam(+ type="tool_use",+ id=cast(str, _tool_use_id),+ name=cast(str, m.get("name", "")),+ input=cast(dict, m.get("input") or {}),+ )+ _cached_tool_use = add_cache_control_to_content(+ anthropic_content_element=_anthropic_tool_use,+ original_content_element=dict(m),+ )+ assistant_content.append(+ cast(AnthropicMessagesToolUseParam, _cached_tool_use)+ ) # handle all *_tool_result blocks (tool_search_tool_result, # web_search_tool_result, bash_code_execution_tool_result, etc.) # Pass through as-is since these are Anthropic-native content typeslitellm/types/llms/openai.pyindex 80b6190db8..8c7dc3bec5 100644@@ -797,6 +797,7 @@ ValidUserMessageContentTypes = [ "guarded_text", "video_url", "file",+ "tool_result", ] # used for validating user messages. Prevent users from accidentally sending anthropic messages. ValidUserMessageContentTypesLiteral = Literal[@@ -808,6 +809,7 @@ ValidUserMessageContentTypesLiteral = Literal[ "guarded_text", "video_url", "file",+ "tool_result", ] ValidUserMessageContentTypes = [@@ -819,6 +821,7 @@ ValidUserMessageContentTypes = [ "guarded_text", "video_url", "file",+ "tool_result", ] # used for validating user messages. Prevent users from accidentally sending anthropic messages. # Assistant message content types (text, thinking, redacted_thinking, image_url)litellm/utils.pyindex 09df88f0ce..f8b1c343f4 100644@@ -7709,9 +7709,25 @@ def has_tool_call_blocks(messages: List[AllMessageValues]) -> bool: for message in messages: if message.get("tool_calls") is not None: return True+ if _has_content_block_of_type(message, ("tool_use",)):+ return True return False +def _has_content_block_of_type(message: AllMessageValues, block_types: tuple) -> bool:+ """+ Returns true if the message's content list contains a block whose `type` is in+ `block_types` (e.g. Anthropic-format `tool_use` / `thinking` content blocks).+ """+ content = message.get("content")+ if not isinstance(content, list):+ return False+ return any(+ isinstance(block, dict) and block.get("type") in block_types+ for block in content+ )++ def any_assistant_message_has_thinking_blocks( messages: List[AllMessageValues], ) -> bool:@@ -7756,12 +7772,21 @@ def last_assistant_with_tool_calls_has_no_thinking_blocks( # Find the last assistant message with tool_calls last_assistant_with_tools = None for message in messages:- if message.get("role") == "assistant" and message.get("tool_calls") is not None:+ if message.get("role") == "assistant" and (+ message.get("tool_calls") is not None+ or _has_content_block_of_type(message, ("tool_use",))+ ): last_assistant_with_tools = message if last_assistant_with_tools is None: return False + # Anthropic-format clients may send thinking blocks inside content+ if _has_content_block_of_type(+ last_assistant_with_tools, ("thinking", "redacted_thinking")+ ):+ return False+ # Check if it has thinking_blocks thinking_blocks = last_assistant_with_tools.get("thinking_blocks") return thinking_blocks is None or (tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.pyindex 30b47a853e..f2801e03dc 100644@@ -2339,3 +2339,274 @@ def test_anthropic_messages_pt_file_block_preserves_cache_control(): assert text_block["type"] == "text" assert "cache_control" in text_block assert text_block["cache_control"]["type"] == "ephemeral"+++# ---------------------------------------------------------------------------+# Anthropic content-block tool call history (tool_use / tool_result in content)+# Regression tests for clients (e.g. Cursor) that send tool calls as Anthropic+# content blocks instead of the OpenAI `tool_calls` array.+# ---------------------------------------------------------------------------+++def _anthropic_format_tool_history():+ return [+ {"role": "user", "content": "run ls"},+ {+ "role": "assistant",+ "content": [+ {"type": "text", "text": "Running ls"},+ {+ "type": "tool_use",+ "id": "toolu_01xxx",+ "name": "Shell",+ "input": {"command": "ls"},+ },+ ],+ },+ {+ "role": "user",+ "content": [+ {+ "type": "tool_result",+ "tool_use_id": "toolu_01xxx",+ "content": [{"type": "text", "text": "file.txt"}],+ }+ ],+ },+ ]+++@pytest.mark.parametrize("modify_params", [True, False])+def test_anthropic_messages_pt_preserves_content_block_tool_history(+ monkeypatch, modify_params+):+ monkeypatch.setattr(litellm, "modify_params", modify_params)++ result = anthropic_messages_pt(+ _anthropic_format_tool_history(),+ model="claude-sonnet-4-6",+ llm_provider="anthropic",+ )++ assert [m["role"] for m in result] == ["user", "assistant", "user"]+ assert result[1]["content"] == [+ {"type": "text", "text": "Running ls"},+ {+ "type": "tool_use",+ "id": "toolu_01xxx",+ "name": "Shell",+ "input": {"command": "ls"},+ },+ ]+ assert result[2]["content"] == [+ {+ "type": "tool_result",+ "tool_use_id": "toolu_01xxx",+ "content": [{"type": "text", "text": "file.txt"}],+ }+ ]+++def test_anthropic_messages_pt_content_block_tool_result_keeps_is_error_and_cache_control():+ messages = _anthropic_format_tool_history()+ messages[2]["content"][0]["is_error"] = True+ messages[2]["content"][0]["cache_control"] = {"type": "ephemeral"}+ messages[1]["content"][1]["cache_control"] = {"type": "ephemeral"}++ result = anthropic_messages_pt(+ messages, model="claude-sonnet-4-6", llm_provider="anthropic"+ )++ tool_use = result[1]["content"][1]+ assert tool_use["cache_control"]["type"] == "ephemeral"+ tool_result = result[2]["content"][0]+ assert tool_result["is_error"] is True+ assert tool_result["cache_control"]["type"] == "ephemeral"+++def test_anthropic_messages_pt_dedupes_content_tool_use_with_tool_calls():+ """A tool_use present both in content and tool_calls is emitted once."""+ messages = _anthropic_format_tool_history()+ messages[1]["tool_calls"] = [+ {+ "id": "toolu_01xxx",+ "type": "function",+ "function": {"name": "Shell", "arguments": '{"command": "ls"}'},+ }+ ]++ result = anthropic_messages_pt(+ messages, model="claude-sonnet-4-6", llm_provider="anthropic"+ )++ tool_uses = [b for b in result[1]["content"] if b["type"] == "tool_use"]+ assert len(tool_uses) == 1+++def test_sanitize_keeps_tool_message_answering_content_tool_use(monkeypatch):+ """role=tool result for a tool_use content block must not be treated as orphaned."""+ monkeypatch.setattr(litellm, "modify_params", True)+ messages = _anthropic_format_tool_history()[:2] + [+ {"role": "tool", "tool_call_id": "toolu_01xxx", "content": "file.txt"}+ ]++ result = sanitize_messages_for_tool_calling(messages)+ assert result == messages++ anthropic_result = anthropic_messages_pt(+ messages, model="claude-sonnet-4-6", llm_provider="anthropic"+ )+ assert anthropic_result[-1]["role"] == "user"+ assert anthropic_result[-1]["content"][0]["type"] == "tool_result"+ assert anthropic_result[-1]["content"][0]["tool_use_id"] == "toolu_01xxx"+++def test_sanitize_does_not_add_dummy_when_content_tool_result_answers_tool_calls(+ monkeypatch,+):+ """OpenAI tool_calls answered by an Anthropic tool_result block need no dummy."""+ monkeypatch.setattr(litellm, "modify_params", True)+ messages = [+ {"role": "user", "content": "run ls"},+ {+ "role": "assistant",+ "content": None,+ "tool_calls": [+ {+ "id": "toolu_01xxx",+ "type": "function",+ "function": {"name": "Shell", "arguments": '{"command": "ls"}'},+ }+ ],+ },+ _anthropic_format_tool_history()[2],+ ]++ result = anthropic_messages_pt(+ messages, model="claude-sonnet-4-6", llm_provider="anthropic"+ )++ tool_results = [b for b in result[-1]["content"] if b["type"] == "tool_result"]+ assert len(tool_results) == 1+ assert tool_results[0]["content"] == [{"type": "text", "text": "file.txt"}]+++def test_sanitize_adds_dummy_result_for_unanswered_content_tool_use(monkeypatch):+ """Trailing assistant tool_use content block gets a dummy result so the+ conversation ends with a user message."""+ monkeypatch.setattr(litellm, "modify_params", True)+ messages = _anthropic_format_tool_history()[:2]++ result = anthropic_messages_pt(+ messages, model="claude-sonnet-4-6", llm_provider="anthropic"+ )++ assert result[-1]["role"] == "user"+ tool_result = result[-1]["content"][0]+ assert tool_result["type"] == "tool_result"+ assert tool_result["tool_use_id"] == "toolu_01xxx"+ assert "Shell" in str(tool_result["content"])+++def test_validate_chat_completion_user_messages_accepts_tool_result():+ from litellm.utils import validate_chat_completion_user_messages++ messages = _anthropic_format_tool_history()+ assert validate_chat_completion_user_messages(messages) == messages+++def test_anthropic_transform_request_with_content_block_tool_history():+ """End-to-end: validation + Anthropic request transform keep tool history."""+ from litellm.llms.anthropic.chat.transformation import AnthropicConfig+ from litellm.utils import validate_and_fix_openai_messages++ messages = validate_and_fix_openai_messages(_anthropic_format_tool_history())+ request = AnthropicConfig().transform_request(+ model="claude-sonnet-4-6",+ messages=messages,+ optional_params={+ "tools": [+ {+ "type": "function",+ "function": {+ "name": "Shell",+ "description": "run shell",+ "parameters": {+ "type": "object",+ "properties": {"command": {"type": "string"}},+ },+ },+ }+ ]+ },+ litellm_params={},+ headers={},+ )++ sent = request["messages"]+ assert sent[-1]["role"] == "user"+ assert sent[1]["content"][-1]["type"] == "tool_use"+ assert sent[1]["content"][-1]["id"] == "toolu_01xxx"+ assert sent[2]["content"][0]["type"] == "tool_result"+ assert sent[2]["content"][0]["tool_use_id"] == "toolu_01xxx"+++def test_anthropic_transform_request_content_block_tool_use_without_tools(+ monkeypatch,+):+ """Content-block tool_use history with no `tools=` gets a dummy tool+ (modify_params) instead of being sent to Anthropic and rejected."""+ from litellm.llms.anthropic.chat.transformation import AnthropicConfig++ monkeypatch.setattr(litellm, "modify_params", True)+ request = AnthropicConfig().transform_request(+ model="claude-sonnet-4-6",+ messages=_anthropic_format_tool_history(),+ optional_params={},+ litellm_params={},+ headers={},+ )+ assert [t["name"] for t in request["tools"]] == ["dummy_tool"]++ monkeypatch.setattr(litellm, "modify_params", False)+ with pytest.raises(litellm.UnsupportedParamsError):+ AnthropicConfig().transform_request(+ model="claude-sonnet-4-6",+ messages=_anthropic_format_tool_history(),+ optional_params={},+ litellm_params={},+ headers={},+ )+++def test_anthropic_transform_request_drops_thinking_for_content_tool_use(+ monkeypatch,+):+ """Thinking is dropped when the last content-block tool_use turn has no+ thinking, but kept when the thinking is sent as a content block."""+ from litellm.llms.anthropic.chat.transformation import AnthropicConfig++ monkeypatch.setattr(litellm, "modify_params", True)+ thinking = {"type": "enabled", "budget_tokens": 1024}++ request = AnthropicConfig().transform_request(+ model="claude-sonnet-4-6",+ messages=_anthropic_format_tool_history(),+ optional_params={"thinking": dict(thinking), "max_tokens": 4096},+ litellm_params={},+ headers={},+ )+ assert "thinking" not in request++ messages = _anthropic_format_tool_history()+ messages[1]["content"].insert(+ 0, {"type": "thinking", "thinking": "hmm", "signature": "sig"}+ )+ request = AnthropicConfig().transform_request(+ model="claude-sonnet-4-6",+ messages=messages,+ optional_params={"thinking": dict(thinking), "max_tokens": 4096},+ litellm_params={},+ headers={},+ )+ assert request["thinking"] == thinking
litellm/litellm_core_utils/prompt_templates/factory.pyindex a037360c87d..61b859f7651 100644@@ -2218,6 +2218,7 @@ def _is_orphaned_tool_result( for j in range(len(sanitized_messages) - 1, -1, -1): prev_msg = sanitized_messages[j] if prev_msg.get("role") == "assistant":+ # Check OpenAI format: tool_calls array tool_calls = prev_msg.get("tool_calls") if tool_calls: for tool_call in cast(list, tool_calls):@@ -2231,6 +2232,16 @@ def _is_orphaned_tool_result( found_matching_tool_call = True break + # Check Anthropic format: content blocks with type=tool_use+ if not found_matching_tool_call:+ content = prev_msg.get("content")+ if isinstance(content, list):+ for block in content:+ if isinstance(block, dict) and block.get("type") == "tool_use":+ if block.get("id") == tool_call_id:+ found_matching_tool_call = True+ break+ break if not found_matching_tool_call:litellm/types/llms/openai.pyindex 80b6190db8f..e53d9c36676 100644@@ -797,7 +797,8 @@ ValidUserMessageContentTypes = [ "guarded_text", "video_url", "file",-] # used for validating user messages. Prevent users from accidentally sending anthropic messages.+ "tool_result",+] # used for validating user messages. Includes tool_result for Anthropic-format content blocks. ValidUserMessageContentTypesLiteral = Literal[ "text",@@ -808,6 +809,7 @@ ValidUserMessageContentTypesLiteral = Literal[ "guarded_text", "video_url", "file",+ "tool_result", ] ValidUserMessageContentTypes = [@@ -819,7 +821,8 @@ ValidUserMessageContentTypes = [ "guarded_text", "video_url", "file",-] # used for validating user messages. Prevent users from accidentally sending anthropic messages.+ "tool_result",+] # used for validating user messages. Includes tool_result for Anthropic-format content blocks. # Assistant message content types (text, thinking, redacted_thinking, image_url) ValidAssistantMessageContentTypesLiteral = Literal[tests/test_litellm/llms/anthropic/test_anthropic_tool_content.pynew file mode 100644index 00000000000..17eea53b49b@@ -0,0 +1,110 @@+"""+Tests for Anthropic-format tool_use handling in sanitization and user-message validation.++Covers:+- _is_orphaned_tool_result matching prior assistant tool_use content blocks+- validate_chat_completion_user_messages accepting tool_result content blocks in user messages+"""++import os+import sys++import pytest++sys.path.insert(+ 0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../.."))+)++from litellm.litellm_core_utils.prompt_templates.factory import _is_orphaned_tool_result+from litellm.types.llms.openai import ValidUserMessageContentTypes+from litellm.utils import validate_chat_completion_user_messages+++class TestIsOrphanedToolResultAnthropicFormat:+ """_is_orphaned_tool_result with Anthropic-format tool_use content blocks."""++ def test_not_orphaned_when_anthropic_tool_use_present(self):+ """Tool message is not orphaned when the latest assistant has a matching tool_use block."""+ sanitized_messages = [+ {+ "role": "assistant",+ "content": [+ {"type": "text", "text": "Let me check that."},+ {+ "type": "tool_use",+ "id": "call_123",+ "name": "get_weather",+ "input": {"city": "SF"},+ },+ ],+ }+ ]+ current_message = {+ "role": "tool",+ "tool_call_id": "call_123",+ "content": "72F sunny",+ }+ assert _is_orphaned_tool_result(current_message, sanitized_messages) is False++ def test_orphaned_when_no_tool_use_in_assistant(self):+ """Tool message is orphaned when the latest assistant has no tool_calls or tool_use."""+ sanitized_messages = [+ {+ "role": "assistant",+ "content": "Just a plain text response",+ }+ ]+ current_message = {+ "role": "tool",+ "tool_call_id": "call_456",+ "content": "result data",+ }+ assert _is_orphaned_tool_result(current_message, sanitized_messages) is True++ def test_not_orphaned_with_openai_format_tool_calls(self):+ """Tool message is not orphaned when the assistant uses OpenAI-format tool_calls."""+ sanitized_messages = [+ {+ "role": "assistant",+ "content": None,+ "tool_calls": [+ {+ "id": "call_789",+ "type": "function",+ "function": {"name": "search", "arguments": "{}"},+ }+ ],+ }+ ]+ current_message = {+ "role": "tool",+ "tool_call_id": "call_789",+ "content": "search results",+ }+ assert _is_orphaned_tool_result(current_message, sanitized_messages) is False+++class TestToolResultUserMessageValidation:+ """validate_chat_completion_user_messages with Anthropic-style tool_result blocks."""++ def test_tool_result_in_valid_types(self):+ """tool_result is an allowed user content type for mixed Anthropic-style payloads."""+ assert "tool_result" in ValidUserMessageContentTypes++ def test_validate_accepts_tool_result_in_user_message(self):+ """User messages may include type=tool_result content blocks (e.g. Cursor / Anthropic)."""+ messages = [+ {+ "role": "user",+ "content": [+ {"type": "text", "text": "Here is the tool output."},+ {+ "type": "tool_result",+ "tool_use_id": "call_abc",+ "content": "ok",+ },+ ],+ }+ ]+ out = validate_chat_completion_user_messages(messages)+ assert out == messages