Skip to content

LlmAgent output_key is not written to session state when before_agent_callback short-circuits execution #4837

Description

@gautamvarmadatla

Describe the Bug:
When LlmAgent uses both output_key and before_agent_callback, a types.Content returned from the callback correctly short-circuits execution and is shown to the user, but session.state[output_key] is never updated. This would silently break downstream agents, tools, or prompt templates that rely on output_key , since the UI shows the correct response while the backend state remains stale or empty.

Steps to Reproduce:
We can reproduce using this repro code

fromgoogle.adk.agentsimportLlmAgentfromgoogle.adk.agents.callback_contextimportCallbackContextfromgoogle.adk.runnersimportInMemoryRunnerfromgoogle.genaiimporttypesdefcache_callback(callback_context: CallbackContext):
returntypes.Content(parts=[types.Part(text="cached answer")])
agent=LlmAgent(
name="my_agent",
model="gemini-2.0-flash",
instruction="Answer questions.",
output_key="result",
before_agent_callback=cache_callback,
)
runner=InMemoryRunner(agent=agent, app_name="test")
session=awaitrunner.session_service.create_session(app_name="test", user_id="user1")
asyncfor_inrunner.run_async(
user_id="user1", session_id=session.id,
new_message=types.Content(parts=[types.Part(text="hello")]),
):
passsession=awaitrunner.session_service.get_session(app_name="test", user_id="user1", session_id=session.id)
print(session.state.get("result")) 

Expected Behavior:
session.state["result"] should equal "cached answer", the same text the callback returned to the user.

Observed Behavior:
session.state.get("result") returns None. No error or warning is raised.

Environment Details:

  • ADK Library Version (pip show google-adk): 1.27.1
  • Desktop OS: Windows
  • Python Version (python -V): 3.12.12

How often has this issue occurred?:

  • Always (100%)

Metadata

Metadata

Assignees

Labels

core[Component] This issue is related to the core interface and implementationneeds review[Status] The PR/issue is awaiting review from the maintainer

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions