Skip to content

fix(tools): skip ignored params' annotations on the legacy declaration path - #7437

Closed
Akshaay1 wants to merge 1 commit into
google:mainfrom
Akshaay1:fix/legacy-decl-type-checking-context
Closed

Akshaay1 wants to merge 1 commit into
google:mainfrom
Akshaay1:fix/legacy-decl-type-checking-context

Conversation

@Akshaay1

@Akshaay1 Akshaay1 commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Link to Issue or Description of Change

Fixes #7436.

Problem:
With JSON_SCHEMA_FOR_FUNC_DECL disabled, build_function_declaration removes the ignored params (tool_context, input_stream) from the signature but copies every original annotation onto the stripped function. from_function_with_options then resolves all of them with get_type_hints. A context param typed with a TYPE_CHECKING-only import, the pattern from #6488, raises NameError even though it never reaches the schema.

Solution:
Copy only the kept params' annotations (and return) onto the stripped function. It's one statement and doesn't add an except NameError. The comment on that get_type_hints call says not catching NameError is deliberate, and this keeps it that way: a declared param whose annotation can't be resolved still raises a clear NameError, and a test now pins that.

The JSON-schema path already handles this case since 2ecb3fb, but nothing tested it (see #6488 (comment)). The new test is parametrized over both paths, so it pins that fix too.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Three new cases in tests/unittests/tools/test_function_tool_with_import_annotations.py. I checked that each one fails when the change it guards is reverted:

Test Fails when
test_type_checking_only_context_param_is_left_out_of_declaration[False] this PR's change is reverted (NameError: name 'ToolContext' is not defined)
test_type_checking_only_context_param_is_left_out_of_declaration[True] the NameError catch from 2ecb3fb in _get_function_fields is narrowed back to TypeError
test_type_checking_only_annotation_on_declared_param_still_raises the legacy path is "fixed" by catching NameError around get_type_hints instead
$ pytest tests/unittests/tools/test_function_tool_with_import_annotations.py
16 passed

$ pytest tests/unittests/tools tests/unittests/flows -n auto
3834 passed, 1 skipped

pre-commit run passed on the changed files (ruff, isort, pyink, codespell, compliance and docs checks).

Manual End-to-End (E2E) Tests:

An LlmAgent using the tool from #7436, with a scripted BaseLlm that calls it once, run through InMemoryRunner with ADK_DISABLE_JSON_SCHEMA_FOR_FUNC_DECL=1. On macOS, Python 3.12.13:

Before (main @ 77d4dcd):

  File ".../google/adk/tools/_automatic_function_calling_util.py", line 350, in from_function_with_options
NameError: name 'ToolContext' is not defined

After:

declared params: ['city']
tool result: {'result': 'Sunny in Paris'}
declared params: ['city']
final: Done.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

🤖 Generated with Claude Code

…n path

build_function_declaration drops the ignored params (tool_context,
input_stream) from the signature but copied every original annotation
onto the stripped function. from_function_with_options then resolves
them all with get_type_hints, so a context param typed with a
TYPE_CHECKING-only import raised NameError although it never reaches the
schema. Copy only the kept params' annotations.

NameError is still not caught, so a declared param whose annotation
cannot be resolved keeps its clear error.

Also pins the JSON-schema path fix from 2ecb3fb, which had no test.
@adk-bot

adk-bot commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

Thank you @Akshaay1 for your contribution! 🎉

Your changes have been successfully imported and merged via Copybara in commit 62745cc.

Closing this PR as the changes are now in the main branch.

@adk-bot adk-bot added the merged [Status] This PR is merged label Oct 13, 2026
@adk-bot adk-bot closed this Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged [Status] This PR is merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Legacy function declaration path raises NameError for a context param typed with a TYPE_CHECKING-only import

3 participants