Skip to content

The agent that grows with you

fix(tools): redirect non-string code payloads in execute_code handler

kshitijc5e2bff6c186
diff --git a/tests/tools/test_code_execution.py b/tests/tools/test_code_execution.py
index 15bf58a97e9..10aab8bf0eb 100644
--- a/tests/tools/test_code_execution.py
+++ b/tests/tools/test_code_execution.py
@@ -590,6 +590,19 @@ class TestExecuteCodeEdgeCases(unittest.TestCase):
self.assertIn("Python source", result["error"])
self.assertIn("terminal(command=...)", result["error"])
+ def test_non_string_code_redirects_instead_of_attributeerror(self):
+ for code in (123, {"code": "print(1)"}, ["print(1)"]):
+ with self.subTest(code=code):
+ result = json.loads(registry.dispatch(
+ "execute_code",
+ {"code": code},
+ task_id="test",
+ ))
+ self.assertIn("error", result)
+ self.assertIn(type(code).__name__, result["error"])
+ self.assertIn("Python source as a string", result["error"])
+ self.assertNotIn("AttributeError", result["error"])
+
def test_windows_returns_error(self):
"""When SANDBOX_AVAILABLE is False (e.g. when the backend deems
the sandbox unusable for this environment), execute_code returns
diff --git a/tools/code_execution_tool.py b/tools/code_execution_tool.py
index 797443711e6..e2853c0d157 100644
--- a/tools/code_execution_tool.py
+++ b/tools/code_execution_tool.py
@@ -2069,7 +2069,12 @@ from tools.registry import registry, tool_error
def _execute_code_handler(args: dict, **kwargs) -> str:
- """Validate raw tool arguments before dispatching to ``execute_code``."""
+ """Recover misdirected calls before dispatching to ``execute_code``.
+
+ Models sometimes reuse terminal's ``command`` argument or send a
+ non-string ``code`` payload; both get an actionable redirect instead
+ of a generic failure.
+ """
# Help models recover when they reuse terminal's ``command`` argument.
if "code" not in args and "command" in args:
logger.warning(
@@ -2081,8 +2086,18 @@ def _execute_code_handler(args: dict, **kwargs) -> str:
"commands; for Python, retry as execute_code(code=...)."
)
+ code = args.get("code", "")
+ if code is not None and not isinstance(code, str):
+ # A non-string payload (int, dict, list) would otherwise surface as
+ # a generic AttributeError from code.strip() — redirect instead.
+ return tool_error(
+ f"execute_code received a {type(code).__name__} in 'code', but it "
+ "requires Python source as a string. Retry as "
+ "execute_code(code=\"...\")."
+ )
+
return execute_code(
- code=args.get("code", ""),
+ code=code or "",
task_id=kwargs.get("task_id"),
enabled_tools=kwargs.get("enabled_tools"),
)