Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: keep a Python script's output when a terminal call times out (#17555)

Nilayb5cf3799990c
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index ae96d3d13da..a08fee45d0e 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -8869,6 +8869,7 @@ def _build_safe_env(workdir: str, shell: "str | None" = None) -> dict[str, str]:
"LANG": os.environ.get("LANG", "C.UTF-8"),
"TERM": "dumb",
"PYTHONIOENCODING": "utf-8",
+ "PYTHONUNBUFFERED": "1",
# A GUI backend opens a native window and blocks plt.show() until closed.
"MPLBACKEND": "Agg",
# Remaps ChatGPT code-interpreter paths (/mnt/data) onto the sandbox cwd.
@@ -9057,6 +9058,7 @@ def _build_bypass_env(workdir: str) -> dict[str, str]:
if var in os.environ:
env[var] = workdir
env.setdefault("MPLBACKEND", "Agg")
+ env["PYTHONUNBUFFERED"] = "1"
return env
diff --git a/studio/backend/tests/test_sandbox_tools.py b/studio/backend/tests/test_sandbox_tools.py
index b98b1bf72c6..47a69b1d9ef 100644
--- a/studio/backend/tests/test_sandbox_tools.py
+++ b/studio/backend/tests/test_sandbox_tools.py
@@ -2768,6 +2768,7 @@ class TestSandboxEnvIsolation:
"LANG",
"TERM",
"PYTHONIOENCODING",
+ "PYTHONUNBUFFERED",
"MPLBACKEND",
"PYTHONPATH",
"_UNSLOTH_SANDBOX_BLOCK_METADATA",
diff --git a/studio/backend/tests/test_tool_output_streaming.py b/studio/backend/tests/test_tool_output_streaming.py
index 846ea5dd658..756a98eeb5e 100644
--- a/studio/backend/tests/test_tool_output_streaming.py
+++ b/studio/backend/tests/test_tool_output_streaming.py
@@ -500,6 +500,29 @@ def test_bash_exec_timeout_keeps_output_already_printed():
assert baseline.endswith("Execution timed out after 1 seconds.")
[email protected]("disable_sandbox", [False, True])
+def test_bash_exec_timeout_keeps_a_python_scripts_unflushed_output(disable_sandbox, monkeypatch):
+ monkeypatch.delenv("PYTHONUNBUFFERED", raising = False)
+ code = "import time; print('progress'); time.sleep(30)"
+ command = f'"{sys.executable}" -c "{code}"'
+ result = _bash_exec(command, timeout = 5, disable_sandbox = disable_sandbox)
+ assert "progress" in result
+ assert result.endswith("Execution timed out after 5 seconds.")
+
+
+def test_bash_exec_streams_a_python_scripts_unflushed_print_live():
+ code = "import time; print('progress'); time.sleep(3); print('done')"
+ command = f'"{sys.executable}" -c "{code}"'
+ seen: list[tuple[float, str]] = []
+ result = _bash_exec(
+ command, timeout = 60, output_callback = lambda text: seen.append((time.monotonic(), text))
+ )
+ finished = time.monotonic()
+ assert "progress" in result and "done" in result
+ assert seen and "progress" in seen[0][1]
+ assert finished - seen[0][0] > 2
+
+
def test_python_exec_timeout_still_says_so_when_the_output_printed_a_marker():
from core.inference.tool_loop_controller import strip_result_for_model