Skip to content

Commit 2b7e08a

Browse files
jordanchendevwukath
authored andcommitted
fix: send correct field names for sandbox input files
Merge #5958 ## Problem `AgentEngineSandboxCodeExecutor` was building the input-file payload with incorrect JSON field names: | Code sent | API expects | |------------|-------------| | `contents` | `content` | | `mimeType` | `mime_type` | This caused all input files to be silently unreadable inside the sandbox, producing errors such as: ``` pandas.errors.EmptyDataError: No columns to parse from file ``` Fixes #3690 ## Changes - `src/google/adk/code_executors/agent_engine_sandbox_code_executor.py` — rename the two dict keys in the `input_data['files']` list comprehension. - `tests/unittests/code_executors/test_agent_engine_sandbox_code_executor.py` — add regression test `test_execute_code_sends_correct_field_names_for_input_files` that verifies the correct keys are sent to the API. ## Testing plan - [x] New regression test added that asserts `content` and `mime_type` are used (was failing before the fix, passes after). - [x] All existing tests in the file still pass. ### pytest output ``` uv run --extra test python -m pytest tests/unittests/code_executors/test_agent_engine_sandbox_code_executor.py -v ======================== 11 passed, 5 warnings in 2.04s ======================== ``` ### pre-commit ``` pre-commit run --files src/google/adk/code_executors/agent_engine_sandbox_code_executor.py \ tests/unittests/code_executors/test_agent_engine_sandbox_code_executor.py isort....................................................................Passed pyink....................................................................Passed addlicense...............................................................Passed ``` Co-authored-by: Kathy Wu <wukathy@google.com> COPYBARA_INTEGRATE_REVIEW=#5958 from jordanchendev:fix/3690-sandbox-input-file-field-names eefe911 PiperOrigin-RevId: 933868136
1 parent 0cb4c81 commit 2b7e08a

2 files changed

Lines changed: 50 additions & 2 deletions

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
@@ -181,7 +181,7 @@ def execute_code(
181181
{
182182
'name': f.name,
183183
'content': f.content,
184-
'mimeType': f.mime_type,
184+
'mime_type': f.mime_type,
185185
}
186186
for f in code_execution_input.input_files
187187
]

tests/unittests/code_executors/test_agent_engine_sandbox_code_executor.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def test_execute_code_sends_input_files_with_content_key(
157157
)
158158
sent_files = call_kwargs["input_data"]["files"]
159159
assert sent_files == [
160-
{"name": "data.csv", "content": "a,b,c", "mimeType": "text/csv"}
160+
{"name": "data.csv", "content": "a,b,c", "mime_type": "text/csv"}
161161
]
162162

163163
@patch("vertexai.Client")
@@ -355,6 +355,54 @@ def test_execute_code_creates_sandbox_if_missing(
355355
input_data={"code": 'print("hello world")'},
356356
)
357357

358+
@patch("vertexai.Client")
359+
def test_execute_code_sends_correct_field_names_for_input_files(
360+
self,
361+
mock_vertexai_client,
362+
mock_invocation_context,
363+
):
364+
"""Input files are sent with 'content' and 'mime_type' keys (not 'contents'/'mimeType')."""
365+
mock_api_client = MagicMock()
366+
mock_vertexai_client.return_value = mock_api_client
367+
368+
mock_response = MagicMock()
369+
mock_json_output = MagicMock()
370+
mock_json_output.mime_type = "application/json"
371+
mock_json_output.data = json.dumps({"msg_out": "", "msg_err": ""}).encode(
372+
"utf-8"
373+
)
374+
mock_json_output.metadata = None
375+
mock_response.outputs = [mock_json_output]
376+
mock_api_client.agent_engines.sandboxes.execute_code.return_value = (
377+
mock_response
378+
)
379+
380+
executor = AgentEngineSandboxCodeExecutor(
381+
sandbox_resource_name="projects/123/locations/us-central1/reasoningEngines/456/sandboxEnvironments/789"
382+
)
383+
code_input = CodeExecutionInput(
384+
code="import pandas as pd; df = pd.read_csv('data.csv')",
385+
input_files=[
386+
File(
387+
name="data.csv", content=b"col1,col2\n1,2", mime_type="text/csv"
388+
)
389+
],
390+
)
391+
392+
executor.execute_code(mock_invocation_context, code_input)
393+
394+
mock_api_client.agent_engines.sandboxes.execute_code.assert_called_once_with(
395+
name="projects/123/locations/us-central1/reasoningEngines/456/sandboxEnvironments/789",
396+
input_data={
397+
"code": "import pandas as pd; df = pd.read_csv('data.csv')",
398+
"files": [{
399+
"name": "data.csv",
400+
"content": b"col1,col2\n1,2",
401+
"mime_type": "text/csv",
402+
}],
403+
},
404+
)
405+
358406
def test_init_with_agent_engine_resource_name(self):
359407
"""Tests init when only agent_engine_resource_name is provided."""
360408
agent_engine_name = (

0 commit comments

Comments
 (0)