fix(genai): preserve format/example fields and support integer enums in schema conversion - #1949
Open
Alon Nahmias (alonahmias) wants to merge 1 commit into
Conversation
…in schema conversion - _dict_to_genai_schema never copied 'format' from the intermediate dict into the final schema_dict, so format: date-time, format: enum, etc. were silently dropped for every property. - 'example'/'examples' were missing from _ALLOWED_SCHEMA_FIELDS entirely, even though google.genai.types.Schema natively supports an 'example' field. JSON Schema's plural 'examples' is collapsed to its first element to match Schema.example (singular). - Gemini's Schema.enum only accepts string values, even when the underlying type is INTEGER/NUMBER/BOOLEAN; format: "enum" is required for the API to treat these as enum constraints (see Schema.enum docstring examples). Enum values are now stringified and format is set accordingly, for both object properties and array items. - Removed a redundant duplicate 'enum' assignment in _get_items_from_schema that would have clobbered the new stringified enum with the raw values.
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
While debugging why a Gemini agent kept sending a bare date (
"2026-08-17") instead of the RFC 3339 datetime declared in a tool's OpenAPI schema (format: "date-time", with a matchingexample), I traced the root cause tolangchain_google_genai/_function_utils.py's schema conversion path (_dict_to_genai_schema/_get_properties_from_schema):formatis silently dropped for every property._dict_to_genai_schemabuildsschema_dictfromformatted_schema, but never copiesformatted_schema["format"]into it — even though"format"is in_ALLOWED_SCHEMA_FIELDSand reaches_format_json_schema_to_gapicuntouched. So a property declared withformat: "date-time"never actually reaches the Gemini API call.example/examplesare dropped entirely. They aren't in_ALLOWED_SCHEMA_FIELDSat all, even thoughgoogle.genai.types.Schemahas a nativeexample: Optional[Any]field:Schema.enum's own docstring documents the expected shape for non-string enums:format: "enum"must be set and enum values must be strings even whentypeisINTEGER/NUMBER/BOOLEAN. The current conversion just copies the raw (integer-typed) enum list and never setsformat, so e.g.Literal[100, 110, 120]produces an enum Gemini doesn't recognize correctly.Also found and removed a redundant duplicate
if "enum" in schema: items["enum"] = schema["enum"]in_get_items_from_schema— harmless before this change, but it would have clobbered the new stringified array-item enum with the raw values.Fix
_dict_to_genai_schema: copyformatandexamplefromformatted_schemaintoschema_dict._ALLOWED_SCHEMA_FIELDS: add"example"/"examples";_format_json_schema_to_gapiccollapses plural JSON Schemaexamplesto the singularSchema.example._get_properties_from_schema/_get_items_from_schema: propagateformatgenerally, and forINTEGER/NUMBER/BOOLEANtypes with anenum, stringify the enum values and setformat: "enum", matching Gemini's documented requirement.Testing
test_tool_field_format_is_preserved,test_tool_field_example_is_preserved,test_tool_field_integer_enumtotests/unit_tests/test_function_utils.py.uv run pytest tests/unit_tests— 385 passed (was 382 before, +3 new tests).uv run mypy langchain_google_genai/— no issues.uv run ruff format/ruff check— clean.No existing tests changed behavior; this only adds previously-dropped fields and fixes previously-invalid enum output.