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="...",
    console_endpoint_url="https://api.spatialreal.cloud",  # OpenAPI root: session tokens
    ingress_endpoint_url="wss://driven.us-west.spatialreal.cloud/v2/driveningress",
    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). 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.
  • 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.1.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.1.0
File Size Uploaded
spatialreal-0.1.0.tar.gz 26.8 kB Details

Built distribution (wheel)

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

Total release size: 53.3 kB

Release files / spatialreal-0.1.0.tar.gz

Download URL spatialreal-0.1.0.tar.gz
Size 26.8 kB
Tags Source
SHA-256 checksum
How to use checksums
1685a349ee7d40f861e433cc9be7dff653e16d2f5b009241a661ffee54c3afaa
BLAKE2b-256 checksum
How to use checksums
a552c6ae45c56db6414dd098246fb7e62e3af775cfaf0438f24859f184053ff7
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 Sep 30, 2026.

Transparency log

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

Download URL spatialreal-0.1.0-py3-none-any.whl
Size 26.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f24d1d48b1306e13ccc010c783abcccf63b73c011aadcbb843c35840b4ecf676
BLAKE2b-256 checksum
How to use checksums
f90d3a286221cd9a0c0e3bcef7bfb2a01955b76e16fec7974b6a705ccdb02469
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 Sep 30, 2026.

Transparency log

Release history Release notifications | RSS feed

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

This release

0.1.0 This release

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