`find_context_parameter()` fails to detect context-typed parameters when using `from __future__ import annotations`
Issue
find_context_parameter() in src/google/adk/_internal/tool_utils.py fails to detect context-typed parameters when postponed annotations are enabled via from __future__ import annotations. This breaks tool integration for any codebase using PEP 563 postponed evaluation.
Steps to Reproduce
- Define a tool function with postponed annotations:
from __future__ import annotations
from google.adk import Context
def my_tool(user_id: str, context: Context) -> str:
return f"User: {user_id}"
- Register the tool in an agent and execute it.
- The agent fails to extract the context parameter, raising an error about missing required context.
Root Cause
With postponed annotations, inspect.signature() returns string annotations rather than type objects. The current find_context_parameter() implementation compares annotations using is or == against the Context class, which fails when the annotation is the string "Context" instead of the class itself.
Why This Matters
- Postponed annotations are increasingly common in production codebases (PEP 563, Python 3.7+).
- The
from __future__ import annotationsimport is recommended in Python 3.10+ style guides to improve performance and enable forward references. - Tools fail silently or with cryptic errors, making debugging difficult for users who follow modern Python practices.
Expected Behavior
find_context_parameter() should detect context parameters regardless of whether annotations are evaluated eagerly or postponed. For postponed annotations, it should resolve string type hints back to the actual Context class before comparison.
What Good Looks Like
The function should:
- Check if the annotation is already the
Contextclass (current behavior). - If the annotation is a string, attempt to resolve it using
typing.get_type_hints()with the function's module globals. - Compare the resolved type against
Context.
Reference: typing.get_type_hints() handles postponed annotations automatically and returns the actual types.
Proposed Fix Location
src/google/adk/_internal/tool_utils.py — find_context_parameter() function
Modify the parameter type check to handle both class references and string annotations by calling typing.get_type_hints() on the function signature before comparison.
Contributed by Klement Gunndu
@KlementMultiverse The issue is in find_context_parameter() (src/google/adk/utils/context_utils.py) — with from __future__ import annotations, inspect.signature() returns string annotations, so the annotation is Context identity check in _is_context_type() always fails.
As a workaround, you can resolve annotations first via typing.get_type_hints(func) and use those resolved types before falling back to param.annotation — could you try that and confirm if it fixes things on your end?
Withdrawing this issue. Apologies for any inconvenience.