I need a simple example of accessing session state values in Agent Engine Code Executor #3547
Replies: 1 comment 2 replies
-
|
Response from ADK Answering Agent (experimental, answer may be inaccurate) TLDR: To use session state values in the Agent Engine Code Executor, you should reference the state variables directly in your agent's instructions. The ADK framework will inject the values into the prompt, allowing the LLM to generate code that includes this data. Hello! You've asked for an example of how to access session state values within the Code Executor. While the executor environment supports statefulness, the primary way to pass data from the ADK session state to the code being executed is through the agent's prompt. Here’s the general pattern:
Conceptual Example Let's say you have a value (e.g., a list of numbers) in your session state under the key # In a previous step, a tool or agent has saved data to the state:
# tool_context.state["my_data"] = [10, 20, 30, 40, 50]Now, you can configure your code-executing agent to use this data: from google.adk.agents import LlmAgent
from google.adk.code_executors import AgentEngineCodeExecutor
code_analyst_agent = LlmAgent(
llm=...,
code_executor=AgentEngineCodeExecutor(),
instructions=[
"You are a data analyst.",
"The user has provided the following data: {my_data}.",
"Write and execute Python code to calculate the sum of this data and print the result."
]
)
# When you run this agent, the LLM will receive instructions like:
# "The user has provided the following data: [10, 20, 30, 40, 50].
# Write and execute Python code to calculate the sum of this data and print the result."
# The LLM would then generate and execute the following code:
# print(sum([10, 20, 30, 40, 50]))Regarding your question about sending a file, while the internal context handling in the ADK does have references to file processing [4], the recommended and most straightforward approach is to pass the content of the data through the session state as shown above, rather than passing a file path. The code execution environment in Agent Engine is designed to be stateful, meaning variables and file contents from one code execution step are available in the next, but the initial data is best provided through the agent's prompt. I hope this clarifies the process! Let me know if you have more questions. [1] Custom Tools Documentation |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have been having trouble trying to send session state values to the coding execution environment. I know it supports sending a file but I don't see any examples of how I can do this.
Beta Was this translation helpful? Give feedback.
All reactions