Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Togethercomputer Together python CodeInterpreter Run

From Leeroopedia
Knowledge Sources
Domains Code_Execution, Tooling
Last Updated 2026-02-15 16:00 GMT

Overview

Concrete tool for executing Python code snippets in a sandboxed environment provided by the Together Python SDK.

Description

The CodeInterpreter class provides a run() method that executes Python code in Together's cloud-hosted code interpreter. Supports session persistence (via session_id for follow-up calls) and file uploads (text or base64-encoded binary files). Returns structured execution results including stdout, stderr, errors, display data, and execute results.

Usage

Import this class when you need to execute Python code remotely, especially for data analysis, visualization, or code evaluation tasks. Access via client.code_interpreter.run().

Code Reference

Source Location

Signature

class CodeInterpreter:
    def run(
        self,
        code: str,
        language: Literal["python"],
        session_id: Optional[str] = None,
        files: Optional[List[Dict[str, Any]]] = None,
    ) -> ExecuteResponse: ...

Import

from together import Together

client = Together()
# Access via client.code_interpreter

I/O Contract

Inputs

Name Type Required Description
code str Yes Python code snippet to execute
language Literal["python"] Yes Programming language (currently only "python")
session_id str No Session identifier for follow-up calls
files List[Dict] No Files to upload: each with name, encoding ("string"/"base64"), content

Outputs

Name Type Description
returns ExecuteResponse Contains data with outputs list, errors, session_id, and status

Usage Examples

from together import Together

client = Together()

# Execute simple code
result = client.code_interpreter.run(
    code="print('Hello, World!')\nresult = 2 + 2\nprint(f'Result: {result}')",
    language="python",
)

for output in result.data.outputs:
    print(f"[{output.type}] {output.data}")

# Use sessions for stateful execution
result1 = client.code_interpreter.run(
    code="x = 42",
    language="python",
)
session = result1.data.session_id

result2 = client.code_interpreter.run(
    code="print(x * 2)",
    language="python",
    session_id=session,
)

# Upload a file and process it
result = client.code_interpreter.run(
    code="import json\nwith open('data.json') as f:\n    data = json.load(f)\nprint(len(data))",
    language="python",
    files=[{
        "name": "data.json",
        "encoding": "string",
        "content": '[{"a": 1}, {"a": 2}]',
    }],
)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment