Reactor Class
The core class for connecting to and interacting with Reactor models in Python.Constructor
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
model_name | str | Yes | The name of the Reactor model to connect to |
api_key | str | No | Your Reactor API key. The SDK auto-fetches a JWT token using this key. Required unless local=True. |
coordinator_url | str | No | The coordinator URL (default: https://api.reactor.inc) |
local | bool | No | Connect to localhost:8080 for local development (default: False) |
Unlike the JavaScript SDK, you pass the API key directly to the constructor and the SDK handles JWT token exchange automatically during
connect().Example
Methods
connect()
Connects to the Reactor coordinator and waits for GPU assignment. Ifapi_key was provided, automatically fetches a JWT token first.
RuntimeError if authentication fails or already connected.
Example:
disconnect()
Disconnects from both the coordinator and GPU machine.recoverable— IfTrue, keeps the session alive for potential reconnection. IfFalse(default), terminates the session completely.
send_command()
Sends a command to the model. Commands are the primary way to control model behavior.command— The command type string (e.g.,"schedule_prompt","start")data— The command payload (typically a dictionary)
publish_track()
Publishes a video track to the model for processing by video-to-video models.track— Anaiortc.MediaStreamTrackobject
unpublish_track()
Stops publishing the video track to the model.set_frame_callback()
Sets a callback to receive individual video frames as NumPy arrays.callback— A function that receives a NumPy array(H, W, 3)with dtypeuint8in RGB format. PassNoneto stop receiving frames.
You can also use the
@reactor.on_frame decorator for the same effect. See Decorators.get_remote_track()
Returns the remote video track from the GPU machine, if available.on()
Registers an event handler.event— The event type to listen for (see Events)handler— Callback function
off()
Removes an event handler.get_status()
Returns the current connection status.ReactorStatus.DISCONNECTED, ReactorStatus.CONNECTING, ReactorStatus.WAITING, or ReactorStatus.READY
Example:
get_state()
Returns the complete current state including status and error information.ReactorState with status and last_error fields.
Example:
get_session_id()
Returns the current session ID if connected.get_last_error()
Returns the most recent error that occurred.reconnect()
Reconnects to an existing session that may have been interrupted.session_id must exist from a previous connection.
Example:
Context Manager
TheReactor class supports async with for automatic connection cleanup:
Events
The following event types can be used withon() and off():
| Event | Handler Signature | Description |
|---|---|---|
"status_changed" | (status: ReactorStatus) | Connection status changed |
"session_id_changed" | (session_id: str | None) | Session ID changed |
"new_message" | (message: Any) | Message received from the model |
"stream_changed" | (track: MediaStreamTrack) | Video stream changed |
"error" | (error: ReactorError) | Error occurred |
"session_expiration_changed" | (expiration: float | None) | Session expiration time changed |
Next Steps
- See Decorators for the decorator-based API
- See Python Types for type definitions
- See the Python SDK Guide for usage patterns and examples