Skip to content

find_context_parameter fails to detect context-typed parameters with postponed annotations #4754

@gautamvarmadatla

Description

@gautamvarmadatla

Describe the Bug:

If a tool function is defined in a module that uses from __future__ import annotations, ADK can fail to recognize Context / ToolContext parameters because find_context_parameter() looks at the raw annotation string instead of resolved type hints. When that happens, find_context_parameter() misses the real context parameter. FunctionTool then falls back to the literal name tool_context, so a custom-named context parameter like ctx is not excluded from declaration building and eventually triggers a misleading ValueError saying it must be named tool_context. This is easy to miss because a parameter already named tool_context happens to match the fallback and masks the bug.

Steps to Reproduce:

  1. Install google-adk from source (main branch)
  2. Create a file with the following code:
  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 that searches for something."""
      return f"Result for {query}"

  tool = FunctionTool(my_tool)
  print(tool._get_declaration())
  1. Run the file with uv run python my_tool.py

Expected Behavior:
The tool should be created successfully with ctx excluded from the schema, identical to the behavior without from __future__ import annotations

Observed Behavior:

  ValueError: `ToolContext` parameter must be named as `tool_context`. Found `ctx` instead in function `my_tool`.

Environment Details:

  • ADK Library Version (pip show google-adk): 1.26.0
  • Desktop OS: Windows 11
  • Python Version (python -V): 3.10.19

Metadata

Metadata

Assignees

No one assigned

    Labels

    tools[Component] This issue is related to tools

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions