adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

Add Lifecycle Error Callbacks (`on_agent_error`, `on_run_error`) to ADK Framework

Open evekhm opened this issue 8 months ago • 3 comments

🔴 Required Information

Is your feature request related to a specific problem?

Yes. Currently, the ADK BasePlugin architecture does not report errors at the agent or invocation (run) level. The framework supports on_model_error_callback and on_tool_error_callback, but if an unhandled exception occurs that crashes the agent process (e.g. fatal tool error, model crashing out of the runner.run_async() loop), the after_agent_callback and after_run_callback bindings are skipped. Consequently, the AGENT_COMPLETED and INVOCATION_COMPLETED events are never emitted to observability sinks (like the BigQueryAgentAnalyticsPlugin). Because these failed runs disappear from the denominator in standard reports, systems will display artificially perfect durations and artificially high success rates (as the failures are never formally registered).

Describe the Solution You'd Like

The ADK BasePlugin should introduce new dedicated lifecycle error hooks:

  • on_agent_error_callback(agent: Agent, error: Exception, ...)
  • on_run_error_callback(invocation_id: str, error: Exception, ...) The BigQueryAgentAnalyticsPlugin (and other telemetry plugins) should implement these to formally emit AGENT_ERROR and INVOCATION_ERROR event types into the downstream datastore immediately prior to the process dying.

Impact on your work

Without these hooks, teams adopting ADK tooling for reliable enterprise deployment cannot accurately measure their agentic pipeline's systemic failure rate. Observability platforms must rely on complicated post-processing state engines rather than simple streaming event aggregations. This is critical for enterprise observability.

🟡 Recommended Information

Describe Alternatives You've Considered

  1. Time-Based Heuristic (Current workaround): Implementing custom BigQuery views that enforce a timeout logic. If a STARTING event hasn't received a corresponding COMPLETED event within that window, we manually flag it as an ERROR. This risks false positives for exceptionally long-running agents, and forces downstream dashboarding metrics to accommodate missing data rather than proper upstream telemetry.

evekhm avatar Mar 09 '26 23:03 evekhm

@evekhm Thanks for the update. The request is currently being reviewed, and I will let you know if anything further is needed. Thank you for understanding.

klateefa avatar Mar 12 '26 06:03 klateefa

Related: #5282 reports that on Workflow(BaseNode) roots in 2.0.0a3, the success-path after_run_callback / after_agent_callback / after_model_callback / on_event_callback also don't fire — not just error paths. _run_node_async (the BaseNode dispatch path) currently calls only before_run_callback and on_user_message_callback; the rest are behind a # TODO: Add tracing and plugin lifecycle for the node runtime path at runners.py:427. So the observability gap described here (AGENT_COMPLETED/INVOCATION_COMPLETED events missing from sinks) currently affects every successful invocation on a Workflow root, not only the crashed ones. Worth considering both the error-path hooks proposed here and the success-path dispatch completeness in #5282 together — a BigQueryAgentAnalyticsPlugin on a Workflow root today sees zero completions, not just zero errors.

surfai avatar Apr 11 '26 08:04 surfai

This feature has been implemented.

BasePlugin now includes dedicated error callbacks (on_agent_error_callback and on_run_error_callback), which are dispatched by PluginManager when unhandled errors escape agent or runner execution. Additionally, BigQueryAgentAnalyticsPlugin implements both callbacks to emit AGENT_ERROR and INVOCATION_ERROR events to BigQuery.

Added in commit https://github.com/google/adk-python/commit/7d0ae63ab13ad68dc1558a3a2c40c18baa531386. Closing this issue.

xuanyang15 avatar Jul 28 '26 20:07 xuanyang15