UNPKG

ai

Version:

AI SDK by Vercel - build apps like ChatGPT, Claude, Gemini, and more with a single interface for any model using the Vercel AI Gateway or go direct to OpenAI, Anthropic, Google, or any other model provider.

170 lines (157 loc) • 5.76 kB
--- title: transcribe description: API Reference for transcribe. --- # `transcribe()` Generates a transcript from an audio file. ```ts import { transcribe } from 'ai'; import { openai } from '@ai-sdk/openai'; import { readFile } from 'fs/promises'; const { text: transcript } = await transcribe({ model: openai.transcription('gpt-4o-transcribe'), audio: await readFile('audio.mp3'), }); console.log(transcript); ``` ## Import <Snippet text={`import { transcribe } from "ai"`} prompt={false} /> ## API Signature ### Parameters <PropertiesTable content={[ { name: 'model', type: 'TranscriptionModelV4', description: 'The transcription model to use.', }, { name: 'audio', type: 'DataContent (string | Uint8Array | ArrayBuffer | Buffer) | URL', description: 'The audio file to generate the transcript from. HTTP(S) URLs are sent directly to models with native URL support unless a custom download function is supplied; other models download the audio locally.', }, { name: 'timestampGranularities', type: "Array<'word' | 'segment'>", isOptional: true, description: 'Word and segment timings to request. Supported by the OpenAI, Azure Speech, Vertex Gemini, and xAI adapters. Explicit provider options take precedence; these adapters warn about unavailable requested timings. AI Gateway warns that it does not forward this option; use provider options or a direct provider model. Omit this option to keep provider defaults. An empty array requests no additional timing detail; it does not suppress timings returned by default.', }, { name: 'providerOptions', type: 'Record<string, JSONObject>', isOptional: true, description: 'Additional provider-specific options.', }, { name: 'maxRetries', type: 'number', isOptional: true, description: 'Maximum number of retries. Default: 2.', }, { name: 'abortSignal', type: 'AbortSignal', isOptional: true, description: 'An optional abort signal to cancel the call.', }, { name: 'headers', type: 'Record<string, string>', isOptional: true, description: 'Additional HTTP headers for the request.', }, { name: 'telemetry', type: 'TelemetryOptions', isOptional: true, description: 'Telemetry configuration. Supports functionId, recordInputs, recordOutputs, isEnabled, and per-call integrations.', }, { name: 'download', type: '(options: { url: URL; abortSignal?: AbortSignal }) => Promise<{ data: Uint8Array; mediaType: string | undefined }>', isOptional: true, description: 'Custom download function for fetching audio from URLs. Supplying this option forces local downloading even for models with native URL support. Use `createDownload()` from `ai` to create a download function with a custom size limit (e.g. `createDownload({ maxBytes: 50 * 1024 * 1024 })`) or fetch transport (e.g. `createDownload({ fetch })` with `guarded-fetch` on Node.js for untrusted URLs). Default: native URL transcription when supported, otherwise built-in download with 2 GiB limit.', }, ]} /> ### Returns <PropertiesTable content={[ { name: 'text', type: 'string', description: 'The complete transcribed text from the audio input.', }, { name: 'segments', type: 'Array<{ text: string; startSecond: number; endSecond: number }>', description: 'An array of transcript segments, each containing a portion of the transcribed text along with its start and end times in seconds.', }, { name: 'words', type: 'Array<{ text: string; startSecond: number; endSecond: number }>', isOptional: true, description: 'Word-level timings when the adapter exposes them, including timings returned by default. Undefined when unavailable. Existing segments behavior is preserved: some providers also put word timings in segments.', }, { name: 'language', type: 'string | undefined', description: 'The language of the transcript in ISO-639-1 format e.g. "en" for English.', }, { name: 'durationInSeconds', type: 'number | undefined', description: 'The duration of the transcript in seconds.', }, { name: 'warnings', type: 'Warning[]', description: 'Warnings from the model provider (e.g. unsupported settings).', }, { name: 'providerMetadata', type: 'Record<string, JSONObject>', isOptional: true, description: 'Optional metadata from the provider. The outer key is the provider name. The inner values are the metadata. Details depend on the provider.', }, { name: 'responses', type: 'Array<TranscriptionModelResponseMetadata>', description: 'Response metadata from the provider. There may be multiple responses if we made multiple calls to the model.', properties: [ { type: 'TranscriptionModelResponseMetadata', parameters: [ { name: 'timestamp', type: 'Date', description: 'Timestamp for the start of the generated response.', }, { name: 'modelId', type: 'string', description: 'The ID of the response model that was used to generate the response.', }, { name: 'headers', type: 'Record<string, string>', isOptional: true, description: 'Response headers.', }, ], }, ], }, ]} />