Skip to content

Sanitize Gemini tool-parameter schemas#51

Merged
ScriptSmith merged 2 commits into
mainfrom
fix-gemini-schema-sanitize
Jun 8, 2026
Merged

Sanitize Gemini tool-parameter schemas#51
ScriptSmith merged 2 commits into
mainfrom
fix-gemini-schema-sanitize

Conversation

@ScriptSmith

Copy link
Copy Markdown
Member

No description provided.

@greptile-apps

greptile-apps Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR sanitizes tool parameter schemas before forwarding them to the Vertex/Gemini API, which rejects JSON Schema keywords it does not recognize. Both the chat-completions (convert_tools) and responses API (convert_responses_tools_to_vertex) paths are updated.

  • Adds sanitized_parameters, which chains inline_schema_refs (resolves $ref/$defs into place) and retain_supported_keys (strips every keyword not in Gemini's closed Schema allowlist) before passing parameters to VertexFunctionDeclaration.
  • Includes a fix for sibling schema-valued keywords (e.g. an anyOf co-located with a $ref) that themselves carry nested $ref values — each sibling is now passed through resolve_refs before being merged into the resolved target, preventing the allowlist pass from collapsing them to {}.
  • Adds four tests in responses_api_tests and two in a new tool_conversion_tests module covering unsupported-key stripping, $ref/$defs inlining, recursive-ref degradation, property-name/enum-value preservation, and the sibling-$ref case.

Confidence Score: 5/5

Safe to merge — the change is purely additive sanitization applied before parameters reach the Vertex wire format, with no effect on any other code path.

The sanitization pipeline is logically correct: ref inlining runs before the allowlist strip, recursive schemas are detected via the active stack and degraded gracefully, and sibling schema-valued keywords are themselves passed through resolve_refs before being merged. Both changed call-sites are covered by the new tests, including the previously-flagged sibling-anyOf case. No existing behavior is altered for schemas that were already clean.

No files require special attention.

Important Files Changed

Filename Overview
src/providers/vertex/convert.rs Adds ~200 lines of schema sanitization logic (inline_schema_refs, resolve_refs, retain_supported_keys, sanitized_parameters) plus ~240 lines of tests; wires sanitized_parameters into both convert_tools and convert_responses_tools_to_vertex. Logic is correct and well-tested.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["tool.function.parameters\n(Option<Value>)"] --> B["sanitized_parameters()"]
    B --> C{"is Some?"}
    C -- No --> Z["None → passed to Vertex"]
    C -- Yes --> D["inline_schema_refs()"]
    D --> D1["lift root $defs / definitions\ninto lookup table"]
    D1 --> D2["resolve_refs() — recursive"]
    D2 --> D3{"has $ref?"}
    D3 -- No --> D4["recurse into all child values"]
    D3 -- Yes --> D5{"name in active stack?"}
    D5 -- Yes/cycle --> D6["substitute {type: object}"]
    D5 -- No/found --> D7["clone target, push active,\nresolve_refs target, pop active"]
    D5 -- No/not found or non-local --> D6
    D7 --> D8["resolve each sibling via resolve_refs,\nor_insert into resolved target"]
    D8 --> D9["*value = replacement"]
    D6 --> D9
    D4 --> E["retain_supported_keys()"]
    D9 --> E
    E --> E1["map.retain — drop keys not in allowlist"]
    E1 --> E2["recurse into properties values"]
    E2 --> E3["recurse into items (object or array)"]
    E3 --> E4["recurse into anyOf variants"]
    E4 --> Z2["sanitized Value → passed to Vertex"]
Loading

Reviews (3): Last reviewed commit: "Review fixes" | Re-trigger Greptile

Comment thread src/providers/vertex/convert.rs
@ScriptSmith

Copy link
Copy Markdown
Member Author

@greptile-apps

@ScriptSmith ScriptSmith merged commit 4f21307 into main Jun 8, 2026
20 checks passed
@ScriptSmith ScriptSmith deleted the fix-gemini-schema-sanitize branch June 8, 2026 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant