TranscriptionModel unstable
Defines the provider-neutral service for speech-to-text.
A TranscriptionModel turns a finished recording into text with
transcribe, and a live audio stream into segment updates with
transcribeLive. Failures are reported as AiError values. A language
hint is the only portable option; vocabulary hints, prompts, and other
settings belong to each provider. transcribeLiveBuffered wraps the live
operation so audio survives a dropped connection.
Constructors
Creates a TranscriptionModel service from a provider implementation.
Details
A live session lasts as long as the stream transcribeLive returns. When
audio ends, the provider must flush, emit the remaining final updates, and
complete the stream. Interrupting the stream must close the session. A
dropped connection must fail the stream instead of reconnecting.
Gotchas
Without a provider transcribeLive, the service's transcribeLive fails
with AiError.InvalidUserInputError.
See
- TranscriptionModel for the service shape returned by this constructor
- ProviderResponse for the provider response contract consumed by this constructor
Signature
declare function make(params: { readonly transcribe: (options: TranscribeOptions) => Effect<ProviderResponse, AiError>; readonly transcribeLive?: <E, R>(options: TranscribeLiveOptions<E, R>) => Stream<SegmentUpdate, AiError | E, R>;}): Effect<TranscriptionModel>Models
Notice that transcribeLiveBuffered dropped buffered audio to stay within
its maximum duration. The dropped audio cannot be sent again after a
reconnect.
Signature
interface AudioDiscarded { readonly _tag: "AudioDiscarded"; readonly duration: Duration;}The format of a raw audio stream.
Details
"pcm16" is signed 16-bit little-endian PCM. "mulaw" and "alaw" are
8-bit G.711. Channels are interleaved.
Signature
interface AudioFormat { readonly channels: number; readonly encoding: "pcm16" | "mulaw" | "alaw"; readonly sampleRate: number;}Example Declaring 24 kHz mono PCM16
import type { TranscriptionModel } from "effect/ai"
const format: TranscriptionModel.AudioFormat = { encoding: "pcm16", sampleRate: 24000, channels: 1}Provider response for a transcribed recording. usage defaults to no
reported values and metadata defaults to {}.
Signature
interface ProviderResponse { readonly duration?: Duration; readonly language?: string; readonly metadata?: TranscribeResponseMetadata; readonly segments?: readonly Array<Segment>; readonly text: string; readonly usage?: { readonly audioDuration?: Duration; readonly inputTokens?: number; readonly outputTokens?: number; };}A finished audio recording. data is a base64 string, a byte array, or a
URL.
Gotchas
Providers do not accept every representation. A provider that cannot take
the one given fails with AiError.InvalidUserInputError.
Signature
interface Recording { readonly data: string | Uint8Array<ArrayBufferLike> | URL; readonly fileName?: string; readonly mediaType: string;}A timestamped part of a transcribed recording. Times are offsets from the start of the audio.
Signature
interface Segment { readonly end: Duration; readonly start: Duration; readonly text: string;}The current state of one segment of a live transcript.
Details
text is the whole text of the segment so far, not an addition to earlier
updates. Keep one entry for each id and replace it on every update. A
segment receives no more updates once final is true.
start and end are offsets from the start of the audio and are set only
when the provider reports them.
Signature
interface SegmentUpdate { readonly _tag: "SegmentUpdate"; readonly end?: Duration; readonly final: boolean; readonly id: string; readonly start?: Duration; readonly text: string;}The transcript of a recording with usage and response metadata.
See
- TranscriptionUsage for usage metadata
Signature
interface TranscribeResponse { readonly duration?: Duration; readonly language?: string; readonly metadata: TranscribeResponseMetadata; readonly segments?: readonly Array<Segment>; readonly text: string; readonly usage: TranscriptionUsage;}Provider-specific response metadata keyed by provider name. Provider packages augment this interface with details the portable response leaves out, such as word timestamps, speaker labels, and confidence.
Signature
interface TranscribeResponseMetadata extends ProviderMetadata { [key: string]: Json;}Transcription of recordings and of live audio.
Signature
interface TranscriptionModel { readonly "~effect/ai/TranscriptionModel": "~effect/ai/TranscriptionModel"; readonly transcribe: (options: TranscribeOptions) => Effect<TranscribeResponse, AiError>; readonly transcribeLive: <E, R>(options: TranscribeLiveOptions<E, R>) => Stream<SegmentUpdate, AiError | E, R>;}TranscriptionUsage
unstableProvider-reported usage. Unreported values are undefined.
Details
Providers bill transcription by tokens, by audio duration, or both.
Signature
declare class TranscriptionUsage extends { readonly audioDuration?: Duration; readonly inputTokens?: number; readonly outputTokens?: number;} { constructor(...args: [props?: { readonly audioDuration?: Duration; readonly inputTokens?: number; readonly outputTokens?: number; }, options?: MakeOptions]);}Options
Options for transcribing live audio.
Gotchas
Audio is sent as given. Nothing resamples it, so a provider fails with
AiError.InvalidUserInputError for a format it cannot take.
Signature
interface TranscribeLiveOptions<E, R> { readonly audio: Stream<Uint8Array<ArrayBufferLike>, E, R>; readonly format: AudioFormat; readonly language?: string;}Options for transcribing a recording.
Signature
interface TranscribeOptions { readonly audio: Recording; readonly language?: string;}Services
TranscriptionModel
unstableService key for transcribing speech. Create implementations with make.
See
- transcribe for transcribing a recording through the current service
- transcribeLive for transcribing live audio through the current service
Signature
declare const TranscriptionModel: Context.Service<TranscriptionModel, TranscriptionModel>Transcription
transcribe
unstableTranscribes a finished recording using the current TranscriptionModel
service.
See
- transcribeLive for transcribing audio as it arrives
Signature
declare function transcribe(options: TranscribeOptions): Effect<TranscribeResponse, AiError, TranscriptionModel>Example Transcribing a recording
import { Effect } from "effect"import { TranscriptionModel } from "effect/ai"
const program = Effect.fnUntraced(function*(bytes: Uint8Array) { const { text, segments } = yield* TranscriptionModel.transcribe({ audio: { mediaType: "audio/wav", fileName: "call.wav", data: bytes }, language: "en" }) return { text, segments }})transcribeLive
unstableTranscribes live audio using the current TranscriptionModel service.
Details
The session lasts as long as the returned stream. When audio ends, the
provider flushes, the last final updates arrive, and the stream completes.
Interrupting the stream closes the session. The provider decides where
segments begin and end.
Gotchas
- A dropped connection fails the stream. Use
transcribeLiveBufferedto reconnect without losing audio. - A model that only transcribes recordings fails with
AiError.InvalidUserInputError.
See
- transcribeLiveBuffered for surviving a dropped connection
Signature
declare function transcribeLive<E, R>(options: TranscribeLiveOptions<E, R>): Stream<SegmentUpdate, AiError | E, TranscriptionModel | R>Example Keeping the latest text of each segment
import { Stream } from "effect"import { TranscriptionModel } from "effect/ai"
const captions = (microphone: Stream.Stream<Uint8Array>) => TranscriptionModel.transcribeLive({ audio: microphone, format: { encoding: "pcm16", sampleRate: 24000, channels: 1 } }).pipe( Stream.scan( () => new Map<string, string>(), (segments, update: TranscriptionModel.SegmentUpdate) => new Map(segments).set(update.id, update.text) ), Stream.map((segments) => Array.from(segments.values()).join(" ")) )transcribeLiveBuffered
unstableTranscribes live audio and reconnects after a dropped connection, sending again the audio the transcript may not cover yet.
Details
Audio is buffered as it arrives and the buffer is emptied each time a final
segment arrives. When the session fails with a retryable AiError, a new one
opens after the next schedule delay and receives the buffer, followed by
the audio that arrived during the outage. The schedule starts over once a
session produces an update while audio is still open. Once audio has
ended, every new session is sent the same audio, so updates no longer start
the schedule over. When it is exhausted the stream fails with the last
error.
While a session is connected, audio is pulled only as fast as the provider
takes it, so a provider that falls behind slows audio down and nothing
queues up outside the buffer. During an outage audio is pulled freely.
Segment ids are issued here, so they stay unique across sessions, and
segment times stay relative to the start of audio. Segments that were not
final when a session failed receive a last update with empty text, because
their audio is sent again and comes back under new ids.
When the buffer would exceed maxDuration, "error" fails the stream with
an AiError, and "sliding" drops the oldest audio and emits an
AudioDiscarded notice.
Gotchas
- The replay starts where the last final segment arrived, which is not where its speech ended. Speech that began before that point is not sent again, and text is not de-duplicated.
- Only final segments empty the buffer. Audio with no speech fills it too,
so a pause longer than
maxDurationoverflows it. maxDurationdoes not bound a source that cannot wait. Give a live source its own boundedStream.bufferif a slow provider must not hold it up.
See
- transcribeLive for the session this wraps
Signature
declare function transcribeLiveBuffered<E, R, Strategy extends "sliding" | "error">(options: { readonly audio: Stream<Uint8Array<ArrayBufferLike>, E, R>; readonly format: AudioFormat; readonly language?: string; readonly maxDuration: Input; readonly schedule: Schedule<unknown, AiError>; readonly strategy: Strategy;}): Stream<SegmentUpdate | Strategy extends "sliding" ? AudioDiscarded : never, AiError | E, TranscriptionModel | R>Example Reconnecting with a bounded buffer
import { Schedule, Stream } from "effect"import { TranscriptionModel } from "effect/ai"
const updates = (microphone: Stream.Stream<Uint8Array>) => TranscriptionModel.transcribeLiveBuffered({ audio: microphone, format: { encoding: "pcm16", sampleRate: 24000, channels: 1 }, maxDuration: "30 seconds", strategy: "sliding", schedule: Schedule.exponential("200 millis") }).pipe( Stream.filter((event) => event._tag === "SegmentUpdate" && event.final) )