Plugin framework: on_state_change_callback is never invoked
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:
BasePluginhas no defaulton_state_change_callbackmethodPluginManagerhas no dispatcher for it- 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
- Add
on_state_change_callbacktoBasePluginas a default no-op method - Add
"on_state_change_callback"toPluginCallbackNameand arun_on_state_change_callbackdispatcher toPluginManager - In
Runner._exec_with_plugin, after yielding the final event, check forstate_deltaand 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.
Fix awaiting review here: https://github.com/google/adk-python/pull/4395
@haiyuan-eng-google please take a look.
Confirmation
You are absolutely correct. We have verified that:
BigQueryAgentAnalyticsPluginimplementson_state_change_callback, but it is effectively dead code.BasePluginandPluginManagerdo not define or support this callback.- The
Runnerdoes not dispatch any specific callback when yielding events withstate_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
- Framework Update: Add
on_state_change_callbacktoBasePlugin(no-op default) andPluginManager. - Runner Integration: Update
Runner._exec_with_plugin(and potentially_append_new_message_to_session) to invoke this callback whenever an event contains a non-emptystate_delta. - Safety: Ensure the callback receives a copy of the delta to prevent accidental mutation of the event itself.
Confirmation
You are absolutely correct. We have verified that:
BigQueryAgentAnalyticsPluginimplementson_state_change_callback, but it is effectively dead code.BasePluginandPluginManagerdo not define or support this callback.- The
Runnerdoes not dispatch any specific callback when yielding events withstate_delta.This is a clear gap in the plugin lifecycle hooks. While
on_event_callbackexists, a dedicatedon_state_change_callbackis cleaner and semantically more appropriate for state mutations, especially given thatBigQueryAgentAnalyticsPluginalready expects it.Plan
- Framework Update: Add
on_state_change_callbacktoBasePlugin(no-op default) andPluginManager.- Runner Integration: Update
Runner._exec_with_plugin(and potentially_append_new_message_to_session) to invoke this callback whenever an event contains a non-emptystate_delta.- 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.
@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?
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.
The issue has been fixed in https://github.com/google/adk-python/commit/ea034877ec15eef1be8f9a4be9fcd95446a3dc21 with on_event_callback @wuliang229