Context not persisting #3204
Replies: 1 comment 2 replies
-
|
Response from ADK Answering Agent (experimental, answer may be inaccurate) TLDR: Your state update is not being persisted because it occurs after the last Hi @riturajtiwari, I can see the issue you're running into. The problem lies in the timing of your state modification relative to when events are yielded. The ExplanationIn ADK, changes made to the session state (e.g., In your # All events from the sub-agent have been yielded in this loop
async for event in active_agent.run_async(ctx):
# ...
yield event
# The loop is now finished.
# You modify the state here...
if completion_signal_found:
new_index = current_agent_index + 1
ctx.session.state["current_agent_index"] = new_index # This change is only local for now
# ...Because no The SolutionTo ensure your state change is saved, you need to Here is the corrected version of the end of your # ... (The async for loop is the same as before) ...
async for event in active_agent.run_async(ctx):
if not event.content or not event.content.parts:
continue
# Check every event for the completion signal.
text = "".join(p.text for p in event.content.parts if p.text)
completion_signal = active_agent.name + "_complete"
if completion_signal in text:
logger.info(
"[WORKFLOW] Completion signal '%s' found in an event.",
completion_signal,
)
completion_signal_found = True
# Yield all events from the sub-agent.
yield event
# After the sub-agent's turn is fully complete, check if we should advance.
if completion_signal_found:
new_index = current_agent_index + 1
ctx.session.state["current_agent_index"] = new_index
ctx.session.state[self.output_key] = f"Advanced to step {new_index}."
logger.info(
"[WORKFLOW] Advancing to next agent. New index: %s",
new_index,
)
# FIX: Yield a final event to commit the state change.
yield Event(
author=self.name,
content=f"Workflow advanced to agent index {new_index}."
)
else:
logger.info(
"[WORKFLOW] Did not advance. Completion signal not found in any event this turn."
)
# It's good practice to yield a final event here as well.
yield Event(
author=self.name,
content="Workflow step complete. Awaiting next user input."
)By adding this final Let me know if you have any other questions! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This is a follow up to my earlier issue
I have created a custom agent that runs a sequence of several multi-turn conversational agents. My orchestrator looks like this:
When I run this code, my first agent completes. I see this getting recorded. However, on the next turn, this context entry is lost and we are back to the square one. What am I missing?
Beta Was this translation helpful? Give feedback.
All reactions