fix(genai): preserve nested-object required and nullable in tool schemas#1808
Open
obchain (obchain) wants to merge 1 commit into
Open
fix(genai): preserve nested-object required and nullable in tool schemas#1808obchain (obchain) wants to merge 1 commit into
obchain (obchain) wants to merge 1 commit into
Conversation
`_get_properties_from_schema` corrupted nested object schemas in two ways: 1. For any nested object property, the source schema's `required` list was discarded and rebuilt as "every property without a `default` key". A schema with `required: ["name", "role"]` and four nullable-optional fields ended up with all six fields marked required. 2. `_is_nullable_schema` only recognised `anyOf: [T, null]` shapes. On a second conversion pass (e.g. when `_dict_to_genai_schema` recurses into an already-converted inner object) it did not recognise an explicit `nullable: true` key, silently dropping the flag. Honour the source `required` list when present and short-circuit `_is_nullable_schema` on explicit `nullable: true`. Adds four regression tests covering both bugs. Closes langchain-ai#1725
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.
Why
Fixes #1725. When converting a JSON Schema with nested
objectproperties to Gemini's tool format,_get_properties_from_schemain_function_utils.pycorrupts nested objects in two distinct ways:Bug A —
requiredoverwritten. For any nested object property, the source schema's explicitrequiredarray was discarded and rebuilt as "every property that lacks adefaultkey". A schema withrequired: ["name", "role"]and four nullable-optional fields ended up with all six fields marked required. This breaks MCP-style tools and any Pydantic-derived schema with a mix of required/optional nested fields.Bug B —
nullabledropped on second pass. The converter is invoked twice during a single tool conversion: once by_format_json_schema_to_gapicon the outer schema, and again when_dict_to_genai_schemarecurses into the inner object. The first pass correctly convertsanyOf: [T, {"type":"null"}]→{type: T, nullable: true}. The second pass calls_is_nullable_schema, which only recognises theanyOfshape — not an explicitnullable: truekey — so the flag silently disappears.What
_get_properties_from_schema: honour the source schema'srequiredlist when present; default to[]when absent. JSON Schema and Pydantic'smodel_json_schema()always setrequiredexplicitly, so a missing key correctly means "no required fields" rather than "everything without a default"._is_nullable_schema: short-circuit on explicitnullable: trueso re-entrant conversion does not drop the flag.tests/unit_tests/test_function_utils.py:test_nested_object_honors_source_required— Bug A happy path.test_nested_object_without_required_defaults_to_empty— Bug A fallback (covers the real production case where a nested object schema has norequiredkey).test_is_nullable_schema_honors_explicit_flag— Bug B unit-level.test_nested_anyof_nullable_preserved_through_reconversion— Bug B end-to-end.Areas requiring careful review
requiredkey. Previously the converter synthesised one from "properties lackingdefault"; now a missingrequiredis treated as "no required fields". This matches the JSON Schema spec and how Pydantic / MCP tools emit schemas, but is technically a behaviour change for hand-written dict schemas that relied on the synthesis. I believe the synthesis was buggy heuristic rather than intentional — the outer schema does not use it — but worth a second look._is_nullable_schemais additive (one new short-circuit) and should not affect the existinganyOf/typebranches.Disclaimer
AI agents were involved in drafting this contribution; the diagnosis, fix, and tests were reviewed manually before submission.