crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

fix(agents): support Bedrock toolUse format in _parse_native_tool_call (fixes #4972)

Open STiFLeR7 opened this issue 6 months ago β€’ 3 comments

Closes #4972

Summary

Restored tool calling for AWS Bedrock Converse API by supporting the toolUse nesting level in native tool parsing.

Changes

  • Modified CrewAgentExecutor._parse_native_tool_call to unwrap the toolUse key.
  • Added type guards to prevent AttributeErrors on malformed responses (addressing CodeRabbit feedback).

Summary by CodeRabbit

  • Bug Fixes

    • Improved tool-call parsing to correctly handle both wrapped and direct payload formats from Bedrock AI models, enhancing compatibility.
  • Tests

    • Added test suite validating tool-call payload parsing for multiple Bedrock response formats to ensure consistent behavior.

Review Change Stack

STiFLeR7 avatar May 22 '26 06:05 STiFLeR7

No actionable comments were generated in the recent review. πŸŽ‰

ℹ️ Recent review info
βš™οΈ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: f50a1a74-678a-4a8d-92c0-dd5b00519910

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between e187e1de76772441a72b39424e335235760faca1 and e454a70af596037049520427cfd87642c8a95b2f.

πŸ“’ Files selected for processing (2)
  • lib/crewai/src/crewai/agents/crew_agent_executor.py
  • lib/crewai/tests/agents/test_repro_4972.py
πŸ’€ Files with no reviewable changes (1)
  • lib/crewai/tests/agents/test_repro_4972.py

πŸ“ Walkthrough

Walkthrough

This PR fixes Bedrock Converse API tool-call parsing by detecting and unwrapping the "toolUse" wrapper structure in CrewAgentExecutor. The executor now recognizes wrapped payloads, safely unwraps them, and extracts arguments from the standard dict structure. Regression tests cover both wrapped and direct Bedrock payload formats.

Changes

Bedrock toolUse wrapper support

Layer / File(s) Summary
Detect and unwrap toolUse wrapper in executor
lib/crewai/src/crewai/agents/crew_agent_executor.py
_is_tool_call_list recognizes dicts containing a "toolUse" key as valid tool calls. _parse_native_tool_call unwraps tool_call["toolUse"] when present and validates the unwrapped value is a dict, returning None if validation fails.
Test wrapped and direct Bedrock formats
lib/crewai/tests/agents/test_repro_4972.py
TestParseNativeToolCall creates a dummy executor and tests _parse_native_tool_call with both wrapped (Bedrock Converse) and direct tool-call payloads, asserting non-None results and correct extraction of call_id, func_name, and func_args.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A wrapper unwraps, a dict appears,
Bedrock's toolUse conquers our fears,
Arguments flow where once they were lost,
Tests prove the fix at minimal cost.
✨ Tool calls now parse, clean and clear!

πŸš₯ Pre-merge checks | βœ… 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 12.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
βœ… Passed checks (4 passed)
Check name Status Explanation
Description Check βœ… Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check βœ… Passed The title clearly and specifically summarizes the main change: adding support for Bedrock toolUse format in the _parse_native_tool_call method to fix the identified issue.
Linked Issues check βœ… Passed The code changes directly address all requirements from issue #4972: unwrapping the toolUse key to parse Bedrock responses correctly, extracting arguments from the input field, adding type guards to prevent errors on malformed responses, and preserving compatibility.
Out of Scope Changes check βœ… Passed All changes are directly scoped to fixing issue #4972: modifications to _parse_native_tool_call in CrewAgentExecutor and comprehensive test coverage for the Bedrock tool-call parsing functionality.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
πŸ§ͺ Generate unit tests (beta)
  • [ ] Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar May 22 '26 06:05 coderabbitai[bot]

@coderabbitai I have added the dict-type guard as requested in the latest commit. Please review.

STiFLeR7 avatar May 23 '26 12:05 STiFLeR7

[!TIP] For best results, initiate chat on the files or code changes.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Get the current state of the modified file
cat src/crewai/agents/crew_agent_executor.py | grep -n "_parse_native_tool_call" -A 40

# Also check the test file
cat repro_4972.py 2>/dev/null || fd -i "repro_4972" --exec cat {}

Length of output: 1673


🏁 Script executed:

#!/bin/bash
# Find the correct path
fd "crew_agent_executor.py" --exec cat {}

Length of output: 61880


@STiFLeR7 Thanks for the update! The dict-type guard is correctly implemented:

if "toolUse" in tool_call:
    tool_call = tool_call["toolUse"]

if not isinstance(tool_call, dict):
    return None

This safely handles the case where toolUse maps to a non-dict value. βœ…

I do have two remaining observations to flag:


1. _is_tool_call_list doesn't recognize the {"toolUse": {...}} shape

_parse_native_tool_call is only invoked if _is_tool_call_list returns True first. Looking at that method, none of its checks match a raw Bedrock converse item of the form {"toolUse": {...}}:

# None of these match {"toolUse": {"toolUseId": ..., "name": ..., "input": ...}}
isinstance(first_item, dict) and "function" in first_item       # False
isinstance(first_item, dict) and "name" in first_item and "input" in first_item  # False

Consider adding a check there:

if isinstance(first_item, dict) and "toolUse" in first_item:
    return True

This is only needed if LiteLLM does not normalise Bedrock converse responses before returning them. If LiteLLM always converts them to OpenAI format upstream, this gap is irrelevant β€” but it may be worth double-checking or adding a comment clarifying that assumption.


2. Test file placement

repro_4972.py is placed at the repository root. It would be better to move it to the tests/ directory with a standard name (e.g. tests/agents/test_crew_agent_executor_bedrock.py) so it runs as part of the normal test suite rather than being a one-off reproduction script.

coderabbitai[bot] avatar May 23 '26 12:05 coderabbitai[bot]