mstar.api_server.openai.serving_realtime#

/v1/realtime (transcription intent): streaming speech-to-text over a WebSocket.

A subset of OpenAI’s Realtime transcription session, enough for a client that streams microphone audio and wants partial transcripts:

Client events

transcription_session.update {"session": {"input_audio_transcription": {"model", "language", "prompt"}, "input_audio_format": "pcm16", "mstar": {"chunk_seconds", "unfixed_chunks", "unfixed_tokens"}}} input_audio_buffer.append {"audio": <base64 PCM16 mono 16 kHz>} input_audio_buffer.commit end of the utterance: flush and finish input_audio_buffer.clear drop buffered audio and start over

Server events

transcription_session.created / .updated conversation.item.input_audio_transcription.delta append-only text mstar.transcription.partial the whole current hypothesis, unstable tail included conversation.item.input_audio_transcription.completed {"transcript"} error

The decoding follows the Qwen3-ASR SDK’s streaming algorithm: every chunk_seconds of new audio, the audio heard so far is transcribed again as one engine request whose assistant turn is prefilled with the previous hypothesis minus its last unfixed_tokens tokens (once unfixed_chunks chunks are in), so the tail can still be revised while the head is fixed. The adapter decides how that prefix reaches its model (realtime_step_request). Each step is an ordinary request, so sessions batch on the engine like any other traffic.

Functions

pcm16_to_float(payload)

Base64 PCM16 (little-endian, mono) -> float32 in [-1, 1).

stable_prefix(raw_text, tokenizer, ...)

raw_text minus its last unfixed_tokens tokens, backed off further when the cut lands inside a multi-byte character.

Classes

RealtimeSession(session_id, request[, ...])

One WebSocket's transcription state (the SDK's ASRStreamingState).

RealtimeTranscription(api, model_name, ...)

Drives one WebSocket session against the API server.

class mstar.api_server.openai.serving_realtime.RealtimeSession(session_id, request, chunk_seconds=2.0, unfixed_chunks=2, unfixed_tokens=5, pending=<factory>, pending_samples=0, heard=<factory>, chunk_id=0, raw_hypothesis='', emitted='', item_id=<factory>)[source]#

Bases: object

One WebSocket’s transcription state (the SDK’s ASRStreamingState).

Parameters:
append(audio)[source]#
Parameters:

audio (ndarray)

Return type:

None

audio_so_far()[source]#
Return type:

ndarray

chunk_id: int = 0#
property chunk_samples: int#
chunk_seconds: float = 2.0#
clear()[source]#
Return type:

None

emitted: str = ''#
flush()[source]#
Return type:

ndarray | None

heard: list[ndarray]#
item_id: str#
pending: list[ndarray]#
pending_samples: int = 0#
raw_hypothesis: str = ''#
request: TranscriptionRequest#
session_id: str#
take_chunk()[source]#

A full chunk of pending audio, or None until enough arrived.

Return type:

ndarray | None

unfixed_chunks: int = 2#
unfixed_tokens: int = 5#
class mstar.api_server.openai.serving_realtime.RealtimeTranscription(api, model_name, adapter, websocket)[source]#

Bases: object

Drives one WebSocket session against the API server.

Parameters:
  • model_name (str)

  • websocket (WebSocket)

current_text()[source]#
Return type:

str

async emit(text, hold)[source]#

Send the part of text past what was already sent, minus the last hold characters. A revision that reaches into sent text is not resent — deltas are append-only — the partial event carries it.

Parameters:
Return type:

None

async handle(event)[source]#
Parameters:

event (dict)

Return type:

None

async run()[source]#
Return type:

None

async send(event)[source]#
Parameters:

event (dict)

Return type:

None

async step(chunk, final=False)[source]#

Hear chunk, re-decode everything so far from the stable prefix, then send the newly firm text and the whole hypothesis.

Parameters:
  • chunk (ndarray)

  • final (bool)

Return type:

None

update(session)[source]#

Apply a session update, all or nothing.

Parameters:

session (dict)

Return type:

None

mstar.api_server.openai.serving_realtime.pcm16_to_float(payload)[source]#

Base64 PCM16 (little-endian, mono) -> float32 in [-1, 1). Strict decoding: a malformed payload is an error, not silence.

Parameters:

payload (str)

Return type:

ndarray

mstar.api_server.openai.serving_realtime.stable_prefix(raw_text, tokenizer, unfixed_tokens)[source]#

raw_text minus its last unfixed_tokens tokens, backed off further when the cut lands inside a multi-byte character.

Parameters:
  • raw_text (str)

  • unfixed_tokens (int)

Return type:

str