feat: enhance JSON argument parsing and validation in CrewAgentExecutor and BaseTool#4573
Open
joaomdmoura wants to merge 4 commits intomainfrom
Open
feat: enhance JSON argument parsing and validation in CrewAgentExecutor and BaseTool#4573joaomdmoura wants to merge 4 commits intomainfrom
joaomdmoura wants to merge 4 commits intomainfrom
Conversation
…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.
- 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Native function calling bypasses schema validation and swallows JSON parse errors
Two bugs in the native function calling path (
crew_agent_executor.py):Silent argument loss — When the LLM returns malformed JSON tool arguments (common with
codeparams containing unescaped quotes),json.loadsfails andargs_dictis silently set to{}. The tool then gets called with no arguments and crashes with a confusingTypeErrorinstead of telling the agent its JSON was bad.Schema never enforced — Tool registration stores
BaseTool.runas the callable, which is a pure passthrough to_runwith zero validation. Theargs_schemaPydantic model exists and is sent to the LLM, but is never checked server-side. The other code path (tool_usage.py→CrewStructuredTool.invoke) validates properly — this path doesn't.Fixes
Bug 1: Capture the
JSONDecodeErrorand 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)inBaseTool.run()andTool.run()before calling_run/func, bringing the native path to parity with theinvokepath. Positional-arg calls and schema-less tools are unaffected.