You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(genai): preserve field descriptions and enum values for Union types with None in function schemas (#1092)
<!--
# Thank you for contributing to LangChain-google!
-->
<!--
## Checklist for PR Creation
- [ ] PR Title: "[package]: [brief description]"
- Where "package" is genai, vertexai, or community
- Use "docs: ..." for purely docs changes, "templates: ..." for template
changes, "infra: ..." for CI changes
- Example: "community: add foobar LLM"
- [ ] PR Description and Relevant issues:
- Description of the change
- Relevant issues (if applicable)
- Any dependencies required for this change
- [ ] Add Tests and Docs:
- If adding a new integration:
1. Include a test for the integration (preferably unit tests that do not
rely on network access)
2. Add an example notebook showing its use (place in the
`docs/docs/integrations` directory)
- [ ] Lint and Test:
- Run `make format`, `make lint`, and `make test` from the root of the
package(s) you've modified
- See contribution guidelines for more:
https://github.com/langchain-ai/langchain-google/blob/main/README.md#contribute-code
-->
<!--
## Additional guidelines
- [ ] PR title and description are appropriate
- [ ] Necessary tests and documentation have been added
- [ ] Lint and tests pass successfully
- [ ] The following additional guidelines are adhered to:
- Optional dependencies are imported within functions
- No unnecessary dependencies added to pyproject.toml files (except
those required for unit tests)
- PR doesn't touch more than one package
- Changes are backwards compatible
-->
## PR Description
<!-- e.g. "Implement user authentication feature" -->
- When using ChatGoogleGenerativeAI with tools that have parameters
typed as unions with None (e.g., list[str] | None), the field
descriptions and enum values are not being passed to the Gemini model.
This occurs because the schema conversion process loses critical field
properties when handling anyOf JSON schema structures.
## Relevant issues
<!-- e.g. "Fixes #000" -->
Fixes: #1091
## Type
<!-- Select the type of Pull Request -->
<!-- Keep only the necessary ones -->
🐛 Bug Fix
## Changes(optional)
<!-- List of changes -->
- **Preserve field descriptions**: Preserve description before any
schema manipulation. Maintain existing behaviour of preferring the
description from the filtered schema, but fall back to original
description if not present.
- **Preserve enum values**: When handling anyOf schemas for nullable
union types, preserve original enum and items properties that would
otherwise be lost during schema filtering.
- **Enhanced items schema processing**: Added enum preservation in
`_get_items_from_schema` to ensure array item constraints (like Literal
types) are properly passed to Gemini.
- **Extended schema filtering**: Added handling for all anyOf types (not
just arrays/objects) to preserve enums in nullable string literals like
`Optional[Literal["A", "B", "C"]]`.
This fix ensures that field descriptions, enum constraints, and array
item specifications are properly preserved and passed to Google Gemini
models when using Union types with None, enabling proper function
calling with constrained parameters.
## Testing(optional)
<!-- Test procedure -->
- **Unit Tests**: All unit tests pass, including specific
`test_function_utils.py` tests that validate schema preservation
- **Integration Tests**: Previously failing CI tests now pass:
- `test_tool_calling_async` - (was failing in initial commit due to
missing enum constraints)
- `test_structured_output[typeddict]` - (was failing in initial commit
due to lost field descriptions)
- **Schema Validation**: Verified enum preservation for
`Optional[Literal["A", "B", "C"]]` types
- **Backward Compatibility**: Confirmed all existing functionality
remains intact
<!-- Test result -->
- **Before fix**: Descriptions and enum values were lost during anyOf
schema processing, resulting in unconstrained parameters and failed tool
calls
- **After fix**: Descriptions and enum values are properly preserved for
all nullable union types, enabling correct function calling with
constrained parameters
## Note(optional)
<!-- Information about the errors fixed by PR -->
The original issue only affected nullable union types with specific
constraints (enums, literals). Regular non-nullable fields and simple
nullable fields without constraints were unaffected. This fix
specifically targets the anyOf schema processing logic to ensure all
field metadata is preserved during schema transformation.
<!-- Remaining issue or something -->
<!-- Other information about PR -->
---------
Co-authored-by: Mason Daugherty <[email protected]>
Co-authored-by: Mason Daugherty <[email protected]>
Co-authored-by: Copilot <[email protected]>
0 commit comments