Skip to content

fix: set required fields in FunctionDeclaration json_schema fallback path#4799

Open
aperepel wants to merge 1 commit intogoogle:mainfrom
aperepel:fix/required-fields-json-schema-fallback
Open

fix: set required fields in FunctionDeclaration json_schema fallback path#4799
aperepel wants to merge 1 commit intogoogle:mainfrom
aperepel:fix/required-fields-json-schema-fallback

Conversation

@aperepel
Copy link

Summary

  • Fix from_function_with_options() to set required fields when the parameters_json_schema fallback path is used
  • Add regression test covering the bug

Problem

When a FunctionTool wraps a function with parameter types that cause _parse_schema_from_parameter to raise ValueError (e.g. list[str] | None), from_function_with_options() falls into the parameters_json_schema code path. This path never set required on the generated FunctionDeclaration, 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 query parameter when calling a tool because the schema had required: None.

Root cause

The primary path (parameters_properties) calls _get_required_fields(), but the fallback path (parameters_json_schema) did not:

if parameters_properties:
    # ... sets required ✅
elif parameters_json_schema:
    # ... never set required ❌

Fix

Compute required directly from inspect.signature(func) in the fallback path — parameters with no default are required. Using _get_required_fields() doesn't work here because it relies on nullable/default metadata on Schema objects, which isn't preserved through the _generate_json_schema_for_parameterSchema.model_validate() round-trip.

Test plan

  • New test: test_from_function_with_options_required_fields_with_complex_union — verifies query (no default) is in required while mode (has default) and tags (list[str] | None = None) are not
  • All 16 tests in test_from_function_with_options.py pass
  • All 81 tests in related tool test files pass

Fixes #4798

…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
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 FunctionDeclaration objects, particularly when generated via a fallback path for complex parameter types, failed to correctly identify and mark required function parameters. This led to LLMs potentially omitting essential arguments when calling tools. The fix ensures that the required fields are accurately determined directly from the function's signature, thereby maintaining the integrity of tool calls.

Highlights

  • Bug Fix: Fixed from_function_with_options() to correctly set required fields in FunctionDeclaration when the parameters_json_schema fallback path is used, addressing an issue where LLMs treated all parameters as optional.
  • Testing: Added a new regression test, test_from_function_with_options_required_fields_with_complex_union, to cover the bug and verify the fix.

🧠 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
  • src/google/adk/tools/_automatic_function_calling_util.py
    • Added logic to determine and set required fields for FunctionDeclaration when using the parameters_json_schema fallback path, by inspecting the function's signature for parameters without default values.
  • tests/unittests/tools/test_from_function_with_options.py
    • Added a new regression test test_from_function_with_options_required_fields_with_complex_union to verify that required fields are correctly set even when complex union types trigger the json_schema fallback.
Activity
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@adk-bot adk-bot added the tools [Component] This issue is related to tools label Mar 12, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rohityan rohityan self-assigned this Mar 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tools [Component] This issue is related to tools

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: required fields lost in FunctionDeclaration when parameters_json_schema fallback is used

3 participants