Commit c94bb03
Add Nemotron-ASR streaming inference to Python SDK (#612)
## Add Nemotron-ASR streaming inference to Python SDK
### Description
Adds real-time audio streaming support to the Foundry Local Python SDK,
enabling live microphone-to-text transcription via ONNX Runtime GenAI's
StreamingProcessor API (Nemotron ASR).
This is the Python port of C# PR #485 with full feature parity. The
existing `AudioClient` only supports file-based transcription. This PR
introduces `LiveAudioTranscriptionSession` that accepts continuous PCM
audio chunks (e.g., from a microphone) and returns partial/final
transcription results as a synchronous generator.
### What's included
**New files**
- `src/openai/live_audio_transcription_client.py` — Streaming session
with `start()`, `append()`, `get_transcription_stream()`, `stop()`
- `src/openai/live_audio_transcription_types.py` —
`LiveAudioTranscriptionResponse` (ConversationItem-shaped),
`LiveAudioTranscriptionOptions`, `CoreErrorResponse`,
`TranscriptionContentPart`
- `test/openai/test_live_audio_transcription.py` — 22 unit tests for
deserialization, settings, state guards, streaming pipeline
- `test/openai/test_live_audio_transcription_e2e.py` — E2E test with
real native DLLs and nemotron model
- `test/openai/conftest.py` — DLL preload for E2E tests
- `samples/python/live-audio-transcription/src/app.py` — Live microphone
transcription demo
**Modified files**
- `src/openai/audio_client.py` — Added
`create_live_transcription_session()` factory method
- `src/detail/core_interop.py` — Added `StreamingRequestBuffer` struct,
`execute_command_with_binary()`, `start_audio_stream`,
`push_audio_data`, `stop_audio_stream` methods, and `_load_dll_win()`
for robust DLL loading on Windows
- `src/openai/__init__.py` — Exported new live transcription types
- `test/conftest.py` — Pre-load ORT/GenAI DLLs before brotli import to
avoid Windows DLL search conflicts
### API surface
```python
audio_client = model.get_audio_client()
session = audio_client.create_live_transcription_session()
session.settings.sample_rate = 16000
session.settings.channels = 1
session.settings.language = "en"
session.start()
# Push audio from microphone callback (thread-safe)
session.append(pcm_bytes)
# Read results as synchronous generator
for result in session.get_transcription_stream():
print(result.content[0].text)
session.stop()
```
### C# parity
| C# API | Python API | Notes |
|---|---|---|
| `CreateLiveTranscriptionSession()` |
`create_live_transcription_session()` | ✅ |
| `StartAsync(ct)` | `start()` | Sync (matches Python SDK convention) |
| `AppendAsync(ReadOnlyMemory<byte>, ct)` | `append(bytes)` |
Thread-safe, copies data |
| `GetTranscriptionStream()` | `get_transcription_stream()` | Generator
(sync equivalent of IAsyncEnumerable) |
| `StopAsync(ct)` | `stop()` | Drains push queue, sends native stop,
surfaces final result |
| `IAsyncDisposable` | Context manager (`with`) | Idiomatic Python
equivalent |
| `LiveAudioTranscriptionOptions` | `LiveAudioTranscriptionOptions` |
Same fields: sample_rate, channels, bits_per_sample, language,
push_queue_capacity |
| `LiveAudioTranscriptionResponse` | `LiveAudioTranscriptionResponse` |
ConversationItem-shaped: content[0].text/transcript, is_final,
start_time, end_time |
### Design highlights
- **Output type alignment** — `LiveAudioTranscriptionResponse` uses the
OpenAI Realtime `ConversationItem` shape (`content[0].text/transcript`)
for forward compatibility
- **Internal push queue** — Bounded `queue.Queue` serializes audio
pushes from any thread (safe for mic callbacks) with backpressure
- **Fail-fast on errors** — Push loop terminates immediately on any
native error (no retry logic)
- **Settings freeze** — Audio format settings are snapshot-copied at
`start()` and immutable during the session
- **Buffer copy** — `append()` copies input data to avoid issues with
callers reusing buffers (e.g., PyAudio)
- **Routes through existing exports** — `start_audio_stream` and
`stop_audio_stream` route through `execute_command`; `push_audio_data`
routes through `execute_command_with_binary` — no new native entry
points required
- **DLL loading fix** — Uses `LoadLibraryExW` with
`LOAD_WITH_ALTERED_SEARCH_PATH` on Windows to prevent conflicts with
stale system-level ORT DLLs
### Verified working
- ✅ 22 unit tests passing (deserialization, settings, state guards,
streaming pipeline with mocked core)
- ✅ E2E test passing (SDK → Core.dll → onnxruntime-genai.dll →
onnxruntime.dll with nemotron model)
- ✅ Full session lifecycle: start → push synthetic PCM → stop → verify
results
- ✅ Existing tests unaffected
---------
Co-authored-by: ruiren_microsoft <ruiren@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent ec94cba commit c94bb03
6 files changed
Lines changed: 973 additions & 1 deletion
File tree
- sdk/python
- src
- detail
- openai
- test/openai
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
49 | 66 | | |
50 | 67 | | |
51 | 68 | | |
| |||
173 | 190 | | |
174 | 191 | | |
175 | 192 | | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
176 | 203 | | |
177 | 204 | | |
178 | 205 | | |
| |||
295 | 322 | | |
296 | 323 | | |
297 | 324 | | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
298 | 385 | | |
299 | 386 | | |
300 | 387 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
10 | 17 | | |
11 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
61 | 62 | | |
62 | 63 | | |
63 | 64 | | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
64 | 84 | | |
65 | 85 | | |
66 | 86 | | |
| |||
0 commit comments