Following Loop Agent document,raise KeyError: 'Context variable not found: `initial_topic`.'
Hi, I'm totally new to ADK. When I follow the Loop Agent document, I got the error below:
KeyError: 'Context variable not found: `initial_topic`.'
Steps to reproduce:
-
mkdir loop_agent, prepare everything - pasting the code in the document to
agent.py - ran
adk run loop_agent - input any sentence
- The error above raised
How can I fix it? Can anyone help us? thanks
The problem is that the "initial_topic" is a key referenced in the initial_writer_agent instructions, but as seen in the documentation for state, the state values have to be initialized before reference in agent instructions: "Key Existence: Ensure that the key you reference in the instruction string exists in the session.state. If the key is missing, the agent will throw an error. To use a key that may or may not be present, you can include a question mark (?) after the key (e.g. {topic?})." Session State,
So to fix the problem you first have to include a default value for initial_topic in your session.state, you can do that in many ways such as when you initialize the session, or with a before_agent_callback.
I had the same problem at first and do think the documentation should include the state init aswell.
It's a documentation issue.