Response Types

AI Computer methods return strongly typed response objects that provide execution results, status information, and error details.

SandboxResponse

Base response type returned by most API methods. Contains success status and optional data or error information.

1from dataclasses import dataclass
2from typing import Optional, Dict, Any
3
4@dataclass
5class SandboxResponse:
6    success: bool
7    data: Optional[Dict[str, Any]] = None
8    error: Optional[str] = None
9
10# Example usage
11response = await client.setup()
12if response.success:
13    print("Setup successful:", response.data)
14else:
15    print("Setup failed:", response.error)

Fields

success

Whether the operation completed successfully.

Type: bool

data

Optional dictionary containing operation-specific data.

Type: Optional[Dict[str, Any]]

error

Error message if the operation failed.

Type: Optional[str]

ExecutionResponse

Specialized response type for code execution results. Extends SandboxResponse with execution-specific information.

1@dataclass
2class ExecutionResponse(SandboxResponse):
3    output: str = ""
4    execution_time: float = 0.0
5    memory_usage: int = 0
6    exit_code: int = 0
7
8# Example usage
9response = await client.execute_code("print('Hello')")
10if response.success:
11    print("Output:", response.output)
12    print(f"Execution time: {response.execution_time:.2f}s")
13    print(f"Memory used: {response.memory_usage} bytes")
14    print(f"Exit code: {response.exit_code}")

Additional Fields

output

Combined stdout and stderr output from the execution.

Type: str

execution_time

Time taken to execute the code in seconds.

Type: float

memory_usage

Peak memory usage during execution in bytes.

Type: int

exit_code

Process exit code (0 for success).

Type: int

StreamEvent

Event object used in streaming execution to provide real-time updates.

1@dataclass
2class StreamEvent:
3    type: str  # 'stdout', 'stderr', 'error', or 'completed'
4    data: str
5    timestamp: float
6
7# Example usage
8async for event in client.execute_code_stream(code):
9    if event.type == 'stdout':
10        print(f"[{event.timestamp}] Output: {event.data}")
11    elif event.type == 'stderr':
12        print(f"[{event.timestamp}] Error: {event.data}")
13    elif event.type == 'completed':
14        print("Execution completed")

Event Types

stdout

Standard output from the code execution.

data: Output text

stderr

Standard error output from the code execution.

data: Error text

error

Execution or system errors that stop the process.

data: Error message

completed

Signals successful completion of execution.

data: Empty string

Error Types

Common error types that may be encountered during API usage.

SandboxError

Base exception class for sandbox-related errors.

1try:
2    await client.setup()
3except SandboxError as e:
4    print(f"Sandbox error: {str(e)}")

ExecutionError

Raised when code execution fails.

1try:
2    await client.execute_code("invalid python code")
3except ExecutionError as e:
4    print(f"Execution failed: {str(e)}")

ResourceLimitError

Raised when resource limits are exceeded.

1try:
2    await client.execute_code("while True: pass")  # CPU loop
3except ResourceLimitError as e:
4    print(f"Resource limit exceeded: {str(e)}")