Skip to content

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

Closed

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

Activity

  1. added
    tools[Component] This issue is related to tools
    on Mar 7, 2026
  2. added theissue type on Mar 7, 2026
  3. surajksharma07 commented on Mar 8, 2026

    @surajksharma07
    Collaborator

    @gautamvarmadatla Thanks for the detailed report and for already putting up PR #4755 — the root cause is spot on: find_context_parameter() uses an identity check (annotation is Context) that breaks when from __future__ import annotations turns annotations into strings.

    As an immediate workaround until the PR lands, renaming the context parameter to tool_context will unblock you since FunctionTool falls back to that name — could you confirm if that works for your case in the meantime?

    Once you've validated the fix in PR #4755 holds up across your use cases, it'd be great to get that reviewed and merged.

    Team is looking more into it.

  4. gautamvarmadatla commented on Mar 8, 2026

    @gautamvarmadatla
    ContributorAuthor

    hi @surajksharma07, thanks so much for the prompt response.
    I’ve been testing locally with the same patch as in #4755, and it seems to be working on my end.

  5. surajksharma07 commented on Mar 8, 2026

    @surajksharma07
    Collaborator

    @gautamvarmadatla Good to hear the patch in #4755 is working on your end — that confirms the fix is on the right track.

    We'll move ahead with the PR review; once it's been looked at on our side, we'll get it merged in.

    Team is looking more into it.

    Thanks!

  6. surajksharma07 commented on Mar 8, 2026

    @surajksharma07
    Collaborator

    @xuanyang15 Please have a look into it.

  7. removed their assignment
    on Mar 26, 2026
  8. xuanyang15 commented on Mar 26, 2026

    @xuanyang15
    Collaborator

    Covered by 22fc332

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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