Aller au contenu

Leçon 17 · Images, Audio and Realtime

Speech and Realtime Voice

Transcribe audio, turn text into speech and understand realtime voice sessions with WebRTC and client secrets, including deprecations to avoid in 2027.

Voice features come in two main kinds. In file-based work, you have a recording and want text, or you have text and want audio. In realtime work, a person talks with your app and expects a quick spoken reply. They use different endpoints, connection types and models.

This area is changing fast. Several older audio models have announced shutdown dates, so this lesson points out what to avoid.

Voice building blocks

Transcription Audio file to text
Speech output Text to audio
Realtime Live conversation
1 / 2

Speech to text

As of December 2026, the speech-to-text guide recommends gpt-transcribe for recorded speech in its original language.

Python
# Read-only
with open("meeting.mp3", "rb") as audio:
    transcript = client.audio.transcriptions.create(
        model="gpt-transcribe", file=audio
    )
print(transcript.text)

The guide gives a maximum file size of 25 MB. Supported formats include mp3, mp4, m4a, wav, webm, ogg and flac. For longer recordings, compress or split the audio, and avoid cutting in the middle of a sentence. You can add context with a prompt, and streaming of transcript events is available.

OpenAI announced that whisper-1, gpt-4o-transcribe, gpt-4o-mini-transcribe, the diarization model and /v1/audio/translations shut down on February 26, 2027. Timestamps and speaker labels were tied to some of these older models, so check the current guide if you need those features.

Split long audio at silences

When you cut a long recording yourself, cut in pauses. This runnable helper takes a list of pause times that another tool could detect, and plans chunks under a maximum length.

Python Plan audio chunks at pauses

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The final chunk is marked clean because it ends at the end of the file. After transcription, join the parts in order and check the text near each boundary.

Text to speech

The speech endpoint, /v1/audio/speech, takes a model, the input text and a voice.

Python
# Read-only: see the shutdown notice below
with client.audio.speech.with_streaming_response.create(
    model="gpt-4o-mini-tts",
    voice="marin",
    input="Your order is ready for pickup.",
    instructions="Speak calmly and clearly.",
) as response:
    response.stream_to_file("ready.mp3")

There is an important notice: OpenAI's guide says that tts-1, tts-1-hd and gpt-4o-mini-tts shut down on January 6, 2027. The named replacement is a Realtime model, gpt-realtime-2.1-mini, which is not a drop-in change because it does not work on the speech endpoint. If you are starting now, read the current text-to-speech guide before you build.

OpenAI's usage policies require that you clearly tell users the voice is AI-generated. Custom voices need consent from the speaker and have eligibility rules.

Realtime sessions

The Realtime API keeps a live session open so audio can flow both ways with low delay. It suits voice assistants where the user may interrupt. As of December 2026, models include gpt-realtime-2.1 and gpt-realtime-2.1-mini. OpenAI also offers GPT-Live models for full-duplex conversation with separate backend work, plus live transcription and translation models.

Connection Typical use
WebRTC Browser or mobile audio
WebSocket Your trusted server
SIP Telephone calls (see the phone guides)

A browser must not receive your main API key. Instead, your server calls POST /v1/realtime/client_secrets to create a short-lived client secret. The browser uses it to start the session.

sequenceDiagram
  participant B as Browser
  participant S as Your server
  participant O as OpenAI
  B->>S: start voice chat (signed in)
  S->>O: request client secret
  O-->>S: short-lived secret
  S-->>B: secret
  B->>O: WebRTC session

Your server checks the user before it creates a secret.

In the generally available interface, session settings include the session type, and events have names such as response.output_audio.delta. The older beta interface was shut down in May 2026, so remove old beta headers from copied code.

Design for voice

  • Keep spoken answers short. Lists that look fine on screen are tiring to hear.
  • Confirm important details by repeating them: dates, amounts, addresses.
  • Require a clear confirmation before actions, and offer a text or button option.
  • Handle silence, background noise, accents and several languages. Test with real speakers.
  • Offer a way to reach a human.

Voice recordings can identify people and may include private information. Tell people when you record or process audio, get consent where the law requires it, set a retention period and protect the transcripts. A transcript can contain errors, so do not use it alone for legal or medical records.

Test voice the way people use it

Text tests are not enough for voice. Record or collect permission-cleared samples with different accents, speaking speeds, ages and microphones. Add street noise, an echoing room and two people talking. Measure the errors that matter to your task: a wrong digit in a phone number is far worse than a missing "um."

Include names, product codes and technical words. A context prompt or keyword hints may help with them, but check. For live conversations, test interruptions: the user starts talking while the assistant is speaking, says "no, wait," or stays silent. Check what your app does when the connection drops halfway through an action. It must not complete an unconfirmed order.

Log the delay between the end of the user's speech and the start of the reply, because a long pause makes people repeat themselves. Voice features also need an accessible alternative, since not everyone can speak or hear. Offer typing and written transcripts wherever you can.

Common mistakes

  • Starting a new project on a model that already has a shutdown date.
  • Treating a transcript as exact, and acting on an amount or address without confirming it.
  • Sending the main API key to the browser because it is quicker during a demo.
  • Recording people before telling them that you record.

Review a few real transcripts and calls regularly, with permission, because they show problems that planned tests miss.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which approach suits a live conversation where users can interrupt?

    Realtime sessions are built for low-delay, two-way audio.
  2. 2. How should a browser authenticate to a realtime session?

    The server keeps the main key and issues a short-lived secret.
  3. 3. What must you tell users about synthetic speech from the API?

    OpenAI's usage policies require a clear disclosure.
  4. 4. Why cut long audio at pauses?

    Cutting inside a sentence can damage the transcript near the boundary.

Summary

  • Use transcription for recordings, speech output for audio from text, and realtime for live talk.
  • Several older audio models shut down in early 2027; check the current guides.
  • Browsers use short-lived client secrets, never your main key.
  • Design short spoken replies with confirmations and a human fallback.
  • Disclose AI voices and handle recordings responsibly.

Next: Errors, Retries and Rate Limits

Votre progression est enregistrée dans ce navigateur. Aucun compte nécessaire.