Skip to content

feat: return_text on /v1/audio/speech/live - #805

Merged
0xShug0 merged 3 commits into
0xShug0:mainfrom
Liquid4All:lfm2-audio-live-return-text
Oct 6, 2026
Merged

0xShug0 merged 3 commits into
0xShug0:mainfrom
Liquid4All:lfm2-audio-live-return-text

Conversation

@ykhrustalev

Copy link
Copy Markdown
Contributor

This is the last part of the plan in #683: return_text on POST /v1/audio/speech/live, agreed there as ?return_text=true, audio only by default. It builds on #798 (LFM2.5-Audio speech-to-speech).

Problem
LFM2.5-Audio's speech-to-speech entry writes its reply's words as it speaks, and its streaming events carry the new text as it is written, but the live route sends only the audio. A client that wants the words has to run the question again through /v1/tasks/run or transcribe the reply.

What this PR adds

  • A return_text query parameter: true, 1, false or 0 (case sensitive). Absent, empty or bare (&return_text with no value) means false. Other values get a 400 (live speech return_text must be true or false)
  • With return_text=true, each event's new text goes out as speech.text.delta before that event's audio, and speech.text.done carries the whole text before speech.audio.done:
    data: {"type":"speech.text.delta","delta":"Sure! How about"}
    data: {"type":"speech.audio.delta","audio":"<base64 PCM>"}
    ...
    data: {"type":"speech.text.done","text":"Sure! How about ..."}
    data: {"type":"speech.audio.done","timing":{...}}
    data: [DONE]
    
  • speech.text.done is the model's final text, or the joined deltas if it gives none
  • return_text needs stream_format=sse, since raw PCM has no place for text; otherwise it gets a 400 (live speech return_text needs stream_format=sse)
  • A model that writes no text gets HTTP 200 and its audio, then an error event. Today only LFM2.5-Audio's speech-to-speech entry writes text on this route, so that is what return_text=true gives with any other model, for example PersonaPlex or a TTS entry
  • Both server runtimes: the --parallel-jobs runtime (server: separate legacy and experimental parallel jobs runtimes #715) split off after this change was written and has its own live handler, so this PR changes it the same way, as server: add include_params to GET /v1/models, read from the model spec #787 did for its change
  • The server README, --help and docs/community_models/lfm2_audio.md describe the parameter; the docs page drops its limitation that this route returns no text

What does not change
Without return_text, or with false, 0 or an empty value, the stream is the same as on main 8fa20ff in both runtimes. On an A10 (four package and backend setups), 199 of 200 audio-only live requests matched main in every event apart from timing and in PCM bytes. In the other one, a TTS request, main's own server reset the connection; 10 repeats per runtime then matched. On an M3 Ultra all 20 S2S requests matched main the same way. Its TTS requests often hit the connection reset described under Known limitations, on main and this branch alike (in the legacy comparison main got the stream and this branch the reset); read with a client that takes the whole response, they matched main in both runtimes.

Testing

  • server_parallel_lifecycle_test covers the --parallel-jobs route at 1, 2 and 4 slots: text and event order, no text when off, both 400s, speech.text.done with the final text or the joined deltas, the no-text error, and no slot left held. Handlers without the change, with text after audio, accepting other values, always sending the joined deltas, or treating a missing final text as no text fail it. The fixture edits change existing lines instead of adding new ones, so the #L links in docs/maintainers/parallel_server_behavior_contract.md still land on their tests
  • The default runtime has no unit test here (only the parallel one has a test hook); the end to end checks below cover it
  • The server, http, streaming, session pool, lfm2_audio and model spec unit tests pass on Linux (gcc-12) and macOS (AppleClang, Metal), with no new warnings. The three lfm2_audio model tests were skipped on both machines because they need the model package; the end to end checks below use the real models
  • End to end through audiocpp_server, branch and main, both runtimes, on an A10 (EN F16, EN Q8_0 and JP F16 on CUDA, EN Q8_0 on the CPU) and an M3 Ultra (EN and JP Q8_0 on Metal)
  • All 282 speech-to-speech return_text streams start with a text delta, have audio after every text delta, and end with speech.text.done, speech.audio.done and [DONE]. The deltas join to speech.text.done. Each event is valid UTF-8 on its own, so Japanese comes in whole characters. Byte tokens that make no character come out as U+FFFD, as in /v1/tasks/run (seen in some Thai replies)
  • The text equals /v1/tasks/run for the same seed and max_tokens in every pair checked, including 203 replies cut at max_tokens (a cut inside a Thai character drops it). The audio is byte for byte the same as without return_text in every pair checked
  • Both invalid requests get their 400 and the next request succeeds; under --parallel-jobs no slot is left held
  • A TTS streaming entry stood in for a model that writes no text. With return_text=true or 1 it gave HTTP 200, its audio, then the error event live speech model wrote no text; return_text needs a model that writes text, with no speech.text.done, speech.audio.done or [DONE]. Without return_text the same audio ends with live streaming response did not observe input end, as on main
  • Each server took many requests in a row (15 to 100 on the A10); the first, repeated last, gave the same events, text and audio. Both runtimes answered every common S2S request identically

Build and run
As tested on the A10, which also passed -DCMAKE_C_COMPILER=gcc-12 -DCMAKE_CXX_COMPILER=g++-12 -DCMAKE_CUDA_HOST_COMPILER=g++-12; on macOS use -DENGINE_ENABLE_METAL=ON -DENGINE_ENABLE_OPENMP=OFF -DGGML_OPENMP=OFF instead of the CUDA flags. -DENGINE_BUILD_EXTENDED_TESTS=ON adds the http and streaming tests.

cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DENGINE_ENABLE_CUDA=ON -DGGML_CUDA=ON \
  -DCMAKE_CUDA_ARCHITECTURES=86 -DENGINE_BUILD_TESTS=ON -DENGINE_BUILD_MODEL_TESTS=ON \
  -DAUDIOCPP_MODEL_SET=custom -DAUDIOCPP_MODELS=lfm2_audio
ninja -C build audiocpp_server server_parallel_lifecycle_test server_model_slots_test \
  server_runtime_selection_test session_pool_test
(cd build && ctest -R 'server|session_pool' --output-on-failure)

With an S2S streaming entry as in the docs page, run audiocpp_server with or without --parallel-jobs:

ffmpeg -i question.wav -ar 16000 -ac 1 -f s16le - \
  | curl -N -X POST -H 'Expect:' -T - \
      'http://127.0.0.1:8080/v1/audio/speech/live?model=lfm2-audio-s2s-stream&sample_rate=16000&channels=1&sample_format=s16le&return_text=true&input=Respond%20with%20interleaved%20text%20and%20audio.'

Known limitations

  • Raw PCM output (stream_format=audio) has no text
  • A model that writes no text streams all its audio before the error event: no model capability says whether a model writes text, so the route cannot tell in advance
  • Seen on main too: a TTS entry here never reads the PCM body, so its stream ends with live streaming response did not observe input end, and the server then closes with the body unread. A client still sending PCM may get a connection reset, or wait until its timeout, instead of the stream or a 400. On the M3 Ultra that happened to 16 or 17 of 20 TTS requests per server with Python's http.client, on main and this branch alike. The TTS results above come from clients that read the whole response
  • Not tested on Vulkan or Windows, with the Q4_0 packages, or with F16 on Metal

With return_text=true (or 1), the live speech route also streams the text
a model writes as it speaks, such as a spoken reply's words.
speech.text.delta events carry the text written since the previous event,
before that event's audio, and speech.text.done carries the whole text
before speech.audio.done. Without it the stream is audio only, as before.
return_text needs stream_format=sse, since raw PCM has no place for text,
and takes true, 1, false or 0; other values are rejected. A model that
writes no text ends the stream with an error event after its audio, as the
transcription route fails when a model gives no text.

Checked end to end through audiocpp_server on an A10 with a streaming
speech-to-speech model that emits text with its audio (LFM2.5-Audio from
the lfm2_audio port): the text deltas join to speech.text.done, which
matches the offline reply for the same seed; the audio is identical with
and without return_text; Japanese text arrives in whole characters; and
the two invalid requests return 400.
The live route now streams a spoken reply's text with return_text=true, so the page shows it in the example and drops the old limitation.
The parallel runtime split off from the legacy server after return_text
was added to /v1/audio/speech/live and has its own live handler, so
under --parallel-jobs return_text was ignored and the stream stayed
audio only. Give that handler the same change: return_text takes true,
1, false or 0 and needs stream_format=sse, speech.text.delta events
carry each event's text ahead of its audio, speech.text.done carries
the whole text before speech.audio.done, and a model that writes no
text ends the stream with an error after its audio. The checks run
after the request has bound its slot, as the stream_format check does,
so a rejected request gets a 400 and releases the slot. Apart from the
existing lease and busy handling the two handlers are now the same.

The parallel lifecycle test now drives the live route at 1, 2 and 4
slots, with a fixture session that can read the request body, write no
text, or give no final text or one other than its streamed text: the
text and event order with return_text, no text events without it or with
false or 0, both 400s, speech.text.done with the final text or, when the
model gives none, the joined deltas, the no-text error after the audio,
and no slot left held. It fails without the change, with the text
written after the audio, with other values accepted, with the joined
deltas always in speech.text.done, or with a missing final text treated
as no text. The fixture changes stay on existing lines, so the line
links in the parallel server contract still land on their tests.

A CPU build on Linux passes the server, http, streaming, session pool,
lfm2_audio and model spec tests. Through audiocpp_server, LFM2.5-Audio
answers the same English and Japanese questions with the same seed
with identical text, text deltas and audio with and without
--parallel-jobs, and its audio is the same with and without
return_text.
@0xShug0
0xShug0 merged commit 6bb6add into 0xShug0:main Oct 6, 2026
11 checks passed
@0xShug0

0xShug0 commented Oct 6, 2026

Copy link
Copy Markdown
Owner

@ykhrustalev PR merged. Thanks!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants