Repository navigation
fix(tools): include the response schema in RestApiTool declarations - #8033
Open
benclarkeio wants to merge 4 commits into
Open
benclarkeio wants to merge 4 commits into
benclarkeio wants to merge 4 commits into
Conversation
- Set response_json_schema on the function declaration from the operation's parsed 2xx response schema, as McpTool does with outputSchema. - Add OperationParser.get_response_json_schema(), which returns None when the operation declares no 2xx response schema.
- Add include_response_schema to RestApiTool, from_parsed_operation and OpenAPIToolset, defaulting to False, so declarations only carry the 2xx response schema when asked and existing requests don't grow. - Test the option on RestApiTool and that OpenAPIToolset forwards it.
- Describe the supported success statuses and media types as one schema. - Preserve explicit null constraints and translate nullable and write-only fields. - Return declared non-JSON bodies as text objects for opted-in tools. - Cover the schema and decoded results with OpenAPI tool tests.
A success response that lists JSON beside another media type, such as `text/csv`, was declared as a union of its JSON shape and a text object, because httpx sends `Accept: */*` and the server may pick either. Declare such a response by its JSON alone and send an `Accept` header listing the media types the declaration describes, keeping parameters such as `version=2` that can select the shape. - Only tools created with `include_response_schema=True` change. - Operations whose every listed success media type is declared, or whose response schema is unknown, send no `Accept`.
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.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
FunctionDeclaration#2828, fix(models): forward tool output schema on the LiteLLM path #6815Problem:
RestApiTool._get_declaration()builds the declaration from the operation's parameters only, so tools generated byOpenAPIToolsetnever tell the model what they return, even thoughOperationParseralready parses the 2xx response schema.Solution:
include_response_schematoOpenAPIToolset,RestApiTool, andRestApiTool.from_parsed_operation(). The option defaults toFalseand enablesresponse_json_schemaon the JSON Schema declaration path.anyOf. When a declared JSON response has an unknown shape, omit the response schema.nullconstraints, translate OpenAPInullable, and make write-only fields optional in the response schema.+json. Explicitly non-JSON responses return a{"text": ...}object, even when the text is valid JSON.include_response_schema=True, a success response that lists JSON beside another media type (for exampleapplication/jsonandtext/csv) is declared by its JSON alone, and the tool sends anAcceptheader listing the media types the declaration describes (hereAccept: application/json). Without that header, httpx sendsAccept: */*and the server can pick either type, so the declaration had to be a union of the JSON shape and a text object. Parameters such asversion=2are kept, since they can select the shape. Operations whose listed success media types are all declared, or whose response schema is unknown, send noAccept.Testing Plan
Unit Tests:
Tests cover opting in through
OpenAPIToolset, nestednullableandwriteOnlyfields, explicitconst: null, and successful array, text,+json, and empty responses.Manual End-to-End (E2E) Tests:
With the pet store spec from #8031:
Before (on
main, where the option doesn't exist, the schema is never included):After:
Checklist