stt

Gradium’s speech-to-text service implementation.

This module provides integration with Gradium’s real-time speech-to-text WebSocket API for streaming audio transcription, with optional turn detection from the server’s end-pointing signal.

pipecat.services.gradium.stt.language_to_gradium_language(language: Language | str) → str[source]

Convert a Language enum to Gradium’s language code format.

Parameters:

language – The Language enum value to convert, or "any".

Returns:

The corresponding Gradium language code. If language is not in the verified mapping, falls back to the base language code (e.g., en from en-US) and logs a warning (via resolve_language(..., use_base_code=True)).

class pipecat.services.gradium.stt.GradiumSTTSettings(model: str | None | NotGiven = <factory>, extra: dict[str, ~typing.Any]=<factory>, language: Language | str | None | NotGiven = <factory>, delay_in_frames: int | None | NotGiven = <factory>, eot_horizon_s: float | None | NotGiven = <factory>, eot_threshold: float | None | NotGiven = <factory>, post_flush_cooldown_frames: int | None | NotGiven = <factory>)[source]

Bases: STTSettings

Settings for GradiumSTTService.

The eot_* and post_flush_cooldown_frames fields tune turn detection. They are None unless the service is constructed with enable_turn_detection=True, which gives them their defaults.

Parameters:
  • delay_in_frames – Delay in audio frames (80ms each) before text is generated. Higher delays allow more context but increase latency. Allowed values: 7, 8, 10, 12, 14, 16, 20, 24, 36, 48. Default is 12 (960ms). Lower values like 7-8 give faster response.

  • eot_horizon_s – Which end-pointing horizon drives turn decisions; the step entry with the closest horizon_s is used. Default 3.0.

  • eot_threshold – Inactivity probability on that horizon at or above which an open turn ends, and below which a turn opens once the signal has read inactive. Default 0.5.

  • post_flush_cooldown_frames – Number of step messages to ignore after a flush completes before the next turn may start or end. A flush feeds the model delay_in_frames of silence, and the end-pointing signal is unreliable on the frames that follow: it can dip as if speech resumed, then fire again on the first real frames. Default 8.

class pipecat.services.gradium.stt.GradiumSTTService(*, api_key: str, api_endpoint_base_url: str = 'wss://api.gradium.ai/api/speech/asr', encoding: str = 'pcm', sample_rate: int | None = None, params: InputParams | None = None, json_config: str | None = None, enable_turn_detection: bool = False, settings: GradiumSTTSettings | None = None, ttfs_p99_latency: float | None = 0.62, **kwargs)[source]

Bases: WebsocketSTTService

Gradium real-time speech-to-text service.

Provides real-time speech transcription using Gradium’s WebSocket API. Supports both interim and final transcriptions with configurable parameters for audio processing and connection management.

Transcribes English by default. Set settings.language to one of the other supported languages (German, Spanish, French, Portuguese), or to "any" to have Gradium detect the language.

By default the pipeline’s VAD closes each utterance: a VADUserStoppedSpeakingFrame flushes the server and the accumulated text finalizes into a TranscriptionFrame.

With enable_turn_detection=True the server’s end-pointing signal drives turns instead. Every “step” message carries, per horizon, the probability that speech stays inactive over the next horizon_s seconds, and the service watches the horizon closest to eot_horizon_s:

step(inactivity >= threshold) -> step(inactivity < threshold: turn opens)
    -> text* -> step(inactivity >= threshold: turn ends)
    -> ProposedUserStoppedSpeakingFrame -> flush -> TranscriptionFrame
    -> step(inactivity < threshold: next turn opens)

A connection’s first turn opens on the signal falling below the threshold, not on it being there: the estimate reads low on the first steps, before the model has heard anything, then climbs as silence accumulates. After a flush the signal follows the audio, so once a turn’s transcript is pushed the next step below the threshold opens a turn, and speech that continues through the flush starts one.

A turn start broadcasts a ProposedUserStartedSpeakingFrame; a turn end broadcasts a ProposedUserStoppedSpeakingFrame and flushes the server, and the final TranscriptionFrame follows once the flush is acknowledged. Local VAD frames are ignored, and service_metadata_frame() recommends ExternalUserTurnStrategies, which resolve the proposals into the user turn frames, own the interruption, and hold the turn open until that transcript arrives.

Event handlers available (in addition to on_connected / on_disconnected), fired only with turn detection on:

  • on_turn_start(service): the end-pointing signal opened a turn

  • on_turn_end(service): the end-pointing signal closed the turn

Example:

stt = GradiumSTTService(
    api_key="...",
    enable_turn_detection=True,
    settings=GradiumSTTService.Settings(eot_horizon_s=3.0, eot_threshold=0.5),
)
Settings

alias of GradiumSTTSettings

class InputParams(*, language: Language | None = None, delay_in_frames: int | None = None)[source]

Bases: BaseModel

Configuration parameters for Gradium STT API.

Deprecated since version 0.0.105: Use settings=GradiumSTTService.Settings(...) instead. Will be removed in 2.0.0.

