You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -699,79 +699,187 @@ for result in transcript.auto_highlights.results:
699
699
700
700
### **Streaming Examples**
701
701
702
-
[Read more about our streaming service.](https://www.assemblyai.com/docs/streaming/universal-3-pro)
702
+
Real-time speech-to-text via WebSocket against the `u3-rt-pro` model. The SDK ships two clients with identical option/event/handler surfaces — `StreamingClient` (threaded) and `AsyncStreamingClient` (asyncio). Pick whichever fits your codebase.
703
+
704
+
**Handler contract**: every handler is called as `handler(client, event)`. Plain functions and `async def` functions both work; `AsyncStreamingClient` awaits async handlers inline on the read task, so don't block — use `asyncio.create_task(...)` if you need concurrent work.
705
+
706
+
[Read more about the streaming service.](https://www.assemblyai.com/docs/streaming/universal-3-pro)
`AsyncStreamingClient` mirrors `StreamingClient` with async methods. It's safe to use as an async context manager — `disconnect()` runs on block exit even if user code raises. Don't pass `extras.stream_file` directly (it uses blocking `time.sleep`); pace from an async generator instead.
Server-side errors arrive on the `Error` event rather than being raised. The handler receives a `StreamingError` (an `Exception` subclass) with `.code: int | None` — **not** the wire `ErrorEvent` class.
815
+
816
+
`StreamingErrorCodes` is a `dict[int, str]` mapping wire codes to human-readable messages. Use `.get(...)` for lookup:
817
+
818
+
```python
819
+
from assemblyai.streaming.v3 import StreamingErrorCodes
Common codes: `4001` Not Authorized, `4002` Insufficient Funds, `4029` Client sent audio too fast, `4031` Session idle for too long.
827
+
828
+
</details>
829
+
830
+
<details>
831
+
<summary>Change settings mid-session</summary>
832
+
833
+
`set_params` updates an active session. Typical use: enable turn formatting (punctuation, casing) only on confirmed end-of-turn so partial transcripts stay raw:
834
+
835
+
```python
836
+
from assemblyai.streaming.v3 import StreamingSessionParameters
837
+
838
+
defon_turn(client, event):
839
+
if event.end_of_turn andnot event.turn_is_formatted:
For voice agents, `force_endpoint()` flushes the current turn — useful when an external signal (UI button, barge-in detection) determines the user has stopped speaking before VAD does:
844
+
845
+
```python
846
+
client.force_endpoint() # ends the current turn immediately
847
+
```
848
+
849
+
</details>
850
+
851
+
<details>
852
+
<summary>Temporary tokens for browser / edge clients</summary>
853
+
854
+
Don't ship your API key to browsers. Mint a short-lived token server-side and pass it to the client.
# Send `token` to the browser, which connects with options(token=token).
861
+
```
862
+
863
+
**Async server (FastAPI / asyncio):** always wrap in `async with` even though you don't call `connect()` — `create_temporary_token` lazily opens an `httpx.AsyncClient` pool. The context manager closes it on exit; without it you leak a pool every request.
864
+
865
+
```python
866
+
from fastapi import FastAPI
867
+
from assemblyai.streaming.v3 import AsyncStreamingClient, StreamingClientOptions
868
+
869
+
app = FastAPI()
870
+
MASTER_KEY="<YOUR_API_KEY>"
871
+
872
+
@app.get("/streaming-token")
873
+
asyncdefstreaming_token():
874
+
asyncwith AsyncStreamingClient(StreamingClientOptions(api_key=MASTER_KEY)) as client:
0 commit comments