> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ns.rocks/llms.txt
> Use this file to discover all available pages before exploring further.

# Streaming Code Output

> Stream stdout, stderr, results, artifacts, and errors from code execution.

## Stream stdout

<CodeGroup>
  ```python Python SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  def on_stdout(message):
      print(message.line, end="")

  execution = machine.code_interpreter.run_code(
      "for i in range(3): print(f'line {i}')",
      language="python",
      on_stdout=on_stdout,
      envs={"RUN_MODE": "docs"},
      timeout=30,
  )
  ```

  ```typescript TypeScript SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  const execution = await machine.code.run(
    "for i in range(3): print(f'line {i}')",
    {
      language: "python",
      onStdout: (message) => process.stdout.write(message.line),
      envs: { RUN_MODE: "docs" },
      timeout: 30,
    },
  );
  ```

  ```bash CLI theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  nullspace machine code run mch_123 "for i in range(3): print(f'line {i}')" \
    --language python --env RUN_MODE=docs --timeout 30
  ```

  ```bash HTTP API theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  # returns a Server-Sent Events stream of JSON events
  curl -N -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/execute" \
    -H "Authorization: Bearer ${NULLSPACE_API_KEY}" \
    -H "Content-Type: application/json" \
    -d '{"code": "for i in range(3): print(f'\''line {i}'\'')", "language": "python", "envs": {"RUN_MODE": "docs"}, "timeout": 30}'
  ```
</CodeGroup>

## Stream results and errors

<CodeGroup>
  ```python Python SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  def on_result(result):
      print("result", result.text)

  def on_error(error):
      print(error.name, error.value)

  machine.code_interpreter.run_code(
      "1 + 1",
      on_result=on_result,
      on_error=on_error,
  )
  ```

  ```typescript TypeScript SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  await machine.code.run("1 + 1", {
    onResult: (result) => console.log("result", result.text),
    onError: (error) => console.log(error.name, error.value),
  });
  ```

  ```bash CLI theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  nullspace machine code run mch_123 "1 + 1"
  ```

  ```bash HTTP API theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  # result and error events arrive on the SSE stream
  curl -N -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/execute" \
    -H "Authorization: Bearer ${NULLSPACE_API_KEY}" \
    -H "Content-Type: application/json" -d '{"code": "1 + 1"}'
  ```
</CodeGroup>

## Stream artifacts

<CodeGroup>
  ```python Python SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  def on_artifact(artifact):
      print(artifact.id, artifact.kind)

  machine.code_interpreter.run_code(
      "from IPython.display import SVG\nSVG(data='<svg></svg>')",
      on_artifact=on_artifact,
  )
  ```

  ```typescript TypeScript SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  await machine.code.run(
    "from IPython.display import SVG\nSVG(data='<svg></svg>')",
    { onArtifact: (artifact) => console.log(artifact.id, artifact.kind) },
  );
  ```

  ```bash CLI theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  nullspace machine code run mch_123 "from IPython.display import SVG
  SVG(data='<svg></svg>')"
  ```

  ```bash HTTP API theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  # artifact events arrive on the SSE stream
  curl -N -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/execute" \
    -H "Authorization: Bearer ${NULLSPACE_API_KEY}" \
    -H "Content-Type: application/json" \
    -d '{"code": "from IPython.display import SVG\nSVG(data=\"<svg></svg>\")"}'
  ```
</CodeGroup>

`machine.run_code()` is a convenience wrapper for standard output, standard
error, results, artifacts, errors, language, environment, context, and timeout
callbacks.

## Related

* [Code Interpreter](./overview)
* [Runs and artifacts](./runs-artifacts)
