Skip to content

Commit 5b0196c

Browse files
committed
fix(whisper): scrub invalid UTF-8 from segment text before protobuf marshal
whisper.cpp can emit bytes that are not valid UTF-8 — typically a multibyte codepoint split across token boundaries. protobuf string fields reject those at marshal time, which would surface as a transcribe failure. Run strings.ToValidUTF8 on the segment text before it leaves the cgo boundary so the bad byte gets replaced with U+FFFD. Signed-off-by: Ettore Di Giacinto <mudler@localai.io> Assisted-by: Claude:claude-opus-4-7 [Claude Code]
1 parent c8d63a1 commit 5b0196c

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

backend/go/whisper/gowhisper.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ func (w *Whisper) AudioTranscription(opts *pb.TranscriptRequest) (pb.TranscriptR
139139
// segment start/end conversion factor taken from https://github.com/ggml-org/whisper.cpp/blob/master/examples/cli/cli.cpp#L895
140140
s := CppGetSegmentStart(i) * (10000000)
141141
t := CppGetSegmentEnd(i) * (10000000)
142-
txt := strings.Clone(CppGetSegmentText(i))
142+
// whisper.cpp can emit bytes that aren't valid UTF-8 (e.g. a multibyte
143+
// codepoint split across token boundaries); protobuf string fields
144+
// reject those at marshal time. Scrub before the value escapes cgo.
145+
txt := strings.ToValidUTF8(strings.Clone(CppGetSegmentText(i)), "�")
143146
tokens := make([]int32, CppNTokens(i))
144147

145148
if opts.Diarize && CppGetSegmentSpeakerTurnNext(i) {

0 commit comments

Comments
 (0)