Skip to main content

SpatialReal Python SDK

pip install spatialreal — realtime avatar sessions against the SpatialReal backend: stream audio in, get lifecycle signals back, and (in egress mode) have the avatar's synchronized audio/video published straight into a LiveKit room or Agora channel.

Usage

from datetime import datetime, timedelta, timezone
from spatialreal import LiveKitEgressConfig, new_avatar_session

session = new_avatar_session(
    api_key="...",
    app_id="...",
    avatar_id="...",
    expire_at=datetime.now(timezone.utc) + timedelta(hours=1),
    sample_rate=16000,
    livekit_egress=LiveKitEgressConfig(url=..., api_token=..., room_name=..., publisher_id=...),
    on_playback=lambda sig: print(sig.req_id, "ended" if sig.end else "playing"),
    on_error=lambda err: print("error:", err, "retryable:", getattr(err, "retryable", None)),
    on_close=lambda: print("closed"),
)
await session.init()  # API key -> session token (console API)
await session.start()  # WebSocket + handshake; read loop starts

req_id = await session.send_audio(pcm_bytes)  # same id until end=True
await session.send_audio(b"", end=True)  # closes the segment
await session.interrupt()  # interrupts the latest request
await session.close()

Pause / resume (egress mode)

When the server declares the playback_control capability, the current segment can be paused and resumed instead of interrupted — the server holds its send cursor with zero regeneration:

await session.pause()  # server stops writing frames, keeps ingesting audio
# ...keep sending audio, including end=True, while paused...
await session.resume()  # continues from where it stopped

Feature-detect with "playback_control" in session.capabilities. Subscribe to on_playback_state(PlaybackStateEvent) for the resulting state (PLAYING / PAUSED / ENDED / INTERRUPTED with played_ms and, on interrupt, a reason such as pause_timeout). Older servers never send it and ignore pause/resume.

Session objects are single-use: on a dropped connection, create a fresh one (request state is deliberately not reusable across connections).

Endpoints

Endpoints come from the environment you name, so callers write no URLs:

session = new_avatar_session(..., environment="us-west")  # the default

They are resolved from SpatialReal's config service (built-in presets cover it being unreachable), which is what lets a deployment move, or a region be added, without an SDK release. Pass console_endpoint_url / ingress_endpoint_url to override either one — useful against a local or private deployment.

AvatarSDKError.retryable tells you whether reconnecting can succeed — it implements the server's WebSocket close-code contract (40xx: don't retry, 45xx: retry with backoff).

What's new

  • on_playback(PlaybackSignal) — structured playback lifecycle; no protobuf parsing in caller code (transport_frames(raw, is_last) still exists).
  • CloseCode + AvatarSDKError.retryable — the reconnect contract as API.
  • environment="us-west" — endpoints resolved from the config service instead of hard-coded URLs; new regions need no SDK release.
  • session.capabilities — server-declared capabilities from the handshake (feature-detect, don't version-detect).
  • pause() / resume() + on_playback_state(PlaybackStateEvent) — egress-mode server-side playback control (gated on the playback_control capability).
  • on_close fires exactly once per session; the token request has a timeout.
  • AudioFormat.OGG_OPUS takes pre-encoded bytes; the SDK does not encode client-side.

Development

uv venv && uv pip install -e ".[dev]"
python -m pytest tests -q     # protocol-level tests against an in-process fake backend

License

Apache-2.0

Metadata

Release files for spatialreal 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for spatialreal 0.2.2
File Size Uploaded
spatialreal-0.2.2.tar.gz 22.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for spatialreal 0.2.2
File Interpreter ABI Platform
spatialreal-0.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 50.6 kB

Release files / spatialreal-0.2.2.tar.gz

Download URL spatialreal-0.2.2.tar.gz
Size 22.9 kB
Tags Source
SHA-256 checksum
How to use checksums
8158f9e9128be8ffec4f27d1ee5b1669c62f820c9b1dd8cf9515860c40d0037d
BLAKE2b-256 checksum
How to use checksums
9bd7f0375dbb528c2c0e8ee47ceb8577622cb709d2336361766e9d543cfde931
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 15, 2026.

Transparency log

Release files / spatialreal-0.2.2-py3-none-any.whl

Download URL spatialreal-0.2.2-py3-none-any.whl
Size 27.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c0f4d34ae89b9c26b5635df5845730f79f325c3f6fcd9f31ee546e65a16d1121
BLAKE2b-256 checksum
How to use checksums
97138079c14edc9a32e778f3abecf8b0cc2c0c69f8a9528f751949d68ecde794
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 15, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.2 This release

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page