fix(tool): route non-object JSON inputs through the JSON-error path#3204
fix(tool): route non-object JSON inputs through the JSON-error path#3204adityasingh2400 wants to merge 1 commit intoopenai:mainfrom
Conversation
PR openai#3166 added a `ModelBehaviorError` for non-object function-tool input JSON, but the new error was raised without a `JSONDecodeError` cause and therefore bypassed both `default_tool_error_function` and the `_build_handled_function_tool_error_handler` JSON-decode-error branch. As a result the model received the generic "An error occurred while running the tool" message instead of the helpful "An error occurred while parsing tool arguments. Please try again with valid JSON" hint, and traces showed `Error running tool (non-fatal)` instead of the JSON-input-specific `Error running tool` span. Add a small `_is_tool_argument_input_error` helper that recognises the structural "Invalid JSON input for tool" `ModelBehaviorError` and route both consumers through the JSON-input branch when no decode error is attached. Add a parametrized regression test asserting the default failure formatter returns the JSON-arguments hint for non-object inputs. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0e55ccf29f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| return isinstance(error, ModelBehaviorError) and str(error).startswith( | ||
| "Invalid JSON input for tool" | ||
| ) |
There was a problem hiding this comment.
Narrow argument-input detection to non-object parse failures
_is_tool_argument_input_error now matches every ModelBehaviorError whose text starts with Invalid JSON input for tool. Schema validation failures raised later use that same prefix, so wrong/missing fields are now reported as JSON parse errors with the JSON-input trace message, not the previous validation/non-fatal path. This goes beyond the non-object JSON case described by the change.
Useful? React with 👍 / 👎.
Summary
PR #3166 added a
ModelBehaviorErrorraised when function tool inputJSON decodes to a non-object value (e.g.
[],\"value\",123,null,true). The new error is correctly raised, but it isn't routed throughthe JSON-input error branches that were already in place for genuine
JSONDecodeErrors, because noJSONDecodeErroris attached as acause/context.
That has two visible effects today:
default_tool_error_function) returnsthe generic
\"An error occurred while running the tool. Please try again. Error: ...\"string instead of the more helpful\"An error occurred while parsing tool arguments. Please try again with valid JSON. Error: ...\"hint that tells the model to fix its arguments.\"Error running tool (non-fatal)\"instead of theJSON-input-specific
\"Error running tool\"span.This PR adds a tiny
_is_tool_argument_input_errorhelper that detectsthe structural
\"Invalid JSON input for tool ...\"ModelBehaviorErrorand routes both
default_tool_error_functionand_build_handled_function_tool_error_handlerthrough the JSON-inputbranch when no underlying decode error is attached. The existing
_extract_tool_argument_json_errorpath is unchanged for the genuineJSONDecodeErrorcase.A parametrized regression test confirms the default failure formatter
returns the JSON-arguments hint for the same non-object inputs the
existing
test_function_tool_rejects_non_object_json_inputtest covers.Test plan
pytest tests/test_function_tool.py tests/test_function_tool_decorator.py tests/test_function_schema.py(92 passed)pytest tests/test_tool_metadata.py tests/test_tool_origin.py tests/test_custom_tool.py tests/test_tool_use_behavior.py tests/test_tool_context.py tests/test_tool_converter.py tests/test_tool_guardrails.py tests/test_agent_tool_input.py(72 passed)ruff checkandruff format --checkclean