Skip to content

ReadonlyContext.agent_name raises AttributeError when _invocation_context.agent is None #6063

Description

@lmandrelli

Environment

  • Version: google-adk>=2.1.0 (tested with 2.1.0 and 2.2.0)
  • Python: 3.14
  • OS: Linux (Docker container)

The Issue

I migrated my project from sequential agents to a workflow using ADK 2.0, specifically with deterministic nodes.

After this migration, I started seeing the following error in my logs:

google_adk.google.adk.plugins.bigquery_agent_analytics_plugin - ERROR - BigQuery analytics plugin error in on_event_callback; skipping.
Traceback (most recent call last):
  File "/app/.venv/lib/python3.14/site-packages/google/adk/plugins/bigquery_agent_analytics_plugin.py", line 121, in wrapper
    return await func(self, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.14/site-packages/google/adk/plugins/bigquery_agent_analytics_plugin.py", line 3069, in on_event_callback
    await self._log_event(
    ...<5 lines>...
    )
  File "/app/.venv/lib/python3.14/site-packages/google/adk/plugins/bigquery_agent_analytics_plugin.py", line 2963, in _log_event
    "agent": callback_context.agent_name,
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.14/site-packages/google/adk/agents/readonly_context.py", line 52, in agent_name
    return self._invocation_context.agent.name
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'name'

Impact

While the BigQueryAgentAnalyticsPlugin wraps its callbacks in @_safe_callback (which catches and logs this as a non-fatal warning), the exception prevents the event from being properly logged to BigQuery. The event is silently dropped instead of being written with a fallback agent name.

This is a data-loss issue for anyone relying on the BigQuery analytics plugin.

Steps to Reproduce

  1. Create an ADK workflow using deterministic nodes (ADK 2.0+).
  2. Enable the BigQueryAgentAnalyticsPlugin.
  3. Run the workflow.
  4. Observe the AttributeError in the logs and the missing BigQuery rows.

Bug Description

Under google-adk, ReadonlyContext (and subclasses like CallbackContext) defines the agent_name property as:

@property
def agent_name(self) -> str:
    return self._invocation_context.agent.name

However, _invocation_context.agent is typed as Optional and can legitimately be None (e.g. during certain initialization/teardown phases of the Runner, or when driven by a workflow engine). When agent is None, this raises:

AttributeError: 'NoneType' object has no attribute 'name'

Expected Behavior

ReadonlyContext.agent_name should gracefully handle the case where agent is None, returning a fallback value such as "unknown" or None (typed as Optional[str]), rather than raising an unhandled AttributeError.


Temporary Workaround

This is the monkeypatch I applied in my project to work around this issue:

@property
def _safe_agent_name(self) -> str:
    """Safe wrapper for agent_name to prevent AttributeError when agent is None."""
    try:
        agent = self._invocation_context.agent
        return agent.name if agent is not None else "unknown"
    except AttributeError:
        return "unknown"

setattr(ReadonlyContext, "agent_name", _safe_agent_name)

Metadata

Metadata

Labels

core[Component] This issue is related to the core interface and implementation

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions