fix: normalize incomplete tool requests at extraction layer#1483
Open
gdeyoung wants to merge 1 commit intoagent0ai:mainfrom
Open
fix: normalize incomplete tool requests at extraction layer#1483gdeyoung wants to merge 1 commit intoagent0ai:mainfrom
gdeyoung wants to merge 1 commit intoagent0ai:mainfrom
Conversation
When LLMs output valid JSON with tool_name but missing/malformed tool_args, json_parse_dirty() returned the incomplete dict as-is, causing ValueError crashes in validate_tool_request().
Root cause fix: normalize tool requests in json_parse_dirty() before returning them:
- Missing tool_args -> defaults to {}
- tool_args as string -> converts to dict
- tool_args as non-dict -> defaults to {}
- tool key -> normalized to tool_name
Non-tool requests are left untouched.
Related: agent0ai#1458, agent0ai#1466
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.
Problem
When LLMs output valid JSON with
tool_namebut missing or malformedtool_args,json_parse_dirty()returned the incomplete dict as-is. This causesValueError: Tool request must have a tool_args (type dictionary) fieldcrashes invalidate_tool_request().This is particularly common with:
tool_argswhen emptytool_argsas a JSON string instead of a dict"tool"instead of"tool_name"Root Cause
The extraction layer (
json_parse_dirty()) passes through whateverDirtyJsonparses without ensuring the result has the required fields for a tool request. The validation layer then crashes on incomplete input.Fix
Normalize tool requests at the extraction layer in
json_parse_dirty()before returning them:tool_args→ defaults to{}tool_argsas string → converts to dicttool_argsas non-dict (number, null, etc.) → defaults to{}"tool"key → normalized to"tool_name"Non-tool requests (no
tool_nameortoolkey) are left untouched.Why This Approach
This fixes the root cause rather than band-aiding at the validation layer. Defense-in-depth (try/except at validation) is still good practice, but the extraction layer should guarantee a well-formed tool request structure.
Testing
Verified with 7 test scenarios:
tool_args→ defaults to{}tool_argsas string → converts to dicttool_argsas number → defaults to{}"tool"key → normalized to"tool_name"thoughts+tool_namebut notool_args→ normalizedRelated: #1458, #1466