Skip to content

Commit ee39a89

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: introduces a new AgentEngineSandboxCodeExecutor class that supports executes agent generated code
The AgentEngineSandboxCodeExecutor uses the Vertex AI Code Execution Sandbox API to execute code PiperOrigin-RevId: 821699641
1 parent d327538 commit ee39a89

File tree

8 files changed

+455
-3
lines changed

8 files changed

+455
-3
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# OAuth Sample
2+
3+
## Introduction
4+
5+
This sample data science agent uses Agent Engine Code Execution Sandbox to execute LLM generated code.
6+
7+
8+
## How to use
9+
10+
* 1. Follow https://cloud.google.com/vertex-ai/generative-ai/docs/agent-engine/code-execution/overview to create a code execution sandbox environment.
11+
12+
* 2. Replace the SANDBOX_RESOURCE_NAME with the one you just created. If you dont want to create a new sandbox environment directly, the Agent Engine Code Execution Sandbox will create one for you by default using the AGENT_ENGINE_RESOURCE_NAME you specified, however, please ensure to clean up sandboxes after use, otherwise, it will consume quotas.
13+
14+
15+
## Sample prompt
16+
17+
* Can you write a function that calculates the sum from 1 to 100.
18+
* The dataset is given as below. Store,Date,Weekly_Sales,Holiday_Flag,Temperature,Fuel_Price,CPI,Unemployment Store 1,2023-06-01,1000,0,70,3.0,200,5 Store 2,2023-06-02,1200,1,80,3.5,210,6 Store 3,2023-06-03,1400,0,90,4.0,220,7 Store 4,2023-06-04,1600,1,70,4.5,230,8 Store 5,2023-06-05,1800,0,80,5.0,240,9 Store 6,2023-06-06,2000,1,90,5.5,250,10 Store 7,2023-06-07,2200,0,90,6.0,260,11 Plot a scatter plot showcasing the relationship between Weekly Sales and Temperature for each store, distinguishing stores with a Holiday Flag.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from . import agent
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Data science agent."""
16+
17+
from google.adk.agents.llm_agent import Agent
18+
from google.adk.code_executors.agent_engine_sandbox_code_executor import AgentEngineSandboxCodeExecutor
19+
20+
21+
def base_system_instruction():
22+
"""Returns: data science agent system instruction."""
23+
24+
return """
25+
# Guidelines
26+
27+
**Objective:** Assist the user in achieving their data analysis goals within the context of a Python Colab notebook, **with emphasis on avoiding assumptions and ensuring accuracy.** Reaching that goal can involve multiple steps. When you need to generate code, you **don't** need to solve the goal in one go. Only generate the next step at a time.
28+
29+
**Code Execution:** All code snippets provided will be executed within the Colab environment.
30+
31+
**Statefulness:** All code snippets are executed and the variables stays in the environment. You NEVER need to re-initialize variables. You NEVER need to reload files. You NEVER need to re-import libraries.
32+
33+
**Output Visibility:** Always print the output of code execution to visualize results, especially for data exploration and analysis. For example:
34+
- To look a the shape of a pandas.DataFrame do:
35+
```tool_code
36+
print(df.shape)
37+
```
38+
The output will be presented to you as:
39+
```tool_outputs
40+
(49, 7)
41+
42+
```
43+
- To display the result of a numerical computation:
44+
```tool_code
45+
x = 10 ** 9 - 12 ** 5
46+
print(f'{{x=}}')
47+
```
48+
The output will be presented to you as:
49+
```tool_outputs
50+
x=999751168
51+
52+
```
53+
- You **never** generate ```tool_outputs yourself.
54+
- You can then use this output to decide on next steps.
55+
- Print just variables (e.g., `print(f'{{variable=}}')`.
56+
57+
**No Assumptions:** **Crucially, avoid making assumptions about the nature of the data or column names.** Base findings solely on the data itself. Always use the information obtained from `explore_df` to guide your analysis.
58+
59+
**Available files:** Only use the files that are available as specified in the list of available files.
60+
61+
**Data in prompt:** Some queries contain the input data directly in the prompt. You have to parse that data into a pandas DataFrame. ALWAYS parse all the data. NEVER edit the data that are given to you.
62+
63+
**Answerability:** Some queries may not be answerable with the available data. In those cases, inform the user why you cannot process their query and suggest what type of data would be needed to fulfill their request.
64+
65+
"""
66+
67+
68+
root_agent = Agent(
69+
model="gemini-2.0-flash-001",
70+
name="agent_engine_code_execution_agent",
71+
instruction=base_system_instruction() + """
72+
73+
74+
You need to assist the user with their queries by looking at the data and the context in the conversation.
75+
You final answer should summarize the code and code execution relevant to the user query.
76+
77+
You should include all pieces of data to answer the user query, such as the table from code execution results.
78+
If you cannot answer the question directly, you should follow the guidelines above to generate the next step.
79+
If the question can be answered directly with writing any code, you should do that.
80+
If you doesn't have enough data to answer the question, you should ask for clarification from the user.
81+
82+
You should NEVER install any package on your own like `pip install ...`.
83+
When plotting trends, you should make sure to sort and order the data by the x-axis.
84+
85+
86+
""",
87+
code_executor=AgentEngineSandboxCodeExecutor(
88+
# Replace with your sandbox resource name if you already have one.
89+
sandbox_resource_name="SANDBOX_RESOURCE_NAME",
90+
# "projects/vertex-agent-loadtest/locations/us-central1/reasoningEngines/6842889780301135872/sandboxEnvironments/6545148628569161728",
91+
# Replace with agent engine resource name used for creating sandbox if
92+
# sandbox_resource_name is not set.
93+
agent_engine_resource_name="AGENT_ENGINE_RESOURCE_NAME",
94+
),
95+
)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ dependencies = [
3232
"click>=8.1.8, <9.0.0", # For CLI tools
3333
"fastapi>=0.115.0, <1.0.0", # FastAPI framework
3434
"google-api-python-client>=2.157.0, <3.0.0", # Google API client discovery
35-
"google-cloud-aiplatform[agent_engines]>=1.112.0, <2.0.0",# For VertexAI integrations, e.g. example store.
35+
"google-cloud-aiplatform[agent_engines]>=1.121.0, <2.0.0",# For VertexAI integrations, e.g. example store.
3636
"google-cloud-bigtable>=2.32.0", # For Bigtable database
3737
"google-cloud-discoveryengine>=0.13.12, <0.14.0", # For Discovery Engine Search Tool
3838
"google-cloud-secret-manager>=2.22.0, <3.0.0", # Fetching secrets in RestAPI Tool

src/google/adk/code_executors/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
16+
1517
import logging
1618

1719
from .base_code_executor import BaseCodeExecutor
@@ -29,6 +31,7 @@
2931
'VertexAiCodeExecutor',
3032
'ContainerCodeExecutor',
3133
'GkeCodeExecutor',
34+
'AgentEngineSandboxCodeExecutor',
3235
]
3336

3437

@@ -63,4 +66,14 @@ def __getattr__(name: str):
6366
'GkeCodeExecutor requires additional dependencies. '
6467
'Please install with: pip install "google-adk[extensions]"'
6568
) from e
69+
elif name == 'AgentEngineSandboxCodeExecutor':
70+
try:
71+
from .agent_engine_sandbox_code_executor import AgentEngineSandboxCodeExecutor
72+
73+
return AgentEngineSandboxCodeExecutor
74+
except ImportError as e:
75+
raise ImportError(
76+
'AgentEngineSandboxCodeExecutor requires additional dependencies. '
77+
'Please install with: pip install "google-adk[extensions]"'
78+
) from e
6679
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import annotations
16+
17+
import json
18+
import logging
19+
import mimetypes
20+
import re
21+
from typing import Optional
22+
23+
from typing_extensions import override
24+
import vertexai
25+
from vertexai import types
26+
27+
from ..agents.invocation_context import InvocationContext
28+
from ..utils.feature_decorator import experimental
29+
from .base_code_executor import BaseCodeExecutor
30+
from .code_execution_utils import CodeExecutionInput
31+
from .code_execution_utils import CodeExecutionResult
32+
from .code_execution_utils import File
33+
34+
logger = logging.getLogger('google_adk.' + __name__)
35+
36+
37+
@experimental
38+
class AgentEngineSandboxCodeExecutor(BaseCodeExecutor):
39+
"""A code executor that uses Agent Engine Code Execution Sandbox to execute code.
40+
41+
Attributes:
42+
sandbox_resource_name: If set, load the existing resource name of the code
43+
interpreter extension instead of creating a new one. Format:
44+
projects/123/locations/us-central1/reasoningEngines/456/sandboxEnvironments/789
45+
"""
46+
47+
sandbox_resource_name: str = None
48+
49+
def __init__(
50+
self,
51+
sandbox_resource_name: Optional[str] = None,
52+
agent_engine_resource_name: Optional[str] = None,
53+
**data,
54+
):
55+
"""Initializes the AgentEngineSandboxCodeExecutor.
56+
57+
Args:
58+
sandbox_resource_name: If set, load the existing resource name of code
59+
execution sandbox, if not set, create a new one. Format:
60+
projects/123/locations/us-central1/reasoningEngines/456/
61+
sandboxEnvironments/789
62+
agent_engine_resource_name: The resource name of the agent engine to use
63+
to create the code execution sandbox. Format:
64+
projects/123/locations/us-central1/reasoningEngines/456, when both
65+
sandbox_resource_name and agent_engine_resource_name are set,
66+
agent_engine_resource_name will be ignored.
67+
**data: Additional keyword arguments to be passed to the base class.
68+
"""
69+
super().__init__(**data)
70+
sandbox_resource_name_pattern = r'^projects/([a-zA-Z0-9-_]+)/locations/([a-zA-Z0-9-_]+)/reasoningEngines/(\d+)/sandboxEnvironments/(\d+)$'
71+
agent_engine_resource_name_pattern = r'^projects/([a-zA-Z0-9-_]+)/locations/([a-zA-Z0-9-_]+)/reasoningEngines/(\d+)$'
72+
73+
if sandbox_resource_name is not None:
74+
self.sandbox_resource_name = sandbox_resource_name
75+
self._project_id, self._location = (
76+
self._get_project_id_and_location_from_resource_name(
77+
sandbox_resource_name, sandbox_resource_name_pattern
78+
)
79+
)
80+
elif agent_engine_resource_name is not None:
81+
self._project_id, self._location = (
82+
self._get_project_id_and_location_from_resource_name(
83+
agent_engine_resource_name, agent_engine_resource_name_pattern
84+
)
85+
)
86+
# @TODO - Add TTL for sandbox creation after it is available
87+
# in SDK.
88+
operation = self._get_api_client().agent_engines.sandboxes.create(
89+
spec={'code_execution_environment': {}},
90+
name=agent_engine_resource_name,
91+
config=types.CreateAgentEngineSandboxConfig(
92+
display_name='default_sandbox'
93+
),
94+
)
95+
self.sandbox_resource_name = operation.response.name
96+
else:
97+
raise ValueError(
98+
'Either sandbox_resource_name or agent_engine_resource_name must be'
99+
' set.'
100+
)
101+
102+
@override
103+
def execute_code(
104+
self,
105+
invocation_context: InvocationContext,
106+
code_execution_input: CodeExecutionInput,
107+
) -> CodeExecutionResult:
108+
# Execute the code.
109+
input_data = {
110+
'code': code_execution_input.code,
111+
}
112+
if code_execution_input.input_files:
113+
input_data['files'] = [
114+
{
115+
'name': f.name,
116+
'contents': f.content,
117+
'mimeType': f.mime_type,
118+
}
119+
for f in code_execution_input.input_files
120+
]
121+
122+
code_execution_response = (
123+
self._get_api_client().agent_engines.sandboxes.execute_code(
124+
name=self.sandbox_resource_name,
125+
input_data=input_data,
126+
)
127+
)
128+
saved_files = []
129+
stdout = ''
130+
stderr = ''
131+
for output in code_execution_response.outputs:
132+
if output.mime_type == 'application/json' and (
133+
output.metadata is None
134+
or output.metadata.attributes is None
135+
or 'file_name' not in output.metadata.attributes
136+
):
137+
json_output_data = json.loads(output.data.decode('utf-8'))
138+
stdout = json_output_data.get('stdout', '')
139+
stderr = json_output_data.get('stderr', '')
140+
else:
141+
file_name = ''
142+
if (
143+
output.metadata is not None
144+
and output.metadata.attributes is not None
145+
):
146+
file_name = output.metadata.attributes.get('file_name', b'').decode(
147+
'utf-8'
148+
)
149+
mime_type = output.mime_type
150+
if not mime_type:
151+
mime_type, _ = mimetypes.guess_type(file_name)
152+
saved_files.append(
153+
File(
154+
name=file_name,
155+
content=output.data,
156+
mime_type=mime_type,
157+
)
158+
)
159+
160+
# Collect the final result.
161+
return CodeExecutionResult(
162+
stdout=stdout,
163+
stderr=stderr,
164+
output_files=saved_files,
165+
)
166+
167+
def _get_api_client(self):
168+
"""Instantiates an API client for the given project and location.
169+
170+
It needs to be instantiated inside each request so that the event loop
171+
management can be properly propagated.
172+
173+
Returns:
174+
An API client for the given project and location.
175+
"""
176+
return vertexai.Client(project=self._project_id, location=self._location)
177+
178+
def _get_project_id_and_location_from_resource_name(
179+
self, resource_name: str, pattern: str
180+
) -> tuple[str, str]:
181+
"""Extracts the project ID and location from the resource name."""
182+
match = re.fullmatch(pattern, resource_name)
183+
184+
if not match:
185+
raise ValueError(f'resource name {resource_name} is not valid.')
186+
187+
return match.groups()[0], match.groups()[1]

src/google/adk/code_executors/code_execution_utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
"""Utility functions for code execution."""
1616

17+
from __future__ import annotations
18+
1719
import base64
1820
import binascii
1921
import copy
@@ -34,9 +36,9 @@ class File:
3436
The name of the file with file extension (e.g., "file.csv").
3537
"""
3638

37-
content: str
39+
content: str | bytes
3840
"""
39-
The base64-encoded bytes of the file content.
41+
The base64-encoded bytes of the file content or the original bytes of the file content.
4042
"""
4143

4244
mime_type: str = 'text/plain'

0 commit comments

Comments
 (0)