Skip to content

server: stream /v1/tasks/stream events as SSE on request - #849

Merged
0xShug0 merged 2 commits into
0xShug0:mainfrom
Liquid4All:tasks-stream-sse
Oct 9, 2026
Merged

0xShug0 merged 2 commits into
0xShug0:mainfrom
Liquid4All:tasks-stream-sse

Conversation

@ykhrustalev

@ykhrustalev ykhrustalev commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Lets POST /v1/tasks/stream send each event to the client as soon as the model makes it, instead of all at once at the end. A client opts in with "stream_format": "sse". For lfm2_audio conversations (#838, #844, #845), a reply then starts playing about 1.4 s into turn 10 instead of after the whole reply is done (about 6 s).

Problem

  • /v1/tasks/stream waits until the model has finished, then sends every event in one JSON body. The client hears nothing until the whole reply is ready
  • /v1/audio/speech/live does stream, but it cannot take a conversation's history, so the later turns of a conversation have no way to start playing early

What this PR changes

  • With "stream_format": "sse" in the body, both server runtimes answer with server-sent events:
    data: {"type":"task.stream.event","event":{...}}   one per event, as soon as it is made
    data: {"type":"task.stream.done","result":{...}}   the final result, artifacts included
    data: [DONE]
    
    Each event and the result are the same JSON objects the normal response holds
  • Without the field, nothing changes: the response is the same JSON body as before
  • The change is small: one shared header, app/server/task_stream_format.h, and one if (sse) block in each runtime's handler, reusing the existing SSE helpers. Plus tests, a README section for this route (it had none), and a note in the lfm2_audio Conversations section

What changes for users

  • Clients that do not send stream_format: no change
  • A stream_format other than "sse" or null now gets a 400; before, this route ignored the field. This catches typos and matches /v1/audio/speech. Nothing in this repo sends the field to this route, and the WebUI does not use it. If you would rather keep ignoring other values, it is a one-line change
  • Errors found before streaming starts keep their usual status code (400, 500, and 503 for a busy model with --parallel-jobs). Errors after streaming starts arrive as an error event, so a client knows the run succeeded only when it gets task.stream.done
  • The done event repeats the reply's audio on one line (about 650 KB per 10 s of audio), so a client should not cap the line length
  • An SSE client that disconnects stops the run; a JSON client's run still finishes, as before. An SSE client that stops reading keeps the model busy, as on the other SSE routes
  • This route still takes the question as a server-local audio path, not audio_base64. Would you like audio_base64 here too, in this PR or a follow-up?

Testing
On a Xeon 8358, CPU only, the English LFM2.5-Audio checkpoint at Q4_0, 8 threads, both server runtimes. Tested on main bd88e6e; the branch now sits on #837, which only changes CUDA files.

  • Unit tests: the server, HTTP, CLI and lfm2_audio tests pass, apart from two known flaky HTTP live-body tests that failed once each and build from no file this PR touches. The two changed tests passed 20 of 20 runs and fail without the change. An M3 Ultra built an earlier version of the branch and passed the same tests
  • Without stream_format: responses are byte for byte main's apart from the timing value, including the error responses and ten-turn conversations
  • With SSE, over ten-turn conversations: on every turn the joined events and the final result equal the normal JSON response, reply artifacts included. Each event came in its own chunk, and the first audio event arrived within 3 ms of the server producing it
  • Errors, a busy model and clients disconnecting behaved as described above, and the server stayed healthy
  • Ten-turn conversation, seconds (the two runtimes were within about 0.1 s of each other):
turn normal response, whole reply SSE, first audio
1 4.9 to 5.1 0.5
2 7.4 to 7.5 1.1
3 5.9 to 6.1 1.4
4 9.4 to 9.7 1.5
5 9.3 to 9.6 1.9 to 2.0
6 9.2 to 9.6 1.8
7 2.2 to 2.3 1.4
8 7.8 to 8.2 1.0
9 6.7 to 7.0 1.3
10 5.9 to 6.1 1.4

The whole response takes about the same time either way

  • Not tested: CUDA, a client on another machine, and a model failure partway through a stream

Build and run

cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DENGINE_BUILD_TESTS=ON -DENGINE_BUILD_EXTENDED_TESTS=ON \
  -DENGINE_BUILD_MODEL_TESTS=ON -DAUDIOCPP_MODEL_SET=custom -DAUDIOCPP_MODELS=lfm2_audio,voxcpm1
ninja -C build audiocpp_server server_parallel_lifecycle_test server_runtime_selection_test
(cd build && ctest -R '^(server_parallel_lifecycle_test|server_runtime_selection_test)$')
./build/bin/audiocpp_server --config server.json   # add --parallel-jobs for the parallel runtime
curl -N http://127.0.0.1:8080/v1/tasks/stream -H 'Content-Type: application/json' \
  -d '{"model": "lfm2-audio-s2s-stream", "stream_format": "sse",
       "request": {"audio": "/path/to/question.wav", "options": {"return_codes": true}}}'

with a server.json such as

{"models": [{"id": "lfm2-audio-s2s-stream", "family": "lfm2_audio", "task": "s2s", "mode": "streaming",
  "path": "models/LFM2.5-Audio-1.5B-GGUF",
  "session_options": {"lfm2_audio.model_gguf": "LFM2.5-Audio-1.5B-Q4_0.gguf"}}]}

The test runs used gcc 12 (-DCMAKE_C_COMPILER=gcc-12 -DCMAKE_CXX_COMPILER=g++-12). For a conversation's later turns, send the earlier questions and replies in request.artifacts, as in Conversations in docs/community_models/lfm2_audio.md.

/v1/tasks/stream collects every event of a run and sends them in one
JSON body after the run is over, so a client gets the first audio of a
reply only once the whole reply is done. With "stream_format": "sse" at
the top level of the body, both server runtimes now answer with
server-sent events instead: each event as the model produces it, as
{"type":"task.stream.event","event":E}, where E is the object the JSON
response puts in "events", then {"type":"task.stream.done","result":R},
where R is the object it puts under "result", artifacts included, then
[DONE].

Without the field, or with null, the response is the JSON body as
before. This route used to ignore the field, so a body that carried
any value, "sse" included, got the JSON body; a value other than "sse"
or null is now a 400. Errors found before the stream starts keep the
status the JSON request gets, including 503 for a busy model with
--parallel-jobs, whose slot is bound before the headers. Without
--parallel-jobs the model lock is taken inside the stream, so a busy
model, like any later failure, ends the stream with the transport's
error event, as on the other SSE routes.

Both paths pass the same request to run_streaming_model and write each
event and the result with the same stream_event_json and
streaming_task_result_json, so a run with the same output gives the
bytes the JSON response holds, apart from ttft_ms. A header shared by
both runtimes checks the field and builds the two kinds of line. The
README gets a section for the route, which had none, and the help text
lists it. server_parallel_lifecycle_test covers the SSE path with 1, 2
and 4 slots; server_runtime_selection_test covers the legacy route's
opt-in, its load failure inside the stream and the 400s.
With stream_format sse on /v1/tasks/stream, a reply's text and audio
come as events while it is generated, and the reply artifact for the
next turn comes in the closing task.stream.done event. Say so in
Conversations, with what a client needs to know: the entry needs mode
streaming, the done event repeats the reply's audio on one line, a
stream closed early returns no reply artifact, the route does not read
audio_base64, and a turn the model turns away, such as one past the
step limit, is an error event before any audio. The Limitations entry
on conversations names the route's SSE form too.
@0xShug0
0xShug0 merged commit fd20bbf into 0xShug0:main Oct 9, 2026
11 checks passed
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