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
25 changes: 14 additions & 11 deletions patches/crewai_bedrock_fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ def _sanitize_tool_blocks(messages: list) -> list:
- If not matched, strips the toolUse blocks (keeps text blocks)
- For each user message with toolResult blocks, checks if the previous
assistant message has matching toolUse blocks
- If not matched, strips the toolResult blocks (keeps text blocks)
- If not matched, strips only the unmatched toolResult blocks (keeps
text blocks and toolResult blocks whose toolUseId does match)

This handles both directions of orphaning:
- Executor added toolUse but result is in a different message format
Expand Down Expand Up @@ -171,7 +172,7 @@ def _sanitize_tool_blocks(messages: list) -> list:
if result_ids:
# Check if previous message (in fixed list) has matching toolUse
prev_msg = fixed[-1] if fixed else None
matched = False
use_ids = set()

if (
prev_msg
Expand All @@ -180,22 +181,25 @@ def _sanitize_tool_blocks(messages: list) -> list:
):
prev_content = prev_msg.get("content", [])
if isinstance(prev_content, list):
use_ids = set()
for block in prev_content:
if isinstance(block, dict) and "toolUse" in block:
tu = block["toolUse"]
if isinstance(tu, dict) and "toolUseId" in tu:
use_ids.add(tu["toolUseId"])
matched = result_ids.issubset(use_ids)

if not matched:
# Strip toolResult blocks, keep text blocks
text_blocks = [
if not result_ids.issubset(use_ids):
# Only strip toolResult blocks whose toolUseId is NOT in use_ids
kept_blocks = [
b for b in content
if isinstance(b, dict) and "text" in b
if not (
isinstance(b, dict)
and "toolResult" in b
and isinstance(b["toolResult"], dict)
and b["toolResult"].get("toolUseId") not in use_ids
)
]
if text_blocks:
fixed.append({"role": "user", "content": text_blocks})
if kept_blocks:
fixed.append({"role": "user", "content": kept_blocks})
i += 1
continue

Expand All @@ -205,7 +209,6 @@ def _sanitize_tool_blocks(messages: list) -> list:
return fixed



def _patch_parse_native_tool_call():
"""Patch CrewAgentExecutor._parse_native_tool_call to fix arg extraction.

Expand Down
Loading