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

before_agent_callback is not working as expected

Open jasveer-os opened this issue 1 year ago • 3 comments

A simple agent with defined 'before_agent_callback' which is returning 'None' is terminating the session.

Expected as per docs is that returning None should not bypass LLM and work as usual.

Code:

before_agent_callback:

def log_agent_timing(callback_context: CallbackContext) -> Optional[types.Content]:
    agent_name = callback_context.agent_name
    reqId = callback_context.state[REQUEST_ID]
    print(f"{reqId} [Callback] Before Agent call for agent: {agent_name}")
    events = callback_context.state["events"]
    events.append({"event-name": "before-agent-event", "time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "name": agent_name})
    callback_context.state["events"] = events
    return None

Let me know what else detail is required.

DETAILS OS: Mac ADK version: 1.3.0 Python version: 3.10

jasveer-os avatar Jun 22 '25 11:06 jasveer-os

We are also experiencing the same issue. @jasveer-os if you don't set the state prior to the return, does it continue?

cp2boston avatar Jun 27 '25 15:06 cp2boston

Yes @cp2boston if I don't set state, it works.

jasveer-os avatar Jul 02 '25 07:07 jasveer-os

Thanks for the report. I tried to reproduce on multiple versions and couldn’t trigger a termination when before_agent_callback mutates state and returns None. Versions tested: v1.3.0, v1.10.0, v1.11.0 Environment: macOS, Python 3.13 (ADK supports 3.9+). Behavior is driven by the logic, so the version changes here shouldn’t matter. What happened for me: the callback’s state mutation produces a first event with a state_delta and no content, and then the agent proceeds to generate the normal LLM response as a second event. No termination.

A few thoughts that might explain what you’re seeing: The first event comes out after before_agent_callback can be a pure state-delta event (no content). If your consumer stops at the first yielded event or interprets “no content” as completion, it could look like termination. Consuming the full generator should yield the normal LLM response next. In core, ctx.end_invocation is only set when the callback returns a non-None types.Content (i.e., an override). Returning None with a state mutation should not stop the agent; it just emits a state-delta event before proceeding.

I am closing because does not look like it is reproducible on latest ADK version, but please re-open if it is still in your environment or integration.

GWeale avatar Aug 14 '25 23:08 GWeale