Skip to content

fix: resolve context parameter detection for deferred annotations#4755

Open
gautamvarmadatla wants to merge 1 commit intogoogle:mainfrom
gautamvarmadatla:fix/find-context-parameter-deferred-annotations
Open

fix: resolve context parameter detection for deferred annotations#4755
gautamvarmadatla wants to merge 1 commit intogoogle:mainfrom
gautamvarmadatla:fix/find-context-parameter-deferred-annotations

Conversation

@gautamvarmadatla
Copy link

Closes: #4754

Added a get_type_hints() call inside find_context_parameter() to resolve deferred string annotations back to real types before performing the Context identity check. This mirrors what from_function_with_options() already does in ADK. Then (NameError, TypeError, AttributeError) are caught and fall back to raw param.annotation so mocks, C extensions, and unresolvable forward refs still work correctly.

Testing Plan

Added unit tests covering deferred annotation handling for both find_context_parameter() directly and end-to-end through FunctionTool .

  • test_find_context_parameter_detects_custom_name_with_future_annotations
    • Verifies find_context_parameter() detects a custom-named ToolContext parameter when using from __future__ import annotations
  • test_find_context_parameter_optional_ctx_with_future_annotations
    • Verifies Optional[ToolContext] is correctly detected under deferred annotations
  • test_function_tool_excludes_custom_context_param_with_future_annotations
    • End-to-end test verifying FunctionTool._get_declaration() excludes the context parameter from the generated schema and keeps only query

Manual reproduction

from __future__ import annotations
from google.adk.tools.function_tool import FunctionTool
from google.adk.tools.tool_context import ToolContext

def my_tool(query: str, ctx: ToolContext) -> str:
    """A tool with a custom-named context parameter."""
    return f"Result for {query}"

tool = FunctionTool(my_tool)
print(tool._get_declaration())

Before the fix:

  • Raises ValueError: ToolContext parameter must be named as tool_context

After the fix:

  • Prints the schema with only query in the parameters

Validation

  • 21 passed in 2.45s

Unit Tests:

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

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.

@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 addresses a critical issue where FunctionTool failed to correctly identify and process context parameters when Python's from __future__ import annotations was enabled. The fix ensures that type hints are properly resolved at runtime, allowing the tool to accurately detect context parameters and generate correct function schemas, thereby improving the robustness and compatibility of the ADK with modern Python type hinting practices.

Highlights

  • Deferred Annotation Resolution: Implemented get_type_hints() within find_context_parameter() to correctly resolve deferred string annotations for context parameters, ensuring proper identification when from __future__ import annotations is used.
  • Robust Error Handling: Added error handling for NameError, TypeError, and AttributeError during type hint resolution, allowing the system to gracefully fall back to raw parameter annotations for mocks, C extensions, and unresolvable forward references.
  • Comprehensive Testing: Introduced new unit tests to cover various scenarios of deferred annotation handling, including custom-named context parameters, optional context parameters, and end-to-end FunctionTool schema generation.

🧠 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/utils/context_utils.py
    • Imported get_type_hints from the typing module.
    • Modified find_context_parameter to use get_type_hints for resolving annotations, wrapping the call in a try-except block to handle potential errors during resolution.
    • Updated the logic to retrieve the parameter annotation from the resolved hints, falling back to the raw annotation if resolution fails.
  • tests/unittests/tools/test_function_tool_with_import_annotations.py
    • Added necessary imports for Optional, FunctionTool, ToolContext, and find_context_parameter.
    • Introduced test_find_context_parameter_detects_custom_name_with_future_annotations to verify context parameter detection with deferred annotations and custom names.
    • Added test_find_context_parameter_optional_ctx_with_future_annotations to ensure correct detection of optional context parameters under deferred annotations.
    • Implemented test_function_tool_excludes_custom_context_param_with_future_annotations as an end-to-end test to confirm the context parameter is excluded from the generated function schema.
Activity
  • No human activity has been recorded on this pull request yet.
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 7, 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 effectively resolves an issue with detecting context parameters when using deferred annotations (from __future__ import annotations). The change in find_context_parameter to use get_type_hints is a correct and robust solution, including thoughtful error handling for cases where type hints cannot be resolved. The newly added unit tests are comprehensive, covering both the direct function change and the end-to-end behavior within FunctionTool, ensuring the fix is effective. The implementation is clean and well-tested.

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.

find_context_parameter fails to detect context-typed parameters with postponed annotations

2 participants