Skip to content

fix(python): return only stdout from CodeBox.run()#248

Merged
DorianZheng merged 1 commit intomainfrom
fix/codebox-run-stdout-only
Feb 13, 2026
Merged

fix(python): return only stdout from CodeBox.run()#248
DorianZheng merged 1 commit intomainfrom
fix/codebox-run-stdout-only

Conversation

@yingjunwu
Copy link
Copy Markdown
Contributor

@yingjunwu yingjunwu commented Feb 12, 2026

Summary

  • Change CodeBox.run() to return only stdout (previously returned stdout + stderr)
  • Update SyncCodeBox.run() to match (same change)
  • Update run_script() docstrings in both async and sync APIs
  • Update tests that asserted errors in run() output to use exec() instead

Closes #237

Note

If PR #249 is also merged, run_guest_script() should be updated to return stdout only for consistency.

Test plan

  • Verify run() returns only stdout for successful code
  • Verify errors (exceptions, syntax errors) are NOT in run() output
  • Verify exec() still returns both stdout and stderr
  • Verify SyncCodeBox.run() behavior matches async CodeBox.run()
  • Verify updated tests pass

🤖 Generated with Claude Code

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adjusts the Python SDK’s CodeBox.run() convenience API to avoid leaking container/runtime warnings (e.g., seccomp/libcontainer warnings on macOS) into the returned string by returning stdout only.

Changes:

  • Update CodeBox.run() to return only result.stdout (instead of stdout + stderr).
  • Update CodeBox.run() docstring to reflect the new stdout-only behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 70 to +72
# Execute Python code using python3 -c
result = await self.exec("/usr/local/bin/python", "-c", code)
return result.stdout + result.stderr
return result.stdout
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

run() now returns only result.stdout unconditionally. This will drop Python tracebacks/syntax errors (which are written to stderr) and will break existing behavior/tests that rely on error output being present (e.g., exceptions, NameError). Consider handling non-zero result.exit_code by either (a) raising an exception that includes result.stderr (and possibly error_message) or (b) returning stdout + stderr only on failure, while keeping stdout-only on success to avoid the macOS seccomp warning noise.

Copilot uses AI. Check for mistakes.
Comment on lines 55 to +57
Returns:
Execution output as a string (stdout + stderr)
Execution stdout as a string. Use exec() directly if you need
both stdout and stderr.
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring now states callers should use exec() if they need stderr, but run_script() delegates to run() and the sync wrapper (SyncCodeBox) is documented as mirroring the async API. To avoid inconsistent behavior across these convenience APIs, consider updating the related wrappers/docs (and any tests) to match the new stdout-only contract, or adopt an error-path exception/combined-output strategy as suggested below.

Copilot uses AI. Check for mistakes.
@yingjunwu
Copy link
Copy Markdown
Contributor Author

Self-review after revision: This PR is now correct. The initial version had 3 bugs that have been fixed:

Bug 1 — Broke tests. Three async tests and one sync test asserted that errors (ValueError, SyntaxError, NameError) appear in run() output. Since run() now returns only stdout and Python errors go to stderr, those tests would fail. Fixed by changing them to use exec() and assert on result.stderr.

Bug 2 — SyncCodeBox not updated. SyncCodeBox.run() had its own independent implementation still returning stdout + stderr. Its class docstring says "API mirrors async CodeBox exactly", which would become false. Fixed: SyncCodeBox.run() now also returns result.stdout.

Bug 3 — Stale run_script() docstring. run_script() calls self.run(), so its return value changed implicitly. But its docstring still said "Execution output as a string". Fixed in both async and sync versions.

Dependency note: If PR #249 is also merged, run_guest_script() will need a follow-up to return stdout only for consistency.

CodeBox.run() returned result.stdout + result.stderr, which caused
seccomp/libcontainer warnings to leak into the returned string:

  'hello\n\x1b[2m...\x1b[0m \x1b[33m WARN\x1b[0m ...seccomp not available...\n'

This breaks any code that parses run() output, especially in the
primary AI agent use case where LLMs need clean output.

Fix: return only result.stdout. Users who need stderr can use exec()
directly and access result.stderr separately.

Closes #237

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Copilot AI review requested due to automatic review settings February 13, 2026 02:13
@yingjunwu yingjunwu force-pushed the fix/codebox-run-stdout-only branch from 23e83f5 to 0c5d12d Compare February 13, 2026 02:13
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@DorianZheng DorianZheng merged commit aeecaa3 into main Feb 13, 2026
25 checks passed
@DorianZheng DorianZheng deleted the fix/codebox-run-stdout-only branch February 13, 2026 02:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CodeBox.run() includes seccomp ANSI warnings in returned stdout string

3 participants