fix(agents): support Bedrock toolUse format in _parse_native_tool_call (fixes #4972)
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.
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.pylib/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.
Comment @coderabbitai help to get the list of available commands and usage tips.
@coderabbitai I have added the dict-type guard as requested in the latest commit. Please review.
[!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.