crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[BUG] Anthropic and Bedrock history replay crash with uncaught JSONDecodeError on malformed tool_calls arguments

Open AP24110011695 opened this issue 1 month ago • 1 comments

Description

When replaying conversation history, the Anthropic and Bedrock providers convert assistant tool_calls into their native formats and call json.loads() on each stored arguments string without any error handling:

  • lib/crewai/src/crewai/llms/providers/anthropic/completion.py (_format_messages_for_anthropic, "input": json.loads(func.get("arguments", "{}")))
  • lib/crewai/src/crewai/llms/providers/bedrock/completion.py (_format_messages_for_converse, ... else json.loads(func.get("arguments", "{}") or "{}"))

These argument strings come verbatim from the LLM (e.g. the streaming accumulator stores them as raw strings). If one entry is truncated or malformed — for example a stream cut by finish_reason="length", or a provider emitting non-JSON arguments — every subsequent call raises an uncaught json.JSONDecodeError and the agent loop dies with an opaque traceback instead of degrading.

The Gemini provider guards the identical conversion with try/except (json.JSONDecodeError, TypeError) falling back to {} (gemini/completion.py), and OpenAI does the same for tool-argument parsing — so this is an inconsistency, not a design choice.

Steps to Reproduce

No network or API server needed — only history formatting:

from crewai.llms.providers.anthropic.completion import AnthropicCompletion

llm = AnthropicCompletion(model="claude-3-5-sonnet-20241022", api_key="sk-dummy")
messages = [
    {"role": "user", "content": "hi"},
    {"role": "assistant", "content": None, "tool_calls": [
        {"id": "call_1", "type": "function",
         "function": {"name": "search", "arguments": '{"query": "wea'}}]},  # truncated
    {"role": "tool", "tool_call_id": "call_1", "name": "search", "content": "72"},
]
llm._format_messages_for_anthropic(messages)
# json.JSONDecodeError: Unterminated string starting at: char 11 (line 1 column 12)

The same messages crash BedrockCompletion._format_messages_for_converse.

Expected behavior

History replay should tolerate malformed arguments from earlier turns. A malformed entry should degrade to an empty input ({}) like the Gemini provider does, instead of raising and killing the whole conversation.

Possible Solution

Wrap the json.loads in try/except (json.JSONDecodeError, TypeError) and fall back to {}, mirroring gemini/completion.py. Non-string arguments keep passing through unchanged.

I have this implemented with regression tests for both providers (truncated, valid, and empty/dict arguments; constructing the provider with a dummy key and calling only the formatter — no network). Will open a PR linked to this issue.

Additional context

  • crewAI version: current main (97bbfb61).
  • This report and the accompanying fix/tests were prepared with an AI coding assistant, so per CONTRIBUTING.md the PR should carry the llm-generated label (I can't set labels from my account — could a maintainer add it, and bug?).

AP24110011695 avatar Oct 30 '26 23:10 AP24110011695

Pointing at both formatters and the Gemini guard made the Bedrock half quick to check, thank you.

On main 56c26a7, LLM.call() with your truncated history raises JSONDecodeError before any request goes out. With #8849's Bedrock hunk applied, the same call comes back with a normal answer (Claude Haiku 4.5, us-east-1).

One gap is left on that side: arguments that parse but aren't an object (null, [], "weather") still pass through, and Converse rejects them on both Haiku 4.5 and Nova Lite:

null            The value at messages.1.content.0.toolUse.input is empty.
[] / "weather"  The format of the value at messages.1.content.0.toolUse.input is invalid. Provide a json object for the field and try again.

The streaming parser in the same file already maps these to {} (completion.py L1167-L1175). With arguments = parsed if isinstance(parsed, dict) else {} inside #8849's try, those three and the truncated case all got a 200 on both models, and the PR's Bedrock tests still pass. #8342 (for #8341) already handles the Anthropic half the same way. Happy to leave it with you.

kimnamu avatar Nov 23 '26 01:11 kimnamu