{"record":{"id":"9e1f722144669323","repo":"BerriAI/litellm","slug":"unable-to-parse-anthropic-tool-result-for-message","errorCode":null,"errorMessage":"Unable to parse anthropic tool result for message: {message}","messagePattern":"Unable to parse anthropic tool result for message: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/prompt_templates/factory.py","lineNumber":1692,"sourceCode":"        anthropic_tool_result = AnthropicMessagesToolResultParam(\n            type=\"tool_result\",\n            tool_use_id=sanitized_tool_use_id,\n            content=anthropic_content,\n        )\n\n    if message[\"role\"] == \"function\":\n        function_message: Final[ChatCompletionFunctionMessage] = message\n        tool_call_id = function_message.get(\"tool_call_id\") or str(uuid.uuid4())\n        # Sanitize tool_use_id to match Anthropic's pattern requirement: ^[a-zA-Z0-9_-]+$\n        sanitized_tool_use_id = _sanitize_anthropic_tool_use_id(tool_call_id)\n        anthropic_tool_result = AnthropicMessagesToolResultParam(\n            type=\"tool_result\",\n            tool_use_id=sanitized_tool_use_id,\n            content=anthropic_content,\n        )\n\n    if anthropic_tool_result is None:\n        raise Exception(f\"Unable to parse anthropic tool result for message: {message}\")\n    if cache_control is not None:\n        anthropic_tool_result[\"cache_control\"] = cache_control\n    return anthropic_tool_result\n\n\ndef convert_function_to_anthropic_tool_invoke(\n    function_call: dict | ChatCompletionToolCallFunctionChunk,\n) -> list[AnthropicMessagesToolUseParam]:\n    try:\n        _name: Final = get_attribute_or_key(function_call, \"name\") or \"\"\n        _arguments: Final = get_attribute_or_key(function_call, \"arguments\")\n\n        tool_input: Final = parse_tool_call_arguments(\n            _arguments, tool_name=_name, context=\"Anthropic function to tool invoke\"\n        )\n\n        anthropic_tool_invoke: Final = [\n            AnthropicMessagesToolUseParam(","sourceCodeStart":1674,"sourceCodeEnd":1710,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/prompt_templates/factory.py#L1674-L1710","documentation":"Converting a message to an Anthropic tool_result block left anthropic_tool_result as None - the message did not match the tool/function shapes the converter handles (or its content was unparseable), so the function raises with the full message for inspection. The function-role branch above sets the result; anything falling through triggers this.","triggerScenarios":"A message reaching the tool-result converter with an unexpected role or content shape (e.g. role 'tool' with non-string, non-block-list content); custom pipelines injecting non-standard message dicts into the tool path.","commonSituations":"Middleware transforming message shapes; version drift in litellm's handled cases; hand-constructed tool responses with unusual content types.","solutions":["Log the offending message from the error text and make tool responses standard: {role: 'tool', tool_call_id: ..., content: <str or block list>}","Normalize messages to the OpenAI tool schema before passing them to litellm","Update litellm - newer versions handle more tool-result shapes"],"exampleFix":"# before: odd content shape on a tool message\n{\"role\": \"tool\", \"tool_call_id\": \"call_1\", \"content\": {\"result\": \"72F\"}}\n\n# after: string content\n{\"role\": \"tool\", \"tool_call_id\": \"call_1\", \"content\": \"72F\"}","handlingStrategy":"try-catch","validationCode":"def tool_messages_are_standard(messages: list[dict]) -> bool:\n    for m in messages:\n        if m.get(\"role\") in (\"tool\", \"function\"):\n            if not m.get(\"tool_call_id\"):\n                return False\n            c = m.get(\"content\")\n            if not (isinstance(c, str) or isinstance(c, list)):\n                return False\n    return True","typeGuard":null,"tryCatchPattern":"try:\n    litellm.completion(model=\"anthropic/claude-...\", messages=messages)\nexcept Exception as e:\n    if \"Unable to parse anthropic tool result\" in str(e):\n        # str(e) contains the exact message; stringify/normalize its content and retry\n        messages = normalize_tool_contents(messages)\n    else:\n        raise","preventionTips":["Emit tool responses as {role: 'tool', tool_call_id, content: str-or-blocks} everywhere","JSON.stringify structured tool outputs before putting them in content","Keep litellm current - tool-result shape handling expands over versions"],"tags":["anthropic","tool-calling","message-conversion"],"backgroundTag":"tool-result-parse-failed","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}