Implementation:Togethercomputer Together python CodeInterpreter Run
| 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
- Repository: Together Python
- File: src/together/resources/code_interpreter.py
- Lines: 1-82
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}]',
}],
)