Skip to content

fix: populate required fields in FunctionDeclaration json_schema fallback#4812

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

fix: populate required fields in FunctionDeclaration json_schema fallback#4812
giulio-leone wants to merge 1 commit intogoogle:mainfrom
giulio-leone:fix/required-fields-json-schema-fallback

Conversation

@giulio-leone
Copy link

Summary

Fixes #4798required fields lost in FunctionDeclaration when the parameters_json_schema fallback path is used.

Root Cause

from_function_with_options() has two code paths for building function declarations:

  1. Primary path (parameters_properties): calls _get_required_fields()
  2. Fallback path (parameters_json_schema): triggered when _parse_schema_from_parameter raises ValueError for complex union types (e.g. list[str] | None) — never called _get_required_fields()

Additionally, the fallback path didn't propagate default values from inspect.Parameter to the generated Schema, so _get_required_fields() (which checks schema.default is None) would treat defaulted parameters as required.

Impact

The LLM sees all parameters as optional and may omit required ones. Observed in production: Gemini Flash omitted the mandatory query parameter when calling a tool, because the schema had no required field.

Fix

Three changes to the elif parameters_json_schema branch in from_function_with_options():

Change Purpose
Added _get_required_fields() call Populates required field (mirrors primary path)
Propagate non-None defaults to schema.default Ensures _get_required_fields() correctly excludes defaulted params
Set schema.nullable=True for None-default params Ensures _get_required_fields() correctly excludes nullable params

Test

Added regression test test_required_fields_set_in_json_schema_fallback that verifies:

  • query (no default) → required ✅
  • mode (default='default') → not required ✅
  • tags: list[str] | None = None → not required ✅

Full test suite: 4726 passed, 0 failures

…back

When _parse_schema_from_parameter raises ValueError for complex union
types (e.g. list[str] | None), from_function_with_options falls back to
the parameters_json_schema branch. This branch was missing two things:

1. The _get_required_fields() call to populate declaration.parameters.required
2. Default value propagation from inspect.Parameter to Schema.default/nullable

Without these, the LLM sees all parameters as optional and may omit
required ones.

This fix:
- Adds _get_required_fields() to the elif branch (mirrors primary path)
- Propagates non-None defaults to schema.default
- Sets schema.nullable=True for parameters defaulting to None

Includes regression test with list[str] | None parameter type.

Fixes google#4798

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@gemini-code-assist
Copy link
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@google-cla
Copy link

google-cla bot commented Mar 13, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@adk-bot
Copy link
Collaborator

adk-bot commented Mar 13, 2026

Response from ADK Triaging Agent

Hello @giulio-leone, thank you for creating this PR!

Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). You can find more information at https://cla.developers.google.com/.

Thanks!

@adk-bot adk-bot added the tools [Component] This issue is related to tools label Mar 13, 2026
@rohityan rohityan self-assigned this Mar 13, 2026
@rohityan
Copy link
Collaborator

Hi @giulio-leone , Thank you for your contribution! It appears you haven't yet signed the Contributor License Agreement (CLA). Please visit https://cla.developers.google.com/ to complete the signing process. Once the CLA is signed, we'll be able to proceed with the review of your PR. Thank you!

@rohityan rohityan added the request clarification [Status] The maintainer need clarification or more information from the author label Mar 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

request clarification [Status] The maintainer need clarification or more information from the author 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