Parameters:
  • language – Expected language of the audio (e.g., “en”, “es”, “fr”). This helps ground the model to a specific language and improve transcription quality. Defaults to Language.EN; "any" asks Gradium to detect the language.

  • delay_in_frames – Delay in audio frames (80ms each) before text is generated. Higher delays allow more context but increase latency. Allowed values: 7, 8, 10, 12, 14, 16, 20, 24, 36, 48. Default is 10 (800ms). Lower values like 7-8 give faster response.

language: Language | None
delay_in_frames: int | None
__init__(*, api_key: str, api_endpoint_base_url: str = 'wss://api.gradium.ai/api/speech/asr', encoding: str = 'pcm', sample_rate: int | None = None, params: InputParams | None = None, json_config: str | None = None, enable_turn_detection: bool = False, settings: GradiumSTTSettings | None = None, ttfs_p99_latency: float | None = 0.62, **kwargs)[source]

Initialize the Gradium STT service.

Parameters:
  • api_key – Gradium API key for authentication.

  • api_endpoint_base_url – WebSocket endpoint URL.

  • encoding – Base audio encoding type. One of “pcm”, “wav”, or “opus”. For PCM, the sample rate is appended automatically from the pipeline’s audio_in_sample_rate (e.g., “pcm” becomes “pcm_16000”). Audio at a rate Gradium does not accept is resampled to one it does. Defaults to “pcm”.

  • sample_rate – Audio sample rate in Hz. If None, uses the pipeline sample rate.

  • params –

    Configuration parameters for language and delay settings.

    Deprecated since version 0.0.105: Use settings=GradiumSTTService.Settings(...) instead. Will be removed in 2.0.0.

  • json_config –

    Optional JSON configuration string for additional model settings.

    Deprecated since version 0.0.101: Use params instead for type-safe configuration. Will be removed in 2.0.0.

  • enable_turn_detection – Whether the server’s end-pointing signal decides when user turns start and end, instead of the pipeline’s VAD. Off by default.

  • settings – Runtime-updatable settings. When provided alongside deprecated parameters, settings values take precedence.

  • ttfs_p99_latency – P99 latency from speech end to final transcript in seconds. Override for your deployment. See https://github.com/pipecat-ai/stt-benchmark

  • **kwargs – Additional arguments passed to parent STTService class.

can_generate_metrics() → bool[source]

Check if the service can generate metrics.

Returns:

True if metrics generation is supported.

property send_sample_rate: int

Get the rate audio is sent at, one Gradium takes for PCM encoding.

Returns:

The sample rate in Hz.

property supports_ttfs: bool

TTFS applies only while the pipeline’s VAD ends utterances.

With turn detection on, the server defines the turn boundary, so there is no separate speech-end to final-transcript interval to measure.

service_metadata_frame() → STTMetadataFrame[source]

Recommend external turn strategies when turns are detected server-side.

With turn detection on, the service proposes turn boundaries (ProposedUserStarted/StoppedSpeakingFrame), so the user aggregator resolves those rather than running local VAD/smart-turn. Otherwise the defaults are left in place. Applied unless the user passed their own user_turn_strategies.

async setup(setup: FrameProcessorSetup)[source]

Set up the service and connect.

Parameters:

setup – Configuration object containing setup parameters.

async stop(frame: EndFrame)[source]

Stop the speech-to-text service.

Parameters:

frame – End frame to stop processing.

async cancel(frame: CancelFrame)[source]

Cancel the speech-to-text service.

Parameters:

frame – Cancel frame to abort processing.

async process_frame(frame: Frame, direction: FrameDirection)[source]

Process incoming frames and handle speech events.

A VAD stop flushes the server, unless turn detection is on: then the server’s end-pointing signal decides when a turn ends.

Parameters:
  • frame – The frame to process.

  • direction – Direction of frame flow in the pipeline.

async run_stt(audio: bytes) → AsyncGenerator[Frame | None, None][source]

Process audio data for speech-to-text conversion.

Parameters:

audio – Raw audio bytes to process.

Yields:

None (processing handled via WebSocket messages).

async push_frame(frame: Frame, direction: FrameDirection = FrameDirection.DOWNSTREAM)

Push a frame downstream, tracking TranscriptionFrame timestamps for TTFB.

Stores the timestamp of each TranscriptionFrame for TTFB calculation. If the frame is marked as finalized (via request_finalize/confirm_finalize), reports TTFB immediately and cancels any pending timeout. Otherwise, TTFB is reported after a timeout.

Parameters:
  • frame – The frame to push.

  • direction – The direction to push the frame.

async start_stt_usage_metrics(usage: STTUsage)

Start STT usage metrics collection.

Parameters:

usage – Usage information for the STT operation.

async stop_ttfb_metrics(*, end_time: float | None = None)

Stop time-to-first-byte metrics collection and push results.

Parameters:

end_time – Optional timestamp to use as the end time. If None, uses the current time.