Add initial state in Agent declaration so that the instruction to the agent can use state variable injection
Problem description In current ADK agent implementation, if we want to inject some initial states that are valid before the first agent of the app is really running, we can either create a session with the initial state or implement _async_run_impl with the initial state set before running the subagents. Otherwise, when we reference the state variable in the first agent's instruction, we will receive an error due to invalid state variable.
The first approach requires to create the session, making "adk run/web" impossible. The second approach requires to write custom agent, making it unnecessarily complex with boilerplate code.
I am developing a project with a LoopAgent as the first agent, whose two subagents need coordinate through state variables. Both of their instructions need access to the state variables for the collaboration.
Describe the solution you'd like It would be easier to pass some initial states to the agent declaration directly like Agent( init_state= ...) or Agent( input_key= ...). The state values are merged to the session's state when the agent object is created. Since the initial states are needed by all the instances of the agent anyway, it makes sense to declare them in association with the Agent declaration. Although those states are still managed by a session at runtime, they are actually common to the Agent class. One big advantage is, we can inject the state variable to the instruction of the first agent of the app, without any extra boilerplate code and we can run the agent with adk run/web.
Actually since Agent declaration already has output_key for state variable - as the output state of the agent, it makes sense to introduce an input_key or init_state to the declaration as the input state(s).
Describe alternatives you've considered Current solutions are mentioned above in the problem description.
Hi @Barronli , thanks for reporting!
An workaround that is similar to what you want is using before_agent_callback, where you check and initialize the needed state if they are absent.
Thanks, @Jacksunwei! Yes before_agent_callback is a valid workaround. The other workaround (that I am using for one of the agents in my project) is to introduce another agent (the pre-loop agent) before the loop agent. The pre-loop agent virtually just helps provide the loop agent the initial state variable through its output_key. It runs only once in an invocation. (This is a typical loop coding pattern to init some loop-carried variables before the loop body. )
The case here probably is how to provide a way for an agent to reference some state variable(s) assigned by a later declared agent (in output_key or somewhere else) in the same loop. Then the loop workflow can easily form a semantically complete loop.
@Barronli Thank you for reporting this. I understand the request is currently under review. Please keep me posted if anything else is needed. Thanks for your patience.
@Jacksunwei can you provide an example of a before_agent_callback function that retrieves the user input, and saves it in the agent's state?
@reyolivo here's an example function that you can invoke on an agent that will add or append a callback which will look for environment variables that you've defined (e.g., when invoking adk run or adk web) and will take the value of those env vars and stick them into session state:
from google.adk.agents import Agent
from google.adk.agents.callback_context import CallbackContext
from os import getenv
def add_session_state_initialization_callback(agent: Agent) -> Agent:
"""
Add a callback to initialize the session state
for the agent.
Args:
agent (Agent): The agent to which the callback will be added.
Returns:
Agent: The agent with the added callback.
"""
# Define the callback function that will initialize the session state
def initialize_session_state(callback_context: CallbackContext) -> None:
"""
Initialize the session state with information from environment variables.
Args:
callback_context (CallbackContext): The callback context for the agent.
"""
for attribute in [
"MY_FIRST_STATE_VARIABLE",
"MY_SECOND_STATE_VARIABLE",
# Add additional state variable names as needed
]:
# Set the session state attribute from environment variable or default to None
callback_context.state[attribute] = getenv(attribute, None)
# Now set the callback, accounting for different existing callback configurations
current_callback_value = agent.before_agent_callback
if current_callback_value is None:
# Set the callback
agent.before_agent_callback = initialize_session_state
elif isinstance(current_callback_value, list):
# Add the new instance of the callback to the existing list of callbacks
current_callback_value.append(initialize_session_state)
agent.before_agent_callback = current_callback_value
else:
# Replace the single existing callback with a list containing both the existing and new callback
agent.before_agent_callback = [
current_callback_value,
initialize_session_state,
]
return agent