Outputs

Updated at:

Code Interpreter execution results include logs, expression results, rich results, and error information. Your application should handle these fields separately instead of reading only stdout.

Result structure

FieldDescription
execution.logs.stdoutContent written by print() or other standard output.
execution.logs.stderrStandard error output.
execution.textText view of the final bare expression.
execution.resultsRich-result list that can include structured text or tables.
execution.errorRuntime error. Empty on success.
execution.execution_count / execution.executionCountExecution sequence number inside the current context.

Read stdout and stderr

execution = sandbox.run_code("""
import sys

print("hello")
print("warning", file=sys.stderr)
""")

stdout = "".join(execution.logs.stdout or [])
stderr = "".join(execution.logs.stderr or [])

print(stdout.strip())
print(stderr.strip())

TypeScript example:

const execution = await sandbox.runCode(`
import sys

print("hello")
print("warning", file=sys.stderr)
`);

const stdout = execution.logs.stdout.join("");
const stderr = execution.logs.stderr.join("");

console.log(stdout.trim());
console.log(stderr.trim());

Read expression results

execution = sandbox.run_code("1 + 1")
print(execution.text)

TypeScript example:

const execution = await sandbox.runCode("1 + 1");
console.log(execution.text);

execution.text is suitable for simple text results. For tables, read from execution.results. For charts, save them as files and retrieve them through Filesystem.

Read rich results

execution = sandbox.run_code("""
import pandas as pd

df = pd.DataFrame({"month": ["2026-01", "2026-02"], "revenue": [120, 180]})
df
""")

for result in execution.results:
    if getattr(result, "text", None):
        print(result.text)

TypeScript example:

const execution = await sandbox.runCode(`
import pandas as pd

df = pd.DataFrame({"month": ["2026-01", "2026-02"], "revenue": [120, 180]})
df
`);

for (const result of execution.results) {
  if (result.text) {
    console.log(result.text);
  }
}

Different SDK versions may expose Result fields differently. Print execution.results once during integration to confirm the field names returned by your current SDK.

Handle errors

execution = sandbox.run_code("raise ValueError('bad input')")

if execution.error:
    print(execution.error.name)
    print(execution.error.value)
    print(execution.error.traceback)

TypeScript example:

const execution = await sandbox.runCode("raise ValueError('bad input')");

if (execution.error) {
  console.log(execution.error.name);
  console.log(execution.error.value);
  console.log(execution.error.traceback);
}

Field semantics

The execution.error fields reflect how the SDK wraps the underlying exception. Their actual values may differ from the input exception you provided:

Field

Description

execution.error.name

SDK-wrapped error type name. May differ from the original Python exception class name (for example, the SDK may return "ExecutionError" rather than "ValueError").

execution.error.value

Error summary string. May contain traceback content rather than the original exception message argument (for example, the full stack trace rather than "bad input").

execution.error.traceback

Full stack trace string. Contains the original exception type and message.

Note

Do not rely on execution.error.name or execution.error.value for precise exception type matching. For example, if execution.error.name == "ValueError" may not work as expected because the SDK wraps exceptions before exposing them. To extract the original exception class or message, parse execution.error.traceback instead.

Recommendations

  • When showing results to users, distinguish stdout, expression results, rich results, and errors.

  • In agent scenarios, use execution.error as input for retries or code correction.

  • Prefer execution.results for tables. For charts and report files, write them to the sandbox and download them through Filesystem.