Skip to content

fix(tools): mark Optional parameters without a default as required - #7904

Open
lisry-1 wants to merge 1 commit into
google:mainfrom
lisry-1:fix/optional-param-without-default-required
Open

lisry-1 wants to merge 1 commit into
google:mainfrom
lisry-1:fix/optional-param-without-default-required

Conversation

@lisry-1

@lisry-1 lisry-1 commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

Summary

build_function_declaration computes required parameters via _get_required_fields, which decides requiredness with not schema.nullable. That conflates "nullable" (the value may be null) with "optional" (the argument may be omitted). As a result, an Optional[T] parameter without a default value (e.g. def f(x: str | None)) is wrongly dropped from the declaration's required list.

Problem

In Python, Optional[str] with no default is still a required argument (it just permits None). But:

  • FunctionTool._get_mandatory_args (function_tool.py:641) uses param.default == inspect.Parameter.empty, so it correctly treats such a parameter as mandatory;
  • the declaration's required list (via _get_required_fields) drops it.

The model is told the argument may be omitted, then the runtime rejects the call with "mandatory input parameters are not present" and forces an extra round-trip.

Root cause: types.Schema.default cannot distinguish "no default" from an explicit = None default (both leave default as None), and nullable only means "may be null", not "may be omitted".

Changes

In from_function_with_options, derive required directly from inspect.signature(func).parameters (param.default is inspect.Parameter.empty), matching FunctionTool._get_mandatory_args, instead of calling _get_required_fields.

Tests

Two new cases:

  • test_build_declaration_marks_optional_without_default_as_required: def f(name: str, nickname: str | None) -> required == ['name', 'nickname']
  • test_build_declaration_omits_optional_with_none_default: def f(name: str, nickname: str | None = None) -> required == ['name']

_get_required_fields used `not schema.nullable` to decide requiredness,
confusing "nullable" (value may be null) with "optional" (argument may be
omitted). An `Optional[T]` parameter without a default (e.g.
`def f(x: str | None)`) was therefore dropped from the declaration
required list, while FunctionTool._get_mandatory_args still treats it as
mandatory. This mismatch made the model omit the argument and force an
extra round-trip ("mandatory input parameters are not present").

Derive required from the callable signature instead, matching
FunctionTool._get_mandatory_args.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants