Skip to content

Commit 6262f94

Browse files
GWealecopybara-github
authored andcommitted
fix: use correct 'content' key in sandbox code executor input files
Merge #5505 Fixes #5500 ### Root Cause `AgentEngineSandboxCodeExecutor` builds the input file payload with key `'contents'` (plural), but the Vertex AI SDK (`vertexai/_genai/sandboxes.py`) reads `'content'` (singular). This causes `file.get("content", b"")` to always return the default empty bytes, so uploaded input files silently arrive as zero bytes in the sandbox. ### Fix One-character change: `'contents'` → `'content'` at line 177. Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 930814004
1 parent be1425b commit 6262f94

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

src/google/adk/code_executors/agent_engine_sandbox_code_executor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def execute_code(
180180
input_data['files'] = [
181181
{
182182
'name': f.name,
183-
'contents': f.content,
183+
'content': f.content,
184184
'mimeType': f.mime_type,
185185
}
186186
for f in code_execution_input.input_files

tests/unittests/code_executors/test_agent_engine_sandbox_code_executor.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from google.adk.agents.invocation_context import InvocationContext
2121
from google.adk.code_executors.agent_engine_sandbox_code_executor import AgentEngineSandboxCodeExecutor
2222
from google.adk.code_executors.code_execution_utils import CodeExecutionInput
23+
from google.adk.code_executors.code_execution_utils import File
2324
from google.adk.sessions.session import Session
2425
import pytest
2526

@@ -125,6 +126,40 @@ def test_execute_code_success(
125126
input_data={"code": 'print("hello world")'},
126127
)
127128

129+
@patch("vertexai.Client")
130+
def test_execute_code_sends_input_files_with_content_key(
131+
self,
132+
mock_vertexai_client,
133+
mock_invocation_context,
134+
):
135+
"""Input files must be sent under the 'content' key the SDK expects."""
136+
mock_api_client = MagicMock()
137+
mock_vertexai_client.return_value = mock_api_client
138+
mock_response = MagicMock()
139+
mock_response.outputs = []
140+
mock_api_client.agent_engines.sandboxes.execute_code.return_value = (
141+
mock_response
142+
)
143+
144+
executor = AgentEngineSandboxCodeExecutor(
145+
sandbox_resource_name="projects/123/locations/us-central1/reasoningEngines/456/sandboxEnvironments/789"
146+
)
147+
code_input = CodeExecutionInput(
148+
code='print("hi")',
149+
input_files=[
150+
File(name="data.csv", content="a,b,c", mime_type="text/csv")
151+
],
152+
)
153+
executor.execute_code(mock_invocation_context, code_input)
154+
155+
_, call_kwargs = (
156+
mock_api_client.agent_engines.sandboxes.execute_code.call_args
157+
)
158+
sent_files = call_kwargs["input_data"]["files"]
159+
assert sent_files == [
160+
{"name": "data.csv", "content": "a,b,c", "mimeType": "text/csv"}
161+
]
162+
128163
@patch("vertexai.Client")
129164
def test_execute_code_recreates_sandbox_when_get_returns_none(
130165
self,

0 commit comments

Comments
 (0)