Skip to content

Comments

feat: enhance JSON argument parsing and validation in CrewAgentExecutor and BaseTool#4573

Open
joaomdmoura wants to merge 4 commits intomainfrom
joaomdmoura/fix-tool-execution
Open

feat: enhance JSON argument parsing and validation in CrewAgentExecutor and BaseTool#4573
joaomdmoura wants to merge 4 commits intomainfrom
joaomdmoura/fix-tool-execution

Conversation

@joaomdmoura
Copy link
Collaborator

@joaomdmoura joaomdmoura commented Feb 23, 2026

Native function calling bypasses schema validation and swallows JSON parse errors

Two bugs in the native function calling path (crew_agent_executor.py):

  1. Silent argument loss — When the LLM returns malformed JSON tool arguments (common with code params containing unescaped quotes), json.loads fails and args_dict is silently set to {}. The tool then gets called with no arguments and crashes with a confusing TypeError instead of telling the agent its JSON was bad.

  2. Schema never enforced — Tool registration stores BaseTool.run as the callable, which is a pure passthrough to _run with zero validation. The args_schema Pydantic model exists and is sent to the LLM, but is never checked server-side. The other code path (tool_usage.pyCrewStructuredTool.invoke) validates properly — this path doesn't.

Fixes

Bug 1: Capture the JSONDecodeError and return a descriptive error message to the agent so it can retry with valid JSON, instead of silently dropping all arguments.

Bug 2: Add args_schema.model_validate(kwargs) in BaseTool.run() and Tool.run() before calling _run/func, bringing the native path to parity with the invoke path. Positional-arg calls and schema-less tools are unaffected.

…or and BaseTool

- Added error handling for malformed JSON tool arguments in CrewAgentExecutor, providing descriptive error messages.
- Implemented schema validation for tool arguments in BaseTool, ensuring that invalid arguments raise appropriate exceptions.
- Introduced tests to verify correct behavior for both valid and invalid JSON inputs, enhancing robustness of tool execution.
lorenzejay and others added 3 commits February 24, 2026 09:21
- Introduced a new private method  to handle argument validation for tools, enhancing code clarity and reusability.
- Updated the  method to utilize the new validation method, ensuring consistent error handling for invalid arguments.
- Enhanced exception handling to specifically catch , providing clearer error messages for tool argument validation failures.
- Added a new utility function, parse_tool_call_args, to handle parsing of tool call arguments from JSON strings or dictionaries, enhancing error handling for malformed JSON inputs.
- Updated CrewAgentExecutor and AgentExecutor to utilize the new parsing function, streamlining argument validation and improving clarity in error reporting.
- Introduced unit tests for parse_tool_call_args to ensure robust functionality and correct handling of various input scenarios.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants