crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[BUG] Bedrock Converse forwards OpenAI-style content parts in user and assistant messages unconverted

Open bballhaus opened this issue 1 month ago • 1 comments

Description

BedrockCompletion._format_messages_for_converse passes list content on user and assistant messages through as-is (lib/crewai/src/crewai/llms/providers/bedrock/completion.py, the isinstance(content, list) branch). Converse content blocks are keyed by type ({"text": ...}, {"image": {...}}), so OpenAI-style parts like {"type": "text", "text": ...} or {"type": "image_url", "image_url": {"url": "data:..."}} reach the API in a shape it doesn't accept.

LLMMessage.content documents the list form as multimodal parts, and the agent executor builds image_url parts, so this path is reachable from normal agent use.

#7920 / #7930 cover system and tool messages only; this is the user/assistant side.

Steps to Reproduce

from unittest.mock import patch
from crewai.llm import LLM

llm = LLM(model="bedrock/anthropic.claude-3-5-sonnet-20241022-v2:0")
resp = {"output": {"message": {"role": "assistant", "content": [{"text": "ok"}]}},
        "usage": {"inputTokens": 1, "outputTokens": 1, "totalTokens": 2}}
with patch.object(llm._client, "converse", return_value=resp) as converse:
    llm.call([{"role": "user", "content": [{"type": "text", "text": "look"}]}])
print(converse.call_args.kwargs["messages"])

Expected behavior

[{'role': 'user', 'content': [{'text': 'look'}]}], with image_url data URIs converted to Converse image blocks (as the Anthropic provider does with _convert_image_blocks).

Screenshots/Code snippets

[{'role': 'user', 'content': [{'type': 'text', 'text': 'look'}]}], and an image_url part is forwarded unchanged.

Operating System

Ubuntu 20.04

Python Version

3.10

crewAI Version

1.15.35 (main @ 2666f26)

crewAI Tools Version

1.15.35

Virtual Environment

Venv

Evidence

Repro with a fake Converse client (no AWS calls), Python 3.13:

from unittest.mock import patch
from crewai.llm import LLM

llm = LLM(model="bedrock/anthropic.claude-3-5-sonnet-20241022-v2:0")
resp = {"output": {"message": {"role": "assistant", "content": [{"text": "ok"}]}},
        "usage": {"inputTokens": 1, "outputTokens": 1, "totalTokens": 2}}
with patch.object(llm._client, "converse", return_value=resp) as converse:
    llm.call([{"role": "user", "content": [{"type": "text", "text": "look"}]}])
print(converse.call_args.kwargs["messages"])

Output:

[{'role': 'user', 'content': [{'type': 'text', 'text': 'look'}]}]

An image_url part is forwarded unchanged too:

[{'role': 'user', 'content': [{'type': 'image_url', 'image_url': {'url': 'data:image/png;base64,iVBORw=='}}]}]

Converse content blocks are keyed by type ({"text": ...}, {"image": {...}}), so neither shape is valid for the API.

Possible Solution

In _format_messages_for_converse, convert list content on user and assistant messages instead of passing it through: {"type": "text"} parts to {"text": ...}, and base64 image_url data URIs to Converse image blocks, similar to the Anthropic provider's _convert_image_blocks. Parts that are already Converse-native ({"text": ...}, {"image": ...}) should pass through unchanged. #7930 already moves system/tool content to message_content_text; this is the user/assistant side, so it should coordinate with that PR.

Additional context

Found while adding Bedrock content contract tests for #8128. That PR is test-only; I'll add a strict xfail pointing at this issue there rather than fix it in the same PR.

bballhaus avatar Nov 05 '26 03:11 bballhaus

Thanks for the detailed report and repro — this is a genuine bug. The isinstance(content, list) branch in _format_messages_for_converse forwards OpenAI-style parts ({"type": "text", ...} / {"type": "image_url", ...}) through unchanged on user/assistant messages, while Converse keys blocks by type ({"text": ...}, {"image": ...}).

I've opened a PR to convert these parts to Converse blocks:

  • {"type": "text", "text": ...} → {"text": ...}
  • {"type": "image_url", "image_url": {"url": "data:..."}} → {"image": {"format": ..., "source": {"bytes": ...}}}
  • already-Converse blocks pass through unchanged

See #9153.

fxia0018 avatar Nov 06 '26 04:11 fxia0018