Skip to content

Commit 595b6fd

Browse files
localai-botmudler
andauthored
feat(api/transcription): include segments + duration + language on stream done event (#9709)
streamTranscription previously emitted a done event with just `text`, matching the OpenAI streaming spec exactly. Streaming clients that need per-utterance timings or audio duration had to fall back to the non-streaming JSON path — and that path is exactly the one that trips on ResponseHeaderTimeout when whisper requests queue behind each other on a SingleThread backend. Extend the done event to additively carry `language`, `duration`, and a `segments` array (id, start, end, text — start/end as float seconds, matching TranscriptionSegmentSeconds). Empty / zero values are still omitted; spec-compliant clients ignore the new fields. This unblocks notary's streaming Transcribe (companion change in the notary repo) so it produces the same TranscriptionResult shape as the JSON path while sidestepping the queue-induced header timeouts. Assisted-by: Claude:claude-opus-4-7 [Claude Code] Signed-off-by: Ettore Di Giacinto <mudler@localai.io> Co-authored-by: Ettore Di Giacinto <mudler@localai.io>
1 parent 447c186 commit 595b6fd

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

core/http/endpoints/openai/transcription.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,36 @@ func streamTranscription(c echo.Context, req backend.TranscriptionRequest, ml *m
257257
"delta": finalResult.Text,
258258
})
259259
}
260-
_ = writeEvent(map[string]any{
260+
// done carries the assembled text plus, when the backend produced them,
261+
// per-segment timings, audio duration, and detected language. The OpenAI
262+
// streaming spec only specifies `text`; the extra fields are an additive
263+
// extension so streaming clients (e.g. notetaker) can build the same
264+
// TranscriptionResultSeconds shape they get from the JSON response path
265+
// without us forcing them off SSE just to recover segments. Spec-compliant
266+
// clients ignore unknown fields.
267+
doneEvent := map[string]any{
261268
"type": "transcript.text.done",
262269
"text": finalResult.Text,
263-
})
270+
}
271+
if finalResult.Language != "" {
272+
doneEvent["language"] = finalResult.Language
273+
}
274+
if finalResult.Duration > 0 {
275+
doneEvent["duration"] = finalResult.Duration
276+
}
277+
if len(finalResult.Segments) > 0 {
278+
segs := make([]map[string]any, 0, len(finalResult.Segments))
279+
for _, seg := range finalResult.Segments {
280+
segs = append(segs, map[string]any{
281+
"id": seg.Id,
282+
"start": seg.Start.Seconds(),
283+
"end": seg.End.Seconds(),
284+
"text": seg.Text,
285+
})
286+
}
287+
doneEvent["segments"] = segs
288+
}
289+
_ = writeEvent(doneEvent)
264290
_, _ = fmt.Fprintf(c.Response().Writer, "data: [DONE]\n\n")
265291
c.Response().Flush()
266292
return nil

0 commit comments

Comments
 (0)