Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions agent/bedrock_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,13 +446,14 @@ def _convert_content_to_converse(content) -> List[Dict]:
- Content arrays with text/image_url parts → mixed text/image blocks

Filters out empty text blocks — Bedrock's Converse API rejects messages
where a text content block has an empty ``text`` field (ValidationException:
"text content blocks must be non-empty"). Ref: issue #9486.
where a text content block has an empty or whitespace-only ``text`` field
(ValidationException: "text content blocks must contain non-whitespace
text"). Ref: issue #9486, #39829.
"""
if content is None:
return [{"text": " "}]
return [{"text": "(empty message)"}]
if isinstance(content, str):
return [{"text": content}] if content.strip() else [{"text": " "}]
return [{"text": content}] if content.strip() else [{"text": "(empty message)"}]
if isinstance(content, list):
blocks = []
for part in content:
Expand All @@ -464,7 +465,7 @@ def _convert_content_to_converse(content) -> List[Dict]:
part_type = part.get("type", "")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This handles dict-shaped text parts, but _convert_content_to_converse() still appends raw string list entries unchanged on current main (agent/bedrock_adapter.py:512). Please sanitize a content array such as ["\n"] too, and cover it with a regression test.

if part_type == "text":
text = part.get("text", "")
blocks.append({"text": text if text else " "})
blocks.append({"text": text if text.strip() else "(empty message)"})
elif part_type == "image_url":
image_url = part.get("image_url", {})
url = image_url.get("url", "") if isinstance(image_url, dict) else ""
Expand All @@ -486,7 +487,7 @@ def _convert_content_to_converse(content) -> List[Dict]:
# Remote URL — Converse doesn't support URLs directly,
# include as text reference for the model.
blocks.append({"text": f"[Image: {url}]"})
return blocks if blocks else [{"text": " "}]
return blocks if blocks else [{"text": "(empty message)"}]
return [{"text": str(content)}]


Expand Down Expand Up @@ -574,7 +575,7 @@ def convert_messages_to_converse(
})

if not content_blocks:
content_blocks = [{"text": " "}]
content_blocks = [{"text": "(empty message)"}]

# Merge with previous assistant message if needed (strict alternation)
if converse_msgs and converse_msgs[-1]["role"] == "assistant":
Expand All @@ -600,11 +601,11 @@ def convert_messages_to_converse(

# Converse requires the first message to be from the user
if converse_msgs and converse_msgs[0]["role"] != "user":
converse_msgs.insert(0, {"role": "user", "content": [{"text": " "}]})
converse_msgs.insert(0, {"role": "user", "content": [{"text": "(empty message)"}]})

# Converse requires the last message to be from the user
if converse_msgs and converse_msgs[-1]["role"] != "user":
converse_msgs.append({"role": "user", "content": [{"text": " "}]})
converse_msgs.append({"role": "user", "content": [{"text": "(empty message)"}]})

return (system_blocks if system_blocks else None, converse_msgs)

Expand Down
Loading