Skip to content

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

Closed

Description

@aperepel

Bug Description

When FunctionTool wraps a Python function that includes parameter types causing _parse_schema_from_parameter to raise ValueError (e.g. list[str] | None), the generated FunctionDeclaration has required: None — even for mandatory (no-default) parameters.

This happens because from_function_with_options() in _automatic_function_calling_util.py has two code paths:

  1. Primary path (parameters_properties): calls _get_required_fields() ✅
  2. Fallback path (parameters_json_schema): never calls _get_required_fields() ❌

The fallback is triggered when any parameter has a complex union type like list[str] | None that _parse_schema_from_parameter can't handle. Since the except ValueError catches the error for all parameters (not just the complex one), the entire function's schema is rebuilt via the fallback — and required is silently dropped for every parameter.

Impact

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

Reproduction

"""Minimal repro — tested with google-adk 1.26.0, Python 3.12"""

from google.adk.tools._automatic_function_calling_util import (
    build_function_declaration,
)

# Case 1: simple types → primary path → required IS set
def simple_tool(
    query: str,
    mode: str = "default",
    verbose: bool = False,
) -> str:
    """A tool with simple parameter types."""
    return query

# Case 2: list[str] | None → fallback path → required is LOST
def complex_tool(
    query: str,
    mode: str = "default",
    tags: list[str] | None = None,
) -> str:
    """A tool where one param has a complex union type."""
    return query

decl_simple = build_function_declaration(simple_tool)
decl_complex = build_function_declaration(complex_tool)

print("Case 1 (simple types):", decl_simple.parameters.required)
# → ['query'] ✅

print("Case 2 (list[str] | None):", decl_complex.parameters.required)
# → None ❌  (expected ['query'])

Root Cause

In src/google/adk/tools/_automatic_function_calling_util.py, from_function_with_options():

  if parameters_properties:
    declaration.parameters = types.Schema(
        type='OBJECT',
        properties=parameters_properties,
    )
    declaration.parameters.required = (                    # ✅ set here
        _function_parameter_parse_util._get_required_fields(
            declaration.parameters
        )
    )
  elif parameters_json_schema:
    declaration.parameters = types.Schema(
        type='OBJECT',
        properties=parameters_json_schema,
    )
    # ❌ required is never set in this branch

Suggested Fix

Add the same _get_required_fields() call to the elif branch:

  elif parameters_json_schema:
    declaration.parameters = types.Schema(
        type='OBJECT',
        properties=parameters_json_schema,
    )
    declaration.parameters.required = (
        _function_parameter_parse_util._get_required_fields(
            declaration.parameters
        )
    )

Environment

  • google-adk version: 1.26.0
  • Python version: 3.12
  • OS: macOS

Activity

  1. added
    tools
    on Mar 12, 2026
  2. added theissue type on Mar 12, 2026
  3. added a commit that references this issue on Mar 12, 2026
    d60bd16
  4. surajksharma07 commented on Mar 13, 2026

    @surajksharma07
    Collaborator

    @aperepel the elif parameters_json_schema branch in from_function_with_options() never sets required, so mandatory parameters are silently dropped from the schema.

    As a workaround, you can determine required fields directly from the function signature (params with no default and non-Optional annotation) and assign them to declaration.parameters.required in that branch — this correctly yields ['query'] for your complex_tool case.

    One note worth checking before the PR lands: calling _get_required_fields() on the fallback-path Schema objects may not behave as expected, since _generate_json_schema_for_parameter captures only the type annotation (not defaults), so the resulting types.Schema objects won't carry default-value info that _get_required_fields relies on.

    Could you verify whether the signature-based approach resolves the issue on your end before finalizing the PR?

  5. added
    needs review
    on Mar 26, 2026
  6. surajksharma07 commented on Mar 26, 2026

    @surajksharma07
    Collaborator

    @xuanyang15 Please have a look into it once.

  7. added a commit that references this issue on Mar 30, 2026
    77329cc
  8. added 2 commits that reference this issue on Apr 8, 2026
    17bc5b4
    5ca1849
  9. added a commit that references this issue on Apr 14, 2026
    9b9faa4
  10. added a commit that references this issue on Apr 21, 2026
    eb0fcb4
  11. added a commit that references this issue on Aug 11, 2026
    1724601
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

needs reviewtools

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions