Backtestslitellmrun-20261018-182052#25669

Issue #25669 Merged fix PR # Run 18 Oct 2026 · run-20261018-182052 BerriAI/litellm

[Bug]: Anthropic multi-turn tool call history destroyed when messages use Anthropic content format (tool_use/tool_result in content blocks)

Better

Our patch was judged better than the merged fix.

In this judgment, Patch A = our agent, Patch B = the merged fix.

The issue lists five distinct failure points. Patch B fixes only two of them: it adds tool_result to ValidUserMessageContentTypes and teaches _is_orphaned_tool_result to look at tool_use content blocks. It leaves anthropic_messages_pt untouched, so tool_use blocks in assistant content and tool_result blocks in user content are still silently dropped, and the request still ends on an assistant turn. The independent tests confirm this: Patch B fails every test that checks the Anthropic request body (tool_use forwarded, tool_result follows tool_use, conversation ends with user, multi-turn history preserved), with messages like 'assistant tool_use block missing from request' and 'assert assistant == user'. Patch B would still cause the exact Anthropic error quoted in the issue. Patch A addresses all five points: it adds tool_use and tool_result handlers in anthropic_messages_pt (with ID sanitization and dedup against tool_calls), extends orphan detection and dummy-result insertion to cover both formats via a shared helper, fixes the validation list, and as a consequence the conversation ends correctly on the user turn. It also fixes a real latent bug in _add_missing_tool_results where skipping by count could drop interleaved user messages, now handled with explicit consumed indices. All independent tests pass on A. A's tests are substantive end-to-end checks on the serialized request body and sanitizer output, including the issue's own reproduce case; B's tests are mostly unit checks on the two narrow changes. A's extra refactor of the dummy-result code is somewhat broader than strictly required but is coherent and tested, and does not obscure the fix. A is clearly the better and more complete 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

39m 44s

Wall-clock time in the container

Full test suite

No new failures

2,054 passed · 5 skipped · 2,059 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

542vs 128 merged

Changed lines, ours vs merged

Check 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:

  1. _is_orphaned_tool_result() in sanitize_messages_for_tool_calling() only checks tool_calls array for matching tool IDs. It never checks content[type=tool_use] blocks. Result: every tool_result message 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.
  1. anthropic_messages_pt() processes assistant messages with a content list but has no handler for type=tool_use blocks. They are silently dropped. Same loop effect.
  1. anthropic_messages_pt() processes user messages with a content list but has no handler for type=tool_result blocks. They are silently dropped. Anthropic then errors: tool_use ids were found without tool_result blocks immediately after.
  1. ValidUserMessageContentTypes does not include tool_result, causing validate_chat_completion_user_messages() to reject these messages before they even reach the provider.
  1. anthropic_messages_pt() can produce a trailing assistant message after transforming tool call history, causing Anthropic to reject with conversation 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

  1. Configure LiteLLM proxy with an Anthropic model
  2. Send a multi-turn conversation where assistant messages use content: [{type: "tool_use", id: "toolu_xxx", ...}] and user messages use content: [{type: "tool_result", tool_use_id: "toolu_xxx", ...}]
  3. 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

