Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Openai Openai node Speech Create

From Leeroopedia
Knowledge Sources
Domains Audio, Speech_Synthesis
Last Updated 2026-02-15 00:00 GMT

Overview

Concrete tool for generating speech audio from text provided by the openai-node SDK.

Description

The Speech.create() method sends a POST to /audio/speech with text, model, and voice parameters. It returns a raw Response object containing binary audio data. The caller accesses the audio via .body (ReadableStream), .arrayBuffer(), or .blob().

Usage

Use this method to generate audio from text. Write the response body to a file or stream it to an audio player.

Code Reference

Source Location

  • Repository: openai-node
  • File: src/resources/audio/speech.ts
  • Lines: L24-31 (create method), L36-91 (SpeechCreateParams)

Signature

class Speech extends APIResource {
  create(
    body: SpeechCreateParams,
    options?: RequestOptions,
  ): APIPromise<Response>;
}

interface SpeechCreateParams {
  input: string;                    // Text to synthesize (max 4096 chars)
  model: SpeechModel;              // 'tts-1' | 'tts-1-hd' | 'gpt-4o-mini-tts'
  voice: string;                    // 'alloy' | 'ash' | 'coral' | 'echo' | 'fable' | 'onyx' | 'nova' | 'shimmer' | 'sage' | 'ballad' | 'verse'
  response_format?: string;        // 'mp3' | 'opus' | 'aac' | 'flac' | 'wav' | 'pcm'
  speed?: number;                   // 0.25 to 4.0
  instructions?: string;           // Voice style instructions (gpt-4o-mini-tts only)
}

Import

import OpenAI from 'openai';
// Access via: client.audio.speech.create(...)

I/O Contract

Inputs

Name Type Required Description
input string Yes Text to synthesize (max 4096 characters)
model SpeechModel Yes TTS model ('tts-1', 'tts-1-hd', 'gpt-4o-mini-tts')
voice string Yes Voice ID ('alloy', 'echo', 'nova', etc.)
response_format string No (default 'mp3') Output format
speed number No (default 1.0) Playback speed (0.25-4.0)
instructions string No Style instructions for gpt-4o-mini-tts

Outputs

Name Type Description
response Response Raw HTTP Response with binary audio data in body

Usage Examples

Generate and Save MP3

import OpenAI from 'openai';
import fs from 'fs';

const client = new OpenAI();

const response = await client.audio.speech.create({
  model: 'tts-1',
  voice: 'alloy',
  input: 'Hello! Welcome to our application.',
});

const buffer = Buffer.from(await response.arrayBuffer());
fs.writeFileSync('output.mp3', buffer);

Stream Audio

const response = await client.audio.speech.create({
  model: 'tts-1-hd',
  voice: 'nova',
  input: 'This is high-quality speech synthesis.',
  response_format: 'opus',
  speed: 1.2,
});

// Stream to file
const stream = response.body;
const fileStream = fs.createWriteStream('output.opus');
// Pipe the readable stream to the file

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment