fix(models): forward tool output schema on the LiteLLM path - #6815
Open
schlaepf wants to merge 1 commit into
Open
fix(models): forward tool output schema on the LiteLLM path#6815schlaepf wants to merge 1 commit into
schlaepf wants to merge 1 commit into
Conversation
An MCP tool's declared outputSchema never reached the model when the agent was backed by LiteLlm. _function_declaration_to_tool_param() built the tool payload from name, description and parameters only, ignoring both response_json_schema and response, so the output schema was silently dropped even when the declaration carried it. OpenAI-compatible chat completions tool definitions have no standard field for a tool result schema, so the schema is rendered into the tool description, which is forwarded to the model. Declarations without an output schema keep their description byte-identical. Fixes google#6784
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
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.
Fixes #6784
Description
An MCP tool's declared
outputSchemanever reaches the model when the agent is backed byLiteLlm._function_declaration_to_tool_param()insrc/google/adk/models/lite_llm.pybuilds the tool payload fromname,descriptionandparametersonly. It ignores bothfunction_declaration.responseandfunction_declaration.response_json_schema, so the output schema is silently dropped before the request is built — even whenMCPTool._get_declaration()has populated it correctly. The user-visible symptom is that the model cannot describe or rely on a tool's result shape, the same symptom as #2828 (fixed for the Gemini path inc8e5340).Setting
ADK_ENABLE_JSON_SCHEMA_FOR_FUNC_DECL=1does not help here: the declaration is built correctly, but the LiteLLM conversion still discards it, so the payload is byte-identical with the flag on and off.Approach
OpenAI-compatible chat completions tool definitions have no standard field for the schema of a tool's result. Rather than inventing a non-standard key inside the tool payload, this PR renders the output schema into the tool
description, which is forwarded to the model.A new private helper
_append_response_schema_to_description():response_json_schema(preferred) orresponse(converted via the existing_schema_to_dict),sort_keys=Truefor deterministic output, behind the labelReturns a JSON object conforming to this schema:on a new line,The change is confined to the description;
parametersand every other part of the payload are untouched. Declarations without an output schema produce a byte-identical payload, so existing behaviour is preserved.Testing plan
Unit tests
Four tests added to
tests/unittests/models/test_litellm.py, following the existingtest_function_declaration_to_tool_param*conventions:..._with_response_json_schemaresponse_json_schemais rendered;parametersunchanged..._with_response_schematypes.Schemaresponseis rendered..._without_response_schema..._response_schema_without_descriptionFormatting verified with
pyink --check --config pyproject.tomlon both files (clean).Before / after
Running the reproduction script from #6784 against this branch:
Before — output schema absent from the payload:
After — the declared result shape reaches the model:
{ "type": "function", "function": { "name": "get_widget", "description": "Return a widget.\nReturns a JSON object conforming to this schema: {\"properties\": {\"data\": {\"description\": \"preformatted text\", \"type\": \"string\"}, \"status\": {\"enum\": [\"success\", \"error\"], \"type\": \"string\"}}, \"required\": [\"status\", \"data\"], \"type\": \"object\"}", "parameters": { "type": "object", "properties": {"service": {"type": "string"}}, "required": ["service"] } } } output schema present in payload: TrueThe fix applies regardless of the
JSON_SCHEMA_FOR_FUNC_DECLfeature flag, since it reads whichever ofresponse/response_json_schemathe declaration carries.