Skip to content

Personal AI, On Personal Devices

Python

fix(tools): tell the model when code_interpreter printed nothing (#1072)

Wagner Gadea Lorenz4e65e315b52c
diff --git a/src/openjarvis/tools/code_interpreter.py b/src/openjarvis/tools/code_interpreter.py
index 79851642..a1fcc7ce 100644
--- a/src/openjarvis/tools/code_interpreter.py
+++ b/src/openjarvis/tools/code_interpreter.py
@@ -116,6 +116,13 @@ def _child_limits() -> None: # pragma: no cover - POSIX-only, runs in child
pass
+# Small models otherwise invent a result when nothing was printed.
+_NO_OUTPUT = (
+ "(no output) Nothing was printed, so there is no result yet."
+ " Call print() on the value you need and run again; do not guess the answer."
+)
+
+
@ToolRegistry.register("code_interpreter")
class CodeInterpreterTool(BaseTool):
"""Execute Python code after AST validation, in a hardened subprocess."""
@@ -131,7 +138,8 @@ class CodeInterpreterTool(BaseTool):
return ToolSpec(
name="code_interpreter",
description=(
- "Execute Python code and return the output."
+ "Execute Python code and return what it prints"
+ " (use print() to see results)."
" Code is AST-validated and runs in a hardened, isolated"
" subprocess (no imports of os/sys/subprocess/network, no"
" file IO, no eval/exec)."
@@ -202,7 +210,7 @@ class CodeInterpreterTool(BaseTool):
output = output[: self._max_output] + "\n... (output truncated)"
return ToolResult(
tool_name="code_interpreter",
- content=output or "(no output)",
+ content=output or _NO_OUTPUT,
success=result.returncode == 0,
metadata={"returncode": result.returncode},
)
diff --git a/tests/tools/test_code_interpreter.py b/tests/tools/test_code_interpreter.py
index 3b9d8090..aecaf70e 100644
--- a/tests/tools/test_code_interpreter.py
+++ b/tests/tools/test_code_interpreter.py
@@ -141,7 +141,8 @@ class TestCodeInterpreterTool:
tool = CodeInterpreterTool()
result = tool.execute(code="x = 42")
assert result.success is True
- assert result.content == "(no output)"
+ assert result.content.startswith("(no output)")
+ assert "print()" in result.content
def test_tool_id(self):
tool = CodeInterpreterTool()