fix(tools): return a structured error when RestApiTool is missing a required path param - #7283
Open
chelsealong wants to merge 2 commits into
Open
chelsealong wants to merge 2 commits into
chelsealong wants to merge 2 commits into
Conversation
…equired path param
RestApiTool._prepare_request_params substitutes filled path parameters
into self.endpoint.path.format(**path_params). An omitted required path
parameter leaves the template key unset, so format() raises an
uncaught KeyError that aborts the whole agent invocation instead of
giving the model a retryable {"error": ...} result, unlike
FunctionTool which already reports missing mandatory args this way.
call() now catches KeyError from _prepare_request_params and returns
a structured error naming the missing path parameter.
This was referenced Sep 25, 2026
Open
Resolve conflict in rest_api_tool.py: upstream/main added an InputValidationError catch and a _format_error_response helper for a separate path-traversal fix. Keep both the KeyError catch (missing required path parameter) and the InputValidationError catch, and route the KeyError message through the new _format_error_response helper for consistency.
Contributor
Author
|
Merged upstream/main to resolve the conflict in |
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.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
Problem:
FunctionToolreturns{"error": ...}when a mandatory argument is missing so the model can see the failure and retry.RestApiTooldoes not:_prepare_request_paramsfillspath_paramsonly for keys the caller actually supplied, then doesself.endpoint.path.format(**path_params). If a required path parameter is omitted (and has no schema default), the format template still has an unfilled{placeholder}, sostr.formatraises an uncaughtKeyErrorwith the OpenAPI parameter's original name (e.g.'userId').RestApiTool.call()only catcheshttpx.TimeoutException/httpx.HTTPStatusError, so thisKeyErrorpropagates out of the tool and aborts the whole agent invocation instead of giving the model a retryable error.Solution:
Wrap the
self._prepare_request_params(...)call inRestApiTool.call()in atry/except KeyError, mirroring the structured-error patternFunctionToolalready uses for missing mandatory args. On a missing path parameter,call()now returns{"error": "Tool <name> execution failed. ... Missing required path parameter '<name>'."}instead of raising.Testing Plan
Unit Tests:
Added
test_call_missing_required_path_param_returns_error, which builds aRestApiToolforGET /users/{userId}/messageswith a requireduserIdpath parameter that has no default, calls it with no args, and asserts the tool returns a structured{"error": ...}naminguserIdinstead of raising, and that no HTTP request is made.Regression proof — reverting only the source fix and running the new test:
With the fix restored:
Formatting/lint on the changed files:
Checklist
AI assistance disclosure
This change was prepared with the assistance of Claude Code (Anthropic), under human review before submission.
🤖 Generated with Claude Code