### Stream spoken audio with OpenAI SDKs and cURL Source: https://developers.openai.com/api/docs/guides/text-to-speech Examples for streaming audio output directly to speakers using JavaScript, Python, and command-line tools. ```javascript import OpenAI from "openai"; import { playAudio } from "openai/helpers/audio"; const openai = new OpenAI(); const response = await openai.audio.speech.create({ model: "gpt-4o-mini-tts", voice: "coral", input: "Today is a wonderful day to build something people love!", instructions: "Speak in a cheerful and positive tone.", response_format: "wav", }); await playAudio(response); ``` ```python import asyncio from openai import AsyncOpenAI from openai.helpers import LocalAudioPlayer openai = AsyncOpenAI() async def main() -> None: async with openai.audio.speech.with_streaming_response.create( model="gpt-4o-mini-tts", voice="coral", input="Today is a wonderful day to build something people love!", instructions="Speak in a cheerful and positive tone.", response_format="pcm", ) as response: await LocalAudioPlayer().play(response) if __name__ == "__main__": asyncio.run(main()) ``` ```bash curl https://api.openai.com/v1/audio/speech \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-4o-mini-tts", "input": "Today is a wonderful day to build something people love!", "voice": "coral", "instructions": "Speak in a cheerful and positive tone.", "response_format": "wav" }' | ffplay -i - ``` -------------------------------- ### Example JSON Response Structure Source: https://developers.openai.com/api/docs/guides/text This JSON structure represents a typical response from the OpenAI API when generating text. Note that the 'output' array can contain multiple items, and the text content is not always at a fixed index. ```json [ { "id": "msg_67b73f697ba4819183a15cc17d011509", "type": "message", "role": "assistant", "content": [ { "type": "output_text", "text": "Under the soft glow of the moon, Luna the unicorn danced through fields of twinkling stardust, leaving trails of dreams for every child asleep.", "annotations": [] } ] } ] ``` -------------------------------- ### Generate Text with Instructions (cURL) Source: https://developers.openai.com/api/docs/guides/text Use the `instructions` parameter to provide high-level guidance to the model. This guidance takes priority over the `input` parameter. ```bash curl "https://api.openai.com/v1/responses" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d '{ "model": "gpt-5", "reasoning": {"effort": "low"}, "instructions": "${semicolonsDevMsg}", "input": "${semicolonsPrompt}" }' ``` -------------------------------- ### Generate Text with Instructions (Python) Source: https://developers.openai.com/api/docs/guides/text Use the `instructions` parameter to provide high-level guidance to the model. This guidance takes priority over the `input` parameter. ```python from openai import OpenAI client = OpenAI() response = client.responses.create( model="gpt-5", reasoning={"effort": "low"}, instructions="${semicolonsDevMsg}", input="${semicolonsPrompt}", ) print(response.output_text) ``` -------------------------------- ### Generate Text with Instructions (JavaScript) Source: https://developers.openai.com/api/docs/guides/text Use the `instructions` parameter to provide high-level guidance to the model. This guidance takes priority over the `input` parameter. ```javascript import OpenAI from "openai"; const client = new OpenAI(); const response = await client.responses.create({ model: "gpt-5", reasoning: { effort: "low" }, instructions: "${semicolonsDevMsg}", input: "${semicolonsPrompt}", }); console.log(response.output_text); ``` -------------------------------- ### Generate Text with Messages (cURL) Source: https://developers.openai.com/api/docs/guides/text Provide instructions and user input using a message array with 'developer' and 'user' roles. This is equivalent to using the `instructions` parameter. ```bash curl "https://api.openai.com/v1/responses" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d '{ "model": "gpt-5", "reasoning": {"effort": "low"}, "input": [ { "role": "developer", "content": "${semicolonsDevMsg}" }, { "role": "user", "content": "${semicolonsPrompt}" } ] }' ``` -------------------------------- ### Generate Text with Messages (Python) Source: https://developers.openai.com/api/docs/guides/text Provide instructions and user input using a message array with 'developer' and 'user' roles. This is equivalent to using the `instructions` parameter. ```python from openai import OpenAI client = OpenAI() response = client.responses.create( model="gpt-5", reasoning={"effort": "low"}, input=[ { "role": "developer", "content": "${semicolonsDevMsg}" }, { "role": "user", "content": "${semicolonsPrompt}" } ] ) print(response.output_text) ``` -------------------------------- ### Generate Text with Messages (JavaScript) Source: https://developers.openai.com/api/docs/guides/text Provide instructions and user input using a message array with 'developer' and 'user' roles. This is equivalent to using the `instructions` parameter. ```javascript import OpenAI from "openai"; const client = new OpenAI(); const response = await client.responses.create({ model: "gpt-5", reasoning: { effort: "low" }, input: [ { role: "developer", content: "${semicolonsDevMsg}" }, { role: "user", content: "${semicolonsPrompt}", }, ], }); console.log(response.output_text); ``` -------------------------------- ### Create a Voice Source: https://developers.openai.com/api/docs/guides/text-to-speech Creates a new voice by referencing a consent recording ID and providing an audio sample. ```bash curl https://api.openai.com/v1/audio/voices \ -X POST \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -F "name=test_voice" \ -F "audio_sample=@$HOME/tmp/voice_consent/audio_sample_recording.wav;type=audio/x-wav" \ -F "consent=cons_123abc" ``` -------------------------------- ### Generate text with a prompt template Source: https://developers.openai.com/api/docs/guides/text Use a pre-defined prompt template by providing its ID, version, and a map of string variables. ```javascript import OpenAI from "openai"; const client = new OpenAI(); const response = await client.responses.create({ model: "gpt-5", prompt: { id: "pmpt_abc123", version: "2", variables: { customer_name: "Jane Doe", product: "40oz juice box" } } }); console.log(response.output_text); ``` ```python from openai import OpenAI client = OpenAI() response = client.responses.create( model="gpt-5", prompt={ "id": "pmpt_abc123", "version": "2", "variables": { "customer_name": "Jane Doe", "product": "40oz juice box" } } ) print(response.output_text) ``` ```bash curl https://api.openai.com/v1/responses \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-5", "prompt": { "id": "pmpt_abc123", "version": "2", "variables": { "customer_name": "Jane Doe", "product": "40oz juice box" } } }' ``` -------------------------------- ### Prompt template with file input variable Source: https://developers.openai.com/api/docs/guides/text Include file references in prompt variables by specifying the input_file type and the corresponding file ID. ```javascript import fs from "fs"; import OpenAI from "openai"; const client = new OpenAI(); // Upload a PDF we will reference in the prompt variables const file = await client.files.create({ file: fs.createReadStream("draconomicon.pdf"), purpose: "user_data", }); const response = await client.responses.create({ model: "gpt-5", prompt: { id: "pmpt_abc123", variables: { topic: "Dragons", reference_pdf: { type: "input_file", file_id: file.id, }, }, }, }); console.log(response.output_text); ``` ```python import openai, pathlib client = openai.OpenAI() # Upload a PDF we will reference in the variables file = client.files.create( file=open("draconomicon.pdf", "rb"), purpose="user_data", ) response = client.responses.create( model="gpt-5", prompt={ "id": "pmpt_abc123", "variables": { "topic": "Dragons", "reference_pdf": { "type": "input_file", "file_id": file.id, }, }, }, ) print(response.output_text) ``` ```bash # Assume you have already uploaded the PDF and obtained FILE_ID curl https://api.openai.com/v1/responses \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-5", "prompt": { "id": "pmpt_abc123", "variables": { "topic": "Dragons", "reference_pdf": { "type": "input_file", "file_id": "file-abc123" } } } }' ``` -------------------------------- ### Generate spoken audio from input text Source: https://developers.openai.com/api/docs/guides/text-to-speech Demonstrates how to send a request to the speech endpoint to convert text into an MP3 file using the gpt-4o-mini-tts model. ```javascript import fs from "fs"; import path from "path"; import OpenAI from "openai"; const openai = new OpenAI(); const speechFile = path.resolve("./speech.mp3"); const mp3 = await openai.audio.speech.create({ model: "gpt-4o-mini-tts", voice: "coral", input: "Today is a wonderful day to build something people love!", instructions: "Speak in a cheerful and positive tone.", }); const buffer = Buffer.from(await mp3.arrayBuffer()); await fs.promises.writeFile(speechFile, buffer); ``` ```python from pathlib import Path from openai import OpenAI client = OpenAI() speech_file_path = Path(__file__).parent / "speech.mp3" with client.audio.speech.with_streaming_response.create( model="gpt-4o-mini-tts", voice="coral", input="Today is a wonderful day to build something people love!", instructions="Speak in a cheerful and positive tone.", ) as response: response.stream_to_file(speech_file_path) ``` ```bash curl https://api.openai.com/v1/audio/speech \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-4o-mini-tts", "input": "Today is a wonderful day to build something people love!", "voice": "coral", "instructions": "Speak in a cheerful and positive tone." }' \ --output speech.mp3 ``` -------------------------------- ### Upload Consent Recording Source: https://developers.openai.com/api/docs/guides/text-to-speech Uploads a voice consent recording to the API to obtain a consent ID. ```bash -F "language=en" \ -F "recording=@$HOME/tmp/voice_consent/consent_recording.wav;type=audio/x-wav" ``` -------------------------------- ### Upload Voice Consent Recording Source: https://developers.openai.com/api/docs/guides/text-to-speech Use this cURL command to upload a voice consent recording to the OpenAI API. Ensure your API key is set as an environment variable. ```bash curl https://api.openai.com/v1/audio/voice_consents \ -X POST \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -F "name=test_consent" \ ``` -------------------------------- ### Generate Speech with Custom Voice Source: https://developers.openai.com/api/docs/guides/text-to-speech Uses a specific voice ID to generate speech from text via the Audio API. ```bash curl https://api.openai.com/v1/audio/speech \ -X POST \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-4o-mini-tts", "voice": { "id": "voice_123abc" }, "input": "Maple est le meilleur golden retriever du monde entier.", "language": "fr", "format": "wav" }' \ --output sample.wav ``` -------------------------------- ### Configure Realtime Session Voice Source: https://developers.openai.com/api/docs/guides/text-to-speech Sets the output voice ID within the Realtime API session configuration. ```javascript const sessionConfig = JSON.stringify({ session: { type: "realtime", model: "gpt-realtime", audio: { output: { voice: { id: "voice_123abc" }, }, }, }, }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.