fix: set required fields in FunctionDeclaration json_schema fallback path#4799
fix: set required fields in FunctionDeclaration json_schema fallback path#4799aperepel wants to merge 1 commit intogoogle:mainfrom
Conversation
…path When _parse_schema_from_parameter raises ValueError for complex union types (e.g. list[str] | None), from_function_with_options falls into the parameters_json_schema code path. This path never set the `required` field on the generated FunctionDeclaration, causing LLMs to treat all parameters as optional — even mandatory ones with no default. Compute required fields from the function signature directly in the fallback path, since _get_required_fields() relies on nullable/default metadata that is not preserved by the json_schema fallback. Fixes google#4798
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical bug where Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request fixes a bug where required parameters were not correctly identified when using a fallback path for schema generation. The change correctly populates the required field by inspecting the function's signature for parameters without default values. A regression test is included to verify the fix. The implementation appears correct and effectively resolves the issue.
Summary
from_function_with_options()to setrequiredfields when theparameters_json_schemafallback path is usedProblem
When a
FunctionToolwraps a function with parameter types that cause_parse_schema_from_parameterto raiseValueError(e.g.list[str] | None),from_function_with_options()falls into theparameters_json_schemacode path. This path never setrequiredon the generatedFunctionDeclaration, causing LLMs to treat all parameters as optional — even mandatory ones with no default value.We observed this in production: Gemini Flash omitted a mandatory
queryparameter when calling a tool because the schema hadrequired: None.Root cause
The primary path (
parameters_properties) calls_get_required_fields(), but the fallback path (parameters_json_schema) did not:Fix
Compute
requireddirectly frominspect.signature(func)in the fallback path — parameters with no default are required. Using_get_required_fields()doesn't work here because it relies onnullable/defaultmetadata on Schema objects, which isn't preserved through the_generate_json_schema_for_parameter→Schema.model_validate()round-trip.Test plan
test_from_function_with_options_required_fields_with_complex_union— verifiesquery(no default) is inrequiredwhilemode(has default) andtags(list[str] | None = None) are nottest_from_function_with_options.pypassFixes #4798