Skip to content

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

@gautamvarmadatla

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

  from google.adk.agents import LlmAgent
  from google.adk.agents.callback_context import CallbackContext
  from google.adk.runners import InMemoryRunner
  from google.genai import types

  def cache_callback(callback_context: CallbackContext):
      return types.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 = await runner.session_service.create_session(app_name="test", user_id="user1")
  async for _ in runner.run_async(
      user_id="user1", session_id=session.id,
      new_message=types.Content(parts=[types.Part(text="hello")]),
  ):
      pass
  session = await runner.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 implementation

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions