Skip to content

fix(server): OpenAI read-aloud uses the Realtime API before its speech models shut down - #42542

Closed
daTuzzo wants to merge 2 commits into
pingdotgg:feat/read-aloud-providersfrom
daTuzzo:fix/openai-read-aloud-realtime
Closed

daTuzzo wants to merge 2 commits into
pingdotgg:feat/read-aloud-providersfrom
daTuzzo:fix/openai-read-aloud-realtime

Conversation

@daTuzzo

@daTuzzo daTuzzo commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #35910. This PR targets feat/read-aloud-providers, so its diff shows only this change. It can be merged into #35910, or retargeted to main after #35910 lands, whichever you prefer.

Problem

OpenAI shuts down gpt-4o-mini-tts, tts-1 and tts-1-hd on 6 January 2027 (deprecations, text-to-speech guide). The openai entry of synthesisBackends from #35910 calls /v1/audio/speech with exactly those three models, so OpenAI read-aloud stops working on that date. Their replacement models speak only through the Realtime API, which has no HTTP speech endpoint.

Change

Only the openai entry in synthesisBackends changes. Clients, contracts and saved settings stay as they are.

  • apps/server/src/speech/CloudSpeech.ts: the entry opens one Realtime session per text piece at wss://api.openai.com/v1/realtime?model=gpt-realtime-2.1, with the Effect Socket and Node's WebSocket, as DeviceHubProxy already does. It sends session.update (audio output only, PCM 24 kHz, the chosen voice, turn detection off, reader instructions), then response.create with conversation: "none" and the piece wrapped as JSON. It reads response.output_audio.delta until response.done or error. The per-piece key check, the 32 MB size cap and the 30 s stall timeout are the same bounded wrapper the HTTP backends use, now typed for the SpeechSynthesisPart stream from feat(read-aloud): highlight the paragraph and word being read #39836. The socket has a 10 s open timeout and closes when the piece ends or fails.
  • Word timings: like xAI and OpenRouter, the entry sets no wordTimings and streams MP3 parts only. The timings route reports durationSeconds from the MP3 frames once synthesis ends, and players estimate the words over it. Estimated highlighting follows the written text, so it can drift a little around spelled numbers. Word timings from the Realtime response.output_audio_transcript.delta events could be a later follow-up.
  • apps/server/src/speech/synthesisAudio.ts: encodeMp3Stream encodes the PCM to MP3 as it arrives, with the existing lamejs loader. A delta that ends inside a sample keeps its odd byte for the next one. The audio route still serves one MP3 stream, and the first audio does not wait for the whole piece.
  • apps/server/src/speech/spokenNumbers.ts: numbers, dates, times, money, percentages, versions, IP addresses and IDs are spelled as English words before the model sees the text. Long unseparated numbers and # references are read digit by digit. Digits joined to letters, hyphens, slashes or more digits (1.2s, 3-5, utf-8, 12:30pm) stay as written, so a token is never half spelled. This runs only for English text: language en*, or auto with no letters outside A to Z. Other languages keep their digits.
  • Voices: marin (default), cedar, alloy, ash, ballad, coral, echo, sage, shimmer, verse. A saved voice of the old models (fable, onyx, nova) or a saved old model id falls back to the default, so existing settings keep working.
  • speedAppliedByProvider is now false for OpenAI. The session takes no speed setting, so players speed up playback, as they already do for OpenRouter and ElevenLabs.
  • Realtime errors map to the existing reasons: invalid_api_key gives "rejected the API key", rate_limit_exceeded and insufficient_quota give "reached a usage limit", and anything else gives "could not generate speech". A response that ends failed, incomplete or cancelled, or a session that closes before response.done, fails the stream, so cut-off audio never ends as if complete. Upstream error text never reaches the error message.

Only gpt-realtime-2.1 is offered. An earlier comparison on 5 real T3 Code replies (voice marin, PCM 24 kHz, each audio file run through speech-to-text, each number difference confirmed by two or three speech-to-text models) gave these results:

Setup Confirmed errors in the audio
gpt-realtime-2.1-mini, read exactly Digit misreads in 3 of 5 replies: 35910 heard as 55910, 840 as 849, 123633 as 12363
gpt-realtime-2.1-mini, numbers spelled in code Still repeated and dropped table rows in 1 reply, in both reading modes
gpt-realtime-2.1, read exactly None found in 5 replies
gpt-realtime-2.1, numbers spelled in code, wording smoothed for listening None found in 5 replies

The exact setup of this PR (gpt-realtime-2.1, exact reading, numbers spelled in code) then ran live on 3 new replies. Every number and every table row came back right. See Verification.

Size: 649 changed lines (+543, -106), over the 300-line guide. Tests are 289 of them. The source is 360: 110 for the number speller, 25 for the streaming encoder, and 225 in CloudSpeech.ts, of which 64 are deleted lines, mostly the old HTTP entry, its model list and the stall wrapper that moved up to be shared. The old HTTP test cases for key locking and CJK splitting now run against xAI, which still speaks over HTTP.

Scope

Requested on #35910 (comment): "Everything OpenAI-specific sits in the openai entry of synthesisBackends in apps/server/src/speech/CloudSpeech.ts, so a follow-up can replace it without touching the clients. Your results on digits and table rows are worth carrying into that follow-up." Without it, OpenAI read-aloud breaks on 6 January 2027.

Verification

Tests, with no network and no real key:

  • src/speech/synthesis.test.ts, "speaks OpenAI replies over Realtime sessions and fails on a provider error": a fake Realtime server on 127.0.0.1 receives the connection through a stubbed global WebSocket. It checks the requested URL, the bearer header, the session.update and response.create fields, and the spelled text. It sends a tone as two audio deltas split inside a sample, then a clean response.done. The result is 24 kHz MP3, byte for byte the same as the whole tone encoded in one piece. Through SpeechSynthesisStreams, the timings answer { words: [], done: true } with durationSeconds equal to the 11 MP3 frames of the tone, and the catalog has no wordTimings. A saved gpt-4o-mini-tts model and fable voice fall back to gpt-realtime-2.1 and marin. A Spanish request keeps its digits. An error event with invalid_api_key gives rejected-key, a failed response with insufficient_quota gives rate-limit, an incomplete response gives unavailable, and a session that closes before response.done gives unavailable. A session that never answers fails at the 30 s stall timeout. The upstream diagnostic text never reaches the error message.
  • src/speech/spokenNumbers.test.ts: the digits from the measured misreads (35910, 840, 123633), an IP address, versions, an ISO date with a time, money, percentages and grouped numbers. 1.2s, 1,000ms, 2x, 3-5, utf-8 and 12:30pm stay as written, and 9:00 reads "nine o'clock". Text in Spanish, German and Bulgarian keeps its digits. A markdown table passed through readAloudText keeps every row and every value in order, with its numbers spelled.
  • Mutation checks: changing the completion check from status !== "completed" to status === "failed" makes the Realtime test fail. Dropping the odd byte that encodeMp3Stream carries between deltas makes it fail on the byte comparison. Dropping the encoder's final flush makes it fail only on the timings length (0.168 s instead of 0.264 s).
  • vp test run src/speech/spokenNumbers.test.ts src/speech/synthesis.test.ts src/speech/CloudSpeech.test.ts src/speech/synthesisStreams.test.ts src/speech/synthesisTimings.test.ts in apps/server: 57 passed.
  • vp run check:changed origin/feat/read-aloud-providers --concurrency 1: format, lint, knip, graphql, typecheck of 3 packages and the tests beside the changed files (50 tests in 4 files) all pass.

Live check against OpenAI: this branch's dev server ran with an empty data folder and an OpenAI key copied into it. Three made-up replies went through the read-aloud route (POST /api/voice/synthesis/openai/stream, then the MP3 audio route) with voice marin and language auto. The table reply went through readAloudText first, as the client does. Each MP3 then went through whisper-1.

