Skip to content

fix: temp-scoped state now visible to subsequent agents in same invocation#4618

Open
stakeswky wants to merge 1 commit intogoogle:mainfrom
stakeswky:fix/temp-state-output-key
Open

fix: temp-scoped state now visible to subsequent agents in same invocation#4618
stakeswky wants to merge 1 commit intogoogle:mainfrom
stakeswky:fix/temp-state-output-key

Conversation

@stakeswky
Copy link

Summary

Fixes #4564

When using output_key with a temp: prefix (e.g. output_key='temp:result') in a SequentialAgent, the output was silently lost. Agent-2 could never read the temp state written by agent-1.

Root Cause

Two issues in append_event:

  1. _trim_temp_delta_state() removed temp keys from the event delta before _update_session_state() could apply them to the in-memory session
  2. _update_session_state() also explicitly skipped temp:-prefixed keys
# Before (broken ordering):
async def append_event(self, session, event):
    event = self._trim_temp_delta_state(event)   # temp keys gone!
    self._update_session_state(session, event)    # nothing to apply

Fix

Introduce _apply_temp_state() which writes temp-scoped keys to the in-memory session.state before the event delta is trimmed:

# After:
async def append_event(self, session, event):
    self._apply_temp_state(session, event)        # temp keys → session.state
    event = self._trim_temp_delta_state(event)    # temp keys removed from delta
    self._update_session_state(session, event)    # non-temp keys applied

This ensures:

  • ✅ Temp state is available to subsequent agents within the same invocation
  • ✅ Temp state is still stripped from event deltas (not persisted to storage)
  • ✅ All three session services (InMemory, Database, SQLite) behave consistently

Files Changed

  • src/google/adk/sessions/base_session_service.py: Added _apply_temp_state(), reordered append_event logic, removed temp-skip in _update_session_state
  • src/google/adk/sessions/database_session_service.py: Added _apply_temp_state() call before trim
  • src/google/adk/sessions/sqlite_session_service.py: Added _apply_temp_state() call before trim
  • tests/unittests/sessions/test_session_service.py: Updated existing test + added new test for sequential agent scenario

Testing

All 67 session service tests pass across InMemory, Database, and SQLite backends.

…ation

When using output_key with a temp: prefix (e.g. output_key='temp:result')
in a SequentialAgent, the output was lost because:

1. _trim_temp_delta_state removed temp keys from the event delta BEFORE
   _update_session_state could apply them to the in-memory session
2. _update_session_state also explicitly skipped temp-prefixed keys

This meant agent-2 in a sequential pipeline could never read temp state
written by agent-1's output_key.

Fix: introduce _apply_temp_state() which writes temp-scoped keys to the
in-memory session.state BEFORE the event delta is trimmed. This ensures:
- Temp state is available to subsequent agents within the same invocation
- Temp state is still stripped from event deltas (not persisted to storage)
- All three session services (InMemory, Database, SQLite) behave consistently

Fixes google#4564
@gemini-code-assist
Copy link
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@google-cla
Copy link

google-cla bot commented Feb 25, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@adk-bot adk-bot added the services [Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc label Feb 25, 2026
@adk-bot
Copy link
Collaborator

adk-bot commented Feb 25, 2026

Response from ADK Triaging Agent

Hello @stakeswky, thank you for creating this PR!

It looks like the Contributor License Agreement (CLA) check has failed. Before we can merge this PR, you'll need to sign the CLA. You can do so at https://cla.developers.google.com/.

Thanks!

@stakeswky stakeswky force-pushed the fix/temp-state-output-key branch from 5a416ef to 65c59ed Compare February 25, 2026 04:16
@ryanaiagent ryanaiagent self-assigned this Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

services [Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Output key doesn't work with temp state scope

3 participants