Our patch+486 −56
litellm/litellm_core_utils/prompt_templates/factory.pyindex a037360c87..57dea6d430 100644@@ -2090,11 +2090,73 @@ def _sanitize_empty_text_content(     return message  +def _get_assistant_tool_call_names_by_id(+    message: AllMessageValues,+) -> Dict[str, str]:+    """+    Collect the tool call ids (and tool names) issued by an assistant message.++    Tool calls can be sent either in OpenAI format (`tool_calls` array) or in+    Anthropic format (`{"type": "tool_use", ...}` blocks in the `content` list).+    Both are returned.+    """+    tool_call_names_by_id: Dict[str, str] = {}++    tool_calls = message.get("tool_calls")+    if tool_calls:+        for tool_call in cast(list, tool_calls):+            if isinstance(tool_call, dict):+                tc_id = tool_call.get("id")+                function = tool_call.get("function", {})+                if isinstance(function, dict):+                    tool_name = function.get("name", "unknown_tool")+                else:+                    tool_name = getattr(function, "name", "unknown_tool")+            else:+                tc_id = getattr(tool_call, "id", None)+                function = getattr(tool_call, "function", None)+                tool_name = (+                    getattr(function, "name", "unknown_tool")+                    if function+                    else "unknown_tool"+                )+            if tc_id:+                tool_call_names_by_id[tc_id] = tool_name or "unknown_tool"++    content = message.get("content")+    if isinstance(content, list):+        for block in content:+            if isinstance(block, dict) and block.get("type") == "tool_use":+                block_id = block.get("id")+                if block_id and isinstance(block_id, str):+                    tool_call_names_by_id[block_id] = (+                        block.get("name") or "unknown_tool"+                    )++    return tool_call_names_by_id+++def _get_tool_result_ids_from_user_content(message: AllMessageValues) -> Set[str]:+    """+    Collect tool_use_ids answered by Anthropic-format `{"type": "tool_result", ...}`+    blocks in a user message's `content` list.+    """+    tool_result_ids: Set[str] = set()+    content = message.get("content")+    if isinstance(content, list):+        for block in content:+            if isinstance(block, dict) and block.get("type") == "tool_result":+                tool_use_id = block.get("tool_use_id")+                if tool_use_id and isinstance(tool_use_id, str):+                    tool_result_ids.add(tool_use_id)+    return tool_result_ids++ def _add_missing_tool_results(  # noqa: PLR0915     current_message: AllMessageValues,     messages: List[AllMessageValues],     current_index: int,-) -> Tuple[List[AllMessageValues], int]:+) -> Tuple[List[AllMessageValues], Set[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,@@ -2104,28 +2166,23 @@ def _add_missing_tool_results(  # noqa: PLR0915         A tuple of:         - List containing the assistant message, followed by existing tool results,           followed by any dummy tool results needed-        - Number of original messages consumed (to adjust iteration index)+        - Indices of the original messages consumed (moved into the list above), so+          the caller can skip them without skipping interleaved non-tool messages     """     result_messages: List[AllMessageValues] = []-    tool_calls = current_message.get("tool_calls") -    if not tool_calls or len(cast(list, tool_calls)) == 0:-        return ([current_message], 0)+    # Collect all tool_call_ids from this assistant message (tool_calls array+    # and/or anthropic-format tool_use content blocks)+    tool_call_names_by_id = _get_assistant_tool_call_names_by_id(current_message)+    if not tool_call_names_by_id:+        return ([current_message], set()) -    # Collect all tool_call_ids from this assistant message-    expected_tool_call_ids = set()-    for tool_call in cast(list, tool_calls):-        tool_call_id = None-        if isinstance(tool_call, dict):-            tool_call_id = tool_call.get("id")-        else:-            tool_call_id = getattr(tool_call, "id", None)-        if tool_call_id:-            expected_tool_call_ids.add(tool_call_id)+    expected_tool_call_ids = set(tool_call_names_by_id.keys())      # Collect actual tool result messages that follow this assistant message-    found_tool_call_ids = set()+    found_tool_call_ids: Set[str] = set()     actual_tool_results: List[AllMessageValues] = []+    consumed_indices: Set[int] = set()     j = current_index + 1      while j < len(messages):@@ -2137,9 +2194,20 @@ def _add_missing_tool_results(  # noqa: PLR0915          if next_role in ["tool", "function"]:             tool_call_id = next_msg.get("tool_call_id")-            if tool_call_id and tool_call_id in expected_tool_call_ids:+            if (+                isinstance(tool_call_id, str)+                and tool_call_id in expected_tool_call_ids+            ):                 found_tool_call_ids.add(tool_call_id)                 actual_tool_results.append(next_msg)+                consumed_indices.add(j)+        elif next_role == "user":+            # anthropic-format tool results live inside the user content list.+            # They are kept in place (not consumed), only marked as answered.+            found_tool_call_ids.update(+                _get_tool_result_ids_from_user_content(next_msg)+                & expected_tool_call_ids+            )          j += 1 @@ -2158,26 +2226,7 @@ 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):-                tc_id = None-                if isinstance(tool_call, dict):-                    tc_id = tool_call.get("id")-                else:-                    tc_id = getattr(tool_call, "id", None)--                if tc_id == tool_call_id:-                    if isinstance(tool_call, dict):-                        function = tool_call.get("function", {})-                        if isinstance(function, dict):-                            tool_name = function.get("name", "unknown_tool")-                        else:-                            tool_name = getattr(function, "name", "unknown_tool")-                    else:-                        function = getattr(tool_call, "function", None)-                        if function:-                            tool_name = getattr(function, "name", "unknown_tool")-                    break+            tool_name = tool_call_names_by_id.get(tool_call_id, "unknown_tool")              dummy_tool_result: ChatCompletionToolMessage = {                 "role": "tool",@@ -2186,10 +2235,10 @@ def _add_missing_tool_results(  # noqa: PLR0915             }             result_messages.append(dummy_tool_result) -        # Return the messages and the number of original messages to skip-        return (result_messages, len(actual_tool_results))+        # Return the messages and the original message indices to skip+        return (result_messages, consumed_indices) -    return ([current_message], 0)+    return ([current_message], set())   def _is_orphaned_tool_result(@@ -2212,25 +2261,16 @@ def _is_orphaned_tool_result(     if not tool_call_id:         return False -    # Look back to find the most recent assistant message with tool_calls+    # Look back to find the most recent assistant message and check its tool+    # calls (tool_calls array and/or anthropic-format tool_use content blocks)     found_matching_tool_call = False      for j in range(len(sanitized_messages) - 1, -1, -1):         prev_msg = sanitized_messages[j]         if prev_msg.get("role") == "assistant":-            tool_calls = prev_msg.get("tool_calls")-            if tool_calls:-                for tool_call in cast(list, tool_calls):-                    tc_id = None-                    if isinstance(tool_call, dict):-                        tc_id = tool_call.get("id")-                    else:-                        tc_id = getattr(tool_call, "id", None)--                    if tc_id == tool_call_id:-                        found_matching_tool_call = True-                        break-+            found_matching_tool_call = (+                tool_call_id in _get_assistant_tool_call_names_by_id(prev_msg)+            )             break      if not found_matching_tool_call:@@ -2271,9 +2311,15 @@ def sanitize_messages_for_tool_calling(         return messages      sanitized_messages: List[AllMessageValues] = []+    consumed_indices: Set[int] = set()     i = 0      while i < len(messages):+        # Skip tool results already moved next to their assistant message (Case A)+        if i in consumed_indices:+            i += 1+            continue+         current_message = messages[i]          # Case C: Sanitize empty text content@@ -2288,8 +2334,11 @@ def sanitize_messages_for_tool_calling(             # If dummy tool results were added, extend sanitized_messages and skip consumed messages             if len(result_messages) > 1:                 sanitized_messages.extend(result_messages)-                # Skip the assistant message and any actual tool results that were included-                i += 1 + messages_consumed+                # Skip the assistant message; actual tool results that were included+                # are skipped via consumed_indices (they may follow interleaved+                # user messages, which must be kept)+                consumed_indices.update(messages_consumed)+                i += 1                 continue          # Case B: Check for orphaned tool results@@ -2503,6 +2552,22 @@ def anthropic_messages_pt(  # noqa: PLR0915                                     _file_content_element,                                 )                             )+                        elif m.get("type", "") == "tool_result":+                            # anthropic-format tool result (e.g. clients replaying+                            # anthropic tool call history) - pass through as-is+                            _tool_result_content_element = cast(+                                AnthropicMessagesToolResultParam, dict(m)+                            )+                            _tool_result_id = _tool_result_content_element.get(+                                "tool_use_id"+                            )+                            if _tool_result_id:+                                _tool_result_content_element[+                                    "tool_use_id"+                                ] = _sanitize_anthropic_tool_use_id(+                                    str(_tool_result_id)+                                )+                            user_content.append(_tool_result_content_element)                 elif isinstance(user_message_types_block["content"], str):                     _anthropic_content_text_element: AnthropicMessagesTextParam = {                         "type": "text",@@ -2788,6 +2853,22 @@ 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 tool_use blocks (e.g. clients+                        # replaying anthropic tool call history)+                        elif m.get("type", "") == "tool_use":+                            _tool_use_content_element = cast(+                                AnthropicMessagesToolUseParam, dict(m)+                            )+                            _tool_use_id = _tool_use_content_element.get("id")+                            if _tool_use_id:+                                _tool_use_id = _sanitize_anthropic_tool_use_id(+                                    str(_tool_use_id)+                                )+                                if _tool_use_id in unique_tool_ids:+                                    continue+                                unique_tool_ids.add(_tool_use_id)+                                _tool_use_content_element["id"] = _tool_use_id+                            assistant_content.append(_tool_use_content_element)                         # 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..e2c1a33b05 100644@@ -797,6 +797,7 @@ ValidUserMessageContentTypes = [     "guarded_text",     "video_url",     "file",+    "tool_result",  # anthropic-format tool results, e.g. sent by clients replaying anthropic tool call history ]  # 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",  # anthropic-format tool results, e.g. sent by clients replaying anthropic tool call history ]  ValidUserMessageContentTypes = [@@ -819,6 +821,7 @@ ValidUserMessageContentTypes = [     "guarded_text",     "video_url",     "file",+    "tool_result",  # anthropic-format tool results, e.g. sent by clients replaying anthropic tool call history ]  # used for validating user messages. Prevent users from accidentally sending anthropic messages.  # Assistant message content types (text, thinking, redacted_thinking, image_url)tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.pyindex 30b47a853e..5efc900ea1 100644@@ -2339,3 +2339,243 @@ 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-format tool call history (tool_use / tool_result content blocks)+# ---------------------------------------------------------------------------++ANTHROPIC_FORMAT_TOOL_HISTORY = [+    {"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_anthropic_format_tool_history(+    modify_params, monkeypatch+):+    """+    tool_use blocks in assistant content and tool_result blocks in user content+    must survive conversion, and the conversation must end on the user turn.+    """+    import copy++    monkeypatch.setattr(litellm, "modify_params", modify_params)++    result = anthropic_messages_pt(+        messages=copy.deepcopy(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_dedupes_tool_use_in_content_and_tool_calls():+    """A tool call present both as a tool_use block and in tool_calls is emitted once."""+    messages = [+        {"role": "user", "content": "run ls"},+        {+            "role": "assistant",+            "content": [+                {+                    "type": "tool_use",+                    "id": "toolu_01xxx",+                    "name": "Shell",+                    "input": {"command": "ls"},+                }+            ],+            "tool_calls": [+                {+                    "id": "toolu_01xxx",+                    "type": "function",+                    "function": {"name": "Shell", "arguments": '{"command": "ls"}'},+                }+            ],+        },+        {"role": "tool", "tool_call_id": "toolu_01xxx", "content": "file.txt"},+    ]++    result = anthropic_messages_pt(+        messages=messages, model="claude-sonnet-4-6", llm_provider="anthropic"+    )++    tool_uses = [c for c in result[1]["content"] if c["type"] == "tool_use"]+    assert len(tool_uses) == 1+    assert tool_uses[0]["id"] == "toolu_01xxx"+++def test_sanitize_keeps_tool_message_answering_content_tool_use(monkeypatch):+    """+    A role=tool message answering a tool_use content block must not be dropped+    as orphaned when modify_params=True.+    """+    monkeypatch.setattr(litellm, "modify_params", True)+    messages = [+        {"role": "user", "content": "run ls"},+        {+            "role": "assistant",+            "content": [+                {+                    "type": "tool_use",+                    "id": "toolu_01xxx",+                    "name": "Shell",+                    "input": {"command": "ls"},+                }+            ],+        },+        {"role": "tool", "tool_call_id": "toolu_01xxx", "content": "file.txt"},+    ]++    sanitized = sanitize_messages_for_tool_calling(messages)+    assert sanitized == messages++    result = anthropic_messages_pt(+        messages=messages, model="claude-sonnet-4-6", llm_provider="anthropic"+    )+    assert result[-1]["role"] == "user"+    assert result[-1]["content"][0]["type"] == "tool_result"+    assert result[-1]["content"][0]["tool_use_id"] == "toolu_01xxx"+    assert "file.txt" in json.dumps(result[-1]["content"][0]["content"])+++def test_sanitize_does_not_add_dummy_result_for_content_tool_result(monkeypatch):+    """+    tool_calls answered by an anthropic-format tool_result block must not get+    an extra dummy tool result (Anthropic rejects multiple results per tool_use).+    """+    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"}'},+                }+            ],+        },+        {+            "role": "user",+            "content": [+                {+                    "type": "tool_result",+                    "tool_use_id": "toolu_01xxx",+                    "content": "file.txt",+                }+            ],+        },+    ]++    assert sanitize_messages_for_tool_calling(messages) == messages+++def test_sanitize_adds_dummy_result_for_unanswered_content_tool_use(monkeypatch):+    """Unanswered tool_use content blocks get a dummy result, like tool_calls do."""+    monkeypatch.setattr(litellm, "modify_params", True)+    messages = [+        {"role": "user", "content": "run ls"},+        {+            "role": "assistant",+            "content": [+                {+                    "type": "tool_use",+                    "id": "toolu_01xxx",+                    "name": "Shell",+                    "input": {"command": "ls"},+                }+            ],+        },+        {"role": "user", "content": "never mind"},+    ]++    sanitized = sanitize_messages_for_tool_calling(messages)+    assert [m["role"] for m in sanitized] == ["user", "assistant", "tool", "user"]+    assert sanitized[2]["tool_call_id"] == "toolu_01xxx"+    assert "Shell" in sanitized[2]["content"]+++def test_sanitize_keeps_user_tool_result_before_tool_message_when_adding_dummy(+    monkeypatch,+):+    """+    When a dummy result is added, a user message with a content tool_result that+    sits before a role=tool message must not be skipped (and the role=tool message+    must not be duplicated).+    """+    monkeypatch.setattr(litellm, "modify_params", True)+    messages = [+        {"role": "user", "content": "run things"},+        {+            "role": "assistant",+            "content": None,+            "tool_calls": [+                {+                    "id": f"toolu_{x}",+                    "type": "function",+                    "function": {"name": "Shell", "arguments": "{}"},+                }+                for x in ("a", "b", "c")+            ],+        },+        {+            "role": "user",+            "content": [+                {"type": "tool_result", "tool_use_id": "toolu_a", "content": "A"}+            ],+        },+        {"role": "tool", "tool_call_id": "toolu_b", "content": "B"},+    ]++    result = anthropic_messages_pt(+        messages=messages, model="claude-sonnet-4-6", llm_provider="anthropic"+    )++    assert [m["role"] for m in result] == ["user", "assistant", "user"]+    tool_results = [c for c in result[2]["content"] if c["type"] == "tool_result"]+    assert sorted(c["tool_use_id"] for c in tool_results) == [+        "toolu_a",+        "toolu_b",+        "toolu_c",+    ]tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.pyindex 6b1b9ced24..851c157a1c 100644@@ -3601,3 +3601,89 @@ def test_strip_advisor_blocks_no_op_when_no_advisor_blocks():     original_content = [dict(b) for b in messages[1]["content"]]     result = strip_advisor_blocks_from_messages(messages)     assert result[1]["content"] == original_content+++def test_completion_preserves_anthropic_format_tool_call_history():+    """+    Messages using anthropic content blocks for tool calls (tool_use in assistant+    content, tool_result in user content) must pass validation and reach+    Anthropic intact, ending on the user tool_result turn.+    """+    import json as _json+    from unittest.mock import patch as _patch++    import litellm+    from litellm.llms.custom_httpx.http_handler import HTTPHandler++    client = HTTPHandler()+    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"}},+                },+            },+        }+    ]++    raised = None+    with _patch.object(client, "post") as mock_post:+        try:+            litellm.completion(+                model="anthropic/claude-sonnet-4-6",+                messages=messages,+                tools=tools,+                client=client,+                api_key="sk-test",+            )+        except Exception as e:  # mocked response is not a valid anthropic response+            raised = e++    assert mock_post.call_count == 1, f"request not sent: {raised!r}"+    call_kwargs = mock_post.call_args.kwargs+    body = call_kwargs.get("json") or _json.loads(call_kwargs["data"])+    sent = body["messages"]++    assert [m["role"] for m in sent] == ["user", "assistant", "user"]+    assert {+        "type": "tool_use",+        "id": "toolu_01xxx",+        "name": "Shell",+        "input": {"command": "ls"},+    } in sent[1]["content"]+    assert sent[2]["content"] == [+        {+            "type": "tool_result",+            "tool_use_id": "toolu_01xxx",+            "content": [{"type": "text", "text": "file.txt"}],+        }+    ]tests/test_litellm/test_utils.pyindex 0acbe90130..66dd5bf9f5 100644@@ -3867,3 +3867,23 @@ class TestValidateAndFixThinkingParam:         validate_and_fix_thinking_param(thinking=thinking)         assert "budgetTokens" in thinking         assert "budget_tokens" not in thinking+++def test_validate_chat_completion_user_messages_accepts_anthropic_tool_result():+    """Anthropic-format tool_result blocks in user content must not be rejected."""+    from litellm.utils import validate_chat_completion_user_messages++    messages = [+        {+            "role": "user",+            "content": [+                {+                    "type": "tool_result",+                    "tool_use_id": "toolu_01xxx",+                    "content": [{"type": "text", "text": "file.txt"}],+                }+            ],+        }+    ]++    assert validate_chat_completion_user_messages(messages=messages) == messages 
Merged fix · PR #+126 −2
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