A Python SDK for connecting to avatar services via WebSocket, supporting audio streaming and receiving animation frames.Documentation Index
Fetch the complete documentation index at: https://docs.spatialreal.ai/llms.txt
Use this file to discover all available pages before exploring further.
Repository
The Python SDK is available on GitHub: spatialwalk/avatar-sdk-python and PyPI.Requirements
api_keyapp_idavatar_id- console endpoint URL
- ingress endpoint URL
Endpoints
Build your endpoint URLs using your selected region (see Regions):- Console endpoint URL:
https://console.<region>.spatialwalk.cloud/v1/console - Ingress endpoint URL:
wss://api.<region>.spatialwalk.cloud/v2/driveningress
Quick Start
Session Configuration
Usenew_avatar_session() to configure and create a session:
Session Lifecycle
Audio Format
The SDK currently supports mono 16-bit PCM (s16le) audio:
- Sample rate:
8000,16000,22050,24000,32000,44100,48000 - Channels:
1(mono) - Bit depth:
16-bit - Format: raw PCM bytes
LiveKit Egress Mode
When configured withlivekit_egress, audio and animation data are streamed to a LiveKit room instead of being returned through the WebSocket connection. For end-to-end integration details, see the LiveKit Agent server side guide.
- the server streams output to the specified LiveKit room
- the
transport_framescallback is not invoked - audio and animation data are published under the configured publisher ID
Interrupt
Theinterrupt() method sends an interrupt signal to stop current audio processing. This is only available when using LiveKit egress mode.
end=True has been sent.
Callbacks
transport_frames
Receives animation frames from the server:
on_error
Handles structured SDK errors from the session:
AvatarSDKError instances for token creation failures, WebSocket upgrade rejections, handshake failures, runtime ServerError messages, and unexpected connection drops.
on_close
Called when the session closes:
Error Handling
UseSessionTokenError for token creation failures and AvatarSDKError for all other structured SDK errors:
AvatarSDKError and SessionTokenError expose these fields:
code- stable SDK error codemessage- human-readable messagephase- failure phase such assession_token,websocket_connect,websocket_handshake,websocket_runtime, orwebsocket_sendhttp_status- HTTP status for token or WebSocket upgrade failuresserver_code- server-provided error code, including runtime protobufServerError.codeserver_title/server_detail- parsed server error details when availableconnection_id/req_id- server correlation identifiers when availableraw_body- raw HTTP rejection body for token or WebSocket upgrade failuresclose_code/close_reason- WebSocket close details for unexpected disconnects
Common AvatarSDKErrorCode values
sessionTokenExpired- session token expired or unauthorizedsessionTokenInvalid- invalid or empty session tokenappIDUnrecognized- App ID is not recognized by the serverappIDMismatch- session token belongs to a different appavatarNotFound- avatar does not existbillingRequired- session denied by billing checkscreditsExhausted- runtime or connect-time credits exhaustedsessionDurationExceeded- billing-enforced session timeout reachedunsupportedSampleRate- handshake rejected unsupported audio sample rateinvalidEgressConfig- LiveKit or Agora egress config is invalidegressUnavailable- egress service is unavailable or not configuredidleTimeout- server closed the session after input inactivityupstreamError- internal upstream service failedprotocolError- invalid protobuf or unexpected message sequenceconnectionFailed- transport-level connection failureconnectionClosed- unexpected WebSocket closeserverError- server-side failure that did not match a more specific mappinginvalidRequest- other client-side request validation errorsunknown- fallback when the SDK cannot classify the failure
API Reference
AvatarSession
Main class for managing avatar sessions.
Methods
async init()- initialize the session and obtain a tokenasync start() -> str- start the WebSocket connection and return aconnection_idasync send_audio(audio: bytes, end: bool = False) -> str- send audio data and return a request IDasync interrupt() -> str- interrupt current audio processing in LiveKit egress modeasync close()- close the session and clean up resourcesconfig -> SessionConfig- current session configuration
SessionConfig
Configuration dataclass for avatar sessions.
Fields
avatar_id: str- avatar identifierapi_key: str- API key for authenticationapp_id: str- application identifieruse_query_auth: bool- send WebSocket auth in query params instead of headersexpire_at: datetime- session expiration timesample_rate: int- audio sample rate, default16000bitrate: int- audio bitrate, default0transport_frames: Callable[[bytes, bool], None]- frame callbackon_error: Callable[[Exception], None]- error callbackon_close: Callable[[], None]- close callbackconsole_endpoint_url: str- Console API URLingress_endpoint_url: str- ingress WebSocket URLlivekit_egress: Optional[LiveKitEgressConfig]- LiveKit egress configuration
LiveKitEgressConfig
Configuration for streaming to a LiveKit room.
Fields
url: str- LiveKit server URL, for examplewss://livekit.example.comapi_key: str- LiveKit API keyapi_secret: str- LiveKit API secretroom_name: str- room name to joinpublisher_id: str- publisher identity in the roomextra_attributes: dict[str, str]- extra participant attributesidle_timeout: int- idle timeout in seconds,0uses server defaults
Utility Functions
generate_log_id() -> str- generate a unique log ID in the formatYYYYMMDDHHMMSS_<nanoid>
Exceptions
AvatarSDKError- structured SDK error with stable code and context fieldsSessionTokenError- subclass ofAvatarSDKErrorraised when session token creation fails

