fix(vertex): project tool schemas onto the subset Google accepts - #181
Merged
Conversation
Google's `Schema` parser is strict and rejects the *entire request* over a
single unknown JSON-Schema keyword anywhere in a tool declaration:
400 Invalid JSON payload received. Unknown name "exclusiveMinimum" at
'tools[0].function_declarations[4].parameters.properties[0].value'
Tool schemas written against full JSON Schema — which Anthropic and OpenAI
accept verbatim — therefore made every Gemini request fail. `exclusiveMinimum`,
`additionalProperties` and `oneOf` are the ones that show up in practice.
`sanitize_google_schema` projects a schema onto the documented keyword set,
recursing through `properties`, `items` and `anyOf`. Dropping a constraint is
deliberate and lossy: the tool's description carries the intent for the model
and the caller validates arguments anyway — losing a bound is survivable,
losing every request to a 400 is not.
Exported, because Gemini Live's BidiGenerateContent WebSocket API uses the
same `Schema` type and needs the identical projection.
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
Google's
Schemaparser is strict and rejects the entire request over a single unknown JSON-Schema keyword anywhere in a tool declaration:Tool schemas written against full JSON Schema — which Anthropic and OpenAI accept verbatim — therefore made every Gemini request fail. The keywords that show up in practice:
exclusiveMinimum,additionalProperties,oneOf.Fix
sanitize_google_schemaprojects a schema onto the documented keyword set, recursing throughproperties,itemsandanyOf.vertex.rsapplies it when buildingfunction_declarations.Dropping a constraint is deliberate and lossy: the tool's description carries the intent for the model and the caller validates arguments anyway. Losing a bound is survivable; losing every request to a 400 is not.
The function is exported because Gemini Live's
BidiGenerateContentWebSocket API uses the sameSchematype and needs the identical projection — the Tutor app's realtime path consumes it from here rather than keeping a second copy.Verification
Reproduced and verified against the live API with a real 20-tool set:
400 INVALID_ARGUMENT, three field violations (exclusiveMinimum×2,additionalProperties)cargo test -p llmgreen (3 new tests), clippy clean,cargo fmtapplied.