BerriAI/litellm · error · Exception
Unable to parse anthropic tool result for message
Error message
Unable to parse anthropic tool result for message: {message} What it means
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.
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
Example fix
# before: odd content shape on a tool message
{"role": "tool", "tool_call_id": "call_1", "content": {"result": "72F"}}
# after: string content
{"role": "tool", "tool_call_id": "call_1", "content": "72F"} Defensive patterns
Strategy: try-catch
Validate before calling
def tool_messages_are_standard(messages: list[dict]) -> bool:
for m in messages:
if m.get("role") in ("tool", "function"):
if not m.get("tool_call_id"):
return False
c = m.get("content")
if not (isinstance(c, str) or isinstance(c, list)):
return False
return True Try / catch
try:
litellm.completion(model="anthropic/claude-...", messages=messages)
except Exception as e:
if "Unable to parse anthropic tool result" in str(e):
# str(e) contains the exact message; stringify/normalize its content and retry
messages = normalize_tool_contents(messages)
else:
raise Prevention
- 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
When it happens
Trigger: 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.
Common situations: Middleware transforming message shapes; version drift in litellm's handled cases; hand-constructed tool responses with unusual content types.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Unable to convert openai tool calls=
- Azure Anthropic requests require an api_base. Set…
- BedrockException - . Enable 'litellm.modify_params=True'…
- Chat provider: Invalid function argument delta
- Either file_data or file_id must be present in the file…
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/9e1f722144669323.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/litellm_core_utils/prompt_templates/factory.py:1692
anthropic_tool_result = AnthropicMessagesToolResultParam(
type="tool_result",
tool_use_id=sanitized_tool_use_id,
content=anthropic_content,
)
if message["role"] == "function":
function_message: Final[ChatCompletionFunctionMessage] = message
tool_call_id = function_message.get("tool_call_id") or str(uuid.uuid4())
# Sanitize tool_use_id to match Anthropic's pattern requirement: ^[a-zA-Z0-9_-]+$
sanitized_tool_use_id = _sanitize_anthropic_tool_use_id(tool_call_id)
anthropic_tool_result = AnthropicMessagesToolResultParam(
type="tool_result",
tool_use_id=sanitized_tool_use_id,
content=anthropic_content,
)
if anthropic_tool_result is None:
raise Exception(f"Unable to parse anthropic tool result for message: {message}")
if cache_control is not None:
anthropic_tool_result["cache_control"] = cache_control
return anthropic_tool_result
def convert_function_to_anthropic_tool_invoke(
function_call: dict | ChatCompletionToolCallFunctionChunk,
) -> list[AnthropicMessagesToolUseParam]:
try:
_name: Final = get_attribute_or_key(function_call, "name") or ""
_arguments: Final = get_attribute_or_key(function_call, "arguments")
tool_input: Final = parse_tool_call_arguments(
_arguments, tool_name=_name, context="Anthropic function to tool invoke"
)
anthropic_tool_invoke: Final = [
AnthropicMessagesToolUseParam(View on GitHub (pinned to 77b7c6c40c)