Reply Content Audio Start request to first MP3 byte Whole reply Numbers in the transcript
1 PR and commit IDs, a test count, 1.2s, a version, an ISO date and a time 21.9 s 2.55 s 6.6 s All correct: 35910, 840, 1.2s, 123633, 2.5.13, 24 November 2026, 17:05
2 A 3-row markdown table and a version 19.7 s 1.68 s 6.1 s All correct, every row in order: 35910, 840, 99.5%; 41809, 1,204, 100%; 24.19.0
3 A percentage, a grouped number, durations, an ID and a version 21.1 s 2.41 s 6.8 s All correct: 92%, 41,693, 340 ms, 85 ms, 123633, 3.0.1
  • The first byte took a median of 2.41 s. It is measured from the start request, so it includes the socket handshake and the session setup. The audio came at 3.1 to 3.3 seconds of audio per second.
  • The voice misread nothing. whisper-1 wrote "bill" for "build" in reply 1, and gpt-transcribe heard "build" in the same file, so that was a transcription error.
  • With a fake key, OpenAI accepts the socket, sends an error event with invalid_api_key and closes with code 3000. The route answered 422 with reason rejected-key.
  • After the rebase onto feat(read-aloud): highlight the paragraph and word being read #39836, one more reply ("Merged feat(read-aloud): add optional speech playback across clients #35910 after 840 tests passed, and version 2.5.13 shipped on 2026-11-24.") ran the same way. The audio was 12.05 s, with the first MP3 byte after 2.60 s. The timings route answered { words: [], done: true, durationSeconds: 12.048 }, the same as the MP3 frame length. gpt-transcribe heard every number correctly.
  • Spend: about $0.12 in total, estimated from 74.8 s of audio at the $0.089 per audio minute measured earlier, plus transcription. It was not read from billing.

Not checked:

  • Live: replies in other languages, a reply longer than one 1,000-character piece, voices other than marin, and the rate_limit_exceeded and insufficient_quota codes. The tests cover insufficient_quota with a fake server.
  • Playback and word highlighting in the web, desktop and mobile players, including how they speed up playback now that speedAppliedByProvider is false.

@github-actions github-actions Bot added vouch:unvouched Author is not yet trusted in the VOUCHED list. size:L 100-499 changed lines (additions + deletions). labels Nov 25, 2026
…h models shut down

OpenAI shuts down gpt-4o-mini-tts, tts-1 and tts-1-hd on 6 January 2027,
and their successors speak only over the Realtime API. Only the openai
entry in synthesisBackends changes: it opens one Realtime session per
piece with the existing Effect Socket and Node's WebSocket, and streams
the 24 kHz PCM through the existing lamejs loader to MP3. The per-piece
key check, size cap and stall timeout are shared with the HTTP backends.
A response that does not complete, or a session that closes before
response.done, fails the piece. Like xAI and OpenRouter, it sends no
word timings: players estimate them from the audio length.

Numbers, dates, money, versions and IDs in English text are spelled as
words before the model sees them. Digits joined to letters, hyphens,
slashes or more digits, such as 1.2s or 3-5, stay as written, so no
token is half spelled. With spelled numbers, gpt-realtime-2.1 read every
digit and table row in a test on five real replies; the mini model
repeated and dropped table rows, so it is not offered. Saved voices and
model ids of the retired models fall back to marin and gpt-realtime-2.1.

Follow-up to pingdotgg#35910.

Custom: no OSS fit. Evaluated: to-words 7.5.1 (MIT), n2words 6.3.1 (MIT),
number-to-words 1.2.5 (MIT). Bar failed: appropriate scale. The speller
needs English cardinals only, about 15 lines, and a new server package
must be proven to load in the single-executable CLI, which needs a full
executable build.
@daTuzzo
daTuzzo force-pushed the fix/openai-read-aloud-realtime branch from e0870b2 to cef8852 Compare November 25, 2026 10:05
@Yash-Singh1

Copy link
Copy Markdown
Collaborator

I played around with the realtime API and I don't think it'll cut it for TTS, thanks for this though I'll probably remove OpenAI TTS now that I know it'll be deprecated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L 100-499 changed lines (additions + deletions). vouch:unvouched Author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants