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
./scripts/gen_proto.sh        # regenerate protobuf from proto/ (see proto/SHARED_PROTO_COMMIT)

License

Apache-2.0

Metadata

Release files for spatialreal 0.2.0

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.0
File Size Uploaded
spatialreal-0.2.0.tar.gz 28.6 kB Details

Built distribution (wheel)

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

Total release size: 57.3 kB

Release files / spatialreal-0.2.0.tar.gz

Download URL spatialreal-0.2.0.tar.gz
Size 28.6 kB
Tags Source
SHA-256 checksum
How to use checksums
16d51405133203f45746731c9c6950c4c73f24fa18589b2e3fe5d3154fcbecc8
BLAKE2b-256 checksum
How to use checksums
6082cf7dac350d4af3c3691d5d27c7d19203d9a9238e893965c80aa3832ec660
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 5, 2026.

Transparency log

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

Download URL spatialreal-0.2.0-py3-none-any.whl
Size 28.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5d3970f0cd2ac299a2b1ffbadb9d6976b1fc1137e3a6813b6ee75595735255b7
BLAKE2b-256 checksum
How to use checksums
d82513dc1c65dc4bdec5cfa1b11856c3b4ccbf48e53a3535ac49b742824d5b30
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 5, 2026.

Transparency log

Release history Release notifications | RSS feed

0.2.2

2 release files

0.2.1

2 release files

This release

0.2.0 This release

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