Skip to content

Commit e8ed2b9

Browse files
committed
Allow JSON imports in coding env
1 parent 878280b commit e8ed2b9

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

envs/coding_env/server/python_executor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
logger = logging.getLogger(__name__)
3232
logger.addHandler(logging.NullHandler())
3333

34+
DEFAULT_SAFE_IMPORTS = ["json"]
35+
3436

3537
class PyExecutor:
3638
"""Wrapper around smolagents LocalPythonExecutor.
@@ -43,7 +45,7 @@ class PyExecutor:
4345

4446
def __init__(self, additional_imports: list[str] | None = None):
4547
if additional_imports is None:
46-
additional_imports = []
48+
additional_imports = DEFAULT_SAFE_IMPORTS.copy()
4749

4850
self._executor = LocalPythonExecutor(
4951
additional_authorized_imports=additional_imports

tests/envs/test_python_codeact_reset.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,20 @@ def test_reset_clears_imports():
119119
)
120120

121121

122+
def test_json_import_is_available_by_default():
123+
"""Test that the coding env permits common JSON serialization code."""
124+
env = PythonCodeActEnv()
125+
env.reset()
126+
127+
action = CodeAction(
128+
code="import json\nprint(json.dumps({'status': 'ok', 'n': 3}, sort_keys=True))"
129+
)
130+
obs = env.step(action)
131+
132+
assert obs.exit_code == 0
133+
assert obs.stdout.strip() == '{"n": 3, "status": "ok"}'
134+
135+
122136
def test_reset_preserves_step_count_reset():
123137
"""Test that reset() properly resets step count."""
124138
env = PythonCodeActEnv()

0 commit comments

Comments
 (0)