openai
Version:
The official TypeScript library for the OpenAI API
148 lines (134 loc) • 4.52 kB
text/typescript
// File generated from our OpenAPI spec by Castiron. See CONTRIBUTING.md for details.
import { APIResource } from '../../core/resource';
import { APIPromise } from '../../core/api-promise';
import { buildHeaders } from '../../internal/headers';
import { RequestOptions } from '../../internal/request-options';
function resolveResourceRequestOptions(
options: RequestOptions | undefined,
buildOptions: (options: RequestOptions | undefined) => RequestOptions | Promise<RequestOptions>,
): Promise<RequestOptions> {
return Promise.resolve(options).then(buildOptions);
}
/**
* Turn audio into text or text into audio.
*/
export class Speech extends APIResource {
/**
* Generates audio from the input text.
*
* Returns the audio file content, or a stream of audio events.
*
* The `tts-1`, `tts-1-hd`, `gpt-4o-mini-tts`, `gpt-4o-mini-tts-2025-03-20`, and
* `gpt-4o-mini-tts-2025-12-15` models are deprecated and scheduled to shut down on
* January 6, 2027. These examples use the existing Speech API; for new
* integrations, migrate to `gpt-realtime-2.1-mini` through the
* [Realtime API](https://developers.openai.com/api/docs/guides/realtime). See the
* [deprecation schedule](https://developers.openai.com/api/docs/deprecations) for
* migration details.
*
* @example
* ```ts
* const speech = await client.audio.speech.create({
* input: 'input',
* model: 'tts-1',
* voice: 'ash',
* });
*
* const content = await speech.blob();
* console.log(content);
* ```
*/
create(body: SpeechCreateParams, options?: RequestOptions): APIPromise<Response> {
return this._client.post(
'/audio/speech',
resolveResourceRequestOptions(options, (options) => ({
body,
...options,
headers: buildHeaders([{ Accept: 'application/octet-stream' }, options?.headers]),
__security: { bearerAuth: true },
__binaryResponse: true,
})),
);
}
}
/**
* Deprecated values:
*
* - "tts-1", "tts-1-hd", "gpt-4o-mini-tts", "gpt-4o-mini-tts-2025-12-15":
* Announced shutdown date: 2027-01-06. See
* https://developers.openai.com/api/docs/deprecations for details and
* recommended replacements.
*/
export type SpeechModel = 'tts-1' | 'tts-1-hd' | 'gpt-4o-mini-tts' | 'gpt-4o-mini-tts-2025-12-15';
export interface SpeechCreateParams {
/**
* The text to generate audio for. The maximum length is 4096 characters.
*/
input: string;
/**
* One of the available
* [TTS models](https://developers.openai.com/api/docs/guides/text-to-speech):
* `tts-1`, `tts-1-hd`, `gpt-4o-mini-tts`, or `gpt-4o-mini-tts-2025-12-15`.
*/
model: (string & {}) | SpeechModel;
/**
* The voice to use when generating the audio. Supported built-in voices are
* `alloy`, `ash`, `ballad`, `coral`, `echo`, `fable`, `onyx`, `nova`, `sage`,
* `shimmer`, `verse`, `marin`, and `cedar`. You may also provide a custom voice
* object with an `id`, for example `{ "id": "voice_1234" }`. Previews of the
* voices are available in the
* [Text to speech guide](https://developers.openai.com/api/docs/guides/text-to-speech#voice-options).
* Custom voices must be created from audio samples. Voices created from text
* prompts are supported only in Live.
*/
voice:
| string
| 'alloy'
| 'ash'
| 'ballad'
| 'coral'
| 'echo'
| 'sage'
| 'shimmer'
| 'verse'
| 'marin'
| 'cedar'
| 'fable'
| 'onyx'
| 'nova'
| SpeechCreateParams.ID;
/**
* Control the voice of your generated audio with additional instructions. Does not
* work with `tts-1` or `tts-1-hd`.
*/
instructions?: string;
/**
* The format to audio in. Supported formats are `mp3`, `opus`, `aac`, `flac`,
* `wav`, and `pcm`.
*/
response_format?: 'mp3' | 'opus' | 'aac' | 'flac' | 'wav' | 'pcm';
/**
* The speed of the generated audio. Select a value from `0.25` to `4.0`. `1.0` is
* the default.
*/
speed?: number;
/**
* The format to stream the audio in. Supported formats are `sse` and `audio`.
* `sse` is not supported for `tts-1` or `tts-1-hd`.
*/
stream_format?: 'sse' | 'audio';
}
export namespace SpeechCreateParams {
/**
* Custom voice reference.
*/
export interface ID {
/**
* The custom voice ID, e.g. `voice_1234`.
*/
id: string;
}
}
export declare namespace Speech {
export { type SpeechModel as SpeechModel, type SpeechCreateParams as SpeechCreateParams };
}