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

Plugin framework: on_state_change_callback is never invoked

Open mportdata opened this issue 9 months ago • 7 comments

Description

The plugin framework provides hooks for various lifecycle events (before/after agent, tool, model callbacks, etc.), but there is no mechanism to notify plugins when session state changes occur via event.actions.state_delta.

The BigQueryAgentAnalyticsPlugin already implements an on_state_change_callback() method, demonstrating real demand for this hook, but the framework never invokes it because:

  1. BasePlugin has no default on_state_change_callback method
  2. PluginManager has no dispatcher for it
  3. The runner never detects state deltas and triggers the callback

Expected Behavior

When an event with a non-empty state_delta is yielded from the runner, all registered plugins should have their on_state_change_callback invoked with the state delta (as a copy, to prevent mutation).

Proposed Solution

  1. Add on_state_change_callback to BasePlugin as a default no-op method
  2. Add "on_state_change_callback" to PluginCallbackName and a run_on_state_change_callback dispatcher to PluginManager
  3. In Runner._exec_with_plugin, after yielding the final event, check for state_delta and invoke the callback

The callback should be observational (return value ignored) and receive a copy of the state delta to prevent plugins from mutating event state.

mportdata avatar Feb 06 '26 12:02 mportdata

Fix awaiting review here: https://github.com/google/adk-python/pull/4395

mportdata avatar Feb 09 '26 20:02 mportdata

@haiyuan-eng-google please take a look.

wuliang229 avatar Feb 09 '26 23:02 wuliang229

Confirmation

You are absolutely correct. We have verified that:

  1. BigQueryAgentAnalyticsPlugin implements on_state_change_callback, but it is effectively dead code.
  2. BasePlugin and PluginManager do not define or support this callback.
  3. The Runner does not dispatch any specific callback when yielding events with state_delta.

This is a clear gap in the plugin lifecycle hooks. While on_event_callback exists, a dedicated on_state_change_callback is cleaner and semantically more appropriate for state mutations, especially given that BigQueryAgentAnalyticsPlugin already expects it.

Plan

  1. Framework Update: Add on_state_change_callback to BasePlugin (no-op default) and PluginManager.
  2. Runner Integration: Update Runner._exec_with_plugin (and potentially _append_new_message_to_session) to invoke this callback whenever an event contains a non-empty state_delta.
  3. Safety: Ensure the callback receives a copy of the delta to prevent accidental mutation of the event itself.

haiyuan-eng-google avatar Feb 10 '26 00:02 haiyuan-eng-google

Confirmation

You are absolutely correct. We have verified that:

  1. BigQueryAgentAnalyticsPlugin implements on_state_change_callback, but it is effectively dead code.
  2. BasePlugin and PluginManager do not define or support this callback.
  3. The Runner does not dispatch any specific callback when yielding events with state_delta.

This is a clear gap in the plugin lifecycle hooks. While on_event_callback exists, a dedicated on_state_change_callback is cleaner and semantically more appropriate for state mutations, especially given that BigQueryAgentAnalyticsPlugin already expects it.

Plan

  1. Framework Update: Add on_state_change_callback to BasePlugin (no-op default) and PluginManager.
  2. Runner Integration: Update Runner._exec_with_plugin (and potentially _append_new_message_to_session) to invoke this callback whenever an event contains a non-empty state_delta.
  3. Safety: Ensure the callback receives a copy of the delta to prevent accidental mutation of the event itself.

I have raised a fix here: https://github.com/google/adk-python/pull/4395

This has added an on_state_change_callback no-op method to the BasePlugin and PluginManager as described above. STATE_DELTA was already being logged as part of after_tool_callback, my fix includes removing this implementation to not cause duplicate STATE_DELTA logs.

RE: _append_new_message_to_session I have now added a fix so that _append_new_message_to_session calls the appropriate plug-in method when state_delta is not none.

RE: Safety: please let me know if this has not been addressed by my fix already. In my fix we call on_state_change_callback within Runner._exec_with_plugin when any event has a non-empty actions.state_delta.

mportdata avatar Feb 10 '26 07:02 mportdata

@haiyuan-eng-google FYI, the ADK team doesn't want to add a on_state_change_callback to the framework. Is there a way to fix it without introducing a framework change?

wuliang229 avatar Feb 10 '26 17:02 wuliang229

This issue has been automatically marked as stale because it has not had recent activity for 7 days after a maintainer requested clarification. It will be closed if no further activity occurs within 7 days.

adk-bot avatar Feb 18 '26 06:02 adk-bot

The issue has been fixed in https://github.com/google/adk-python/commit/ea034877ec15eef1be8f9a4be9fcd95446a3dc21 with on_event_callback @wuliang229

haiyuan-eng-google avatar Feb 18 '26 20:02 haiyuan-eng-google