Add SSE stream_format support for audio speech streaming (#4490)

Signed-off-by: syd520zy <529477025@qq.com>
Signed-off-by: faceless void <100186390+syd520zy@users.noreply.github.com>
Signed-off-by: Nick Cao <ncao@redhat.com>
Signed-off-by: Chendi Xue <chendi.xue@intel.com>
Signed-off-by: kTorp <Kristoffer.Torp@amd.com>
Signed-off-by: Junhong Liu <98734602+LJH-LBJ@users.noreply.github.com>
Signed-off-by: Hongsheng Liu <liuhongsheng4@huawei.com>
Signed-off-by: Hyoseop Song <crad_on25@naver.com>
Signed-off-by: Hyoseop Song  <crad_on25@naver.com>
Signed-off-by: amy-why-3459 <wuhaiyan17@huawei.com>
Co-authored-by: Nick Cao <ncao@redhat.com>
Co-authored-by: Chendi.Xue <chendi.xue@intel.com>
Co-authored-by: Hongsheng Liu <liuhongsheng4@huawei.com>
Co-authored-by: Kristoffer <kristoffer.torp@amd.com>
Co-authored-by: Junhong Liu <ljh_lbj@163.com>
Co-authored-by: lishunyang <lishunyang12@163.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Hyoseop Song <crad_on25@naver.com>
Co-authored-by: Zhou Taichang <tzhouam@connect.ust.hk>
Co-authored-by: Alicia <115451386+congw729@users.noreply.github.com>
Co-authored-by: amy-why-3459 <wuhaiyan17@huawei.com>
This commit is contained in:
9 files changed
+320 -51

No files matched your search

+22 -11
View File
@@ -65,12 +65,18 @@ class OpenAICreateSpeechRequest(BaseModel):
ge=0.25,
le=4.0,
)
stream_format: Literal["sse", "audio"] | None = "audio"
stream_format: Literal["sse", "audio"] | None = Field(
default=None,
description=(
"Streaming output format. 'audio' streams raw pcm/wav bytes; "
"'sse' streams OpenAI speech.audio.* SSE events. Omit for non-streaming."
),
)
stream: bool = Field(
default=False,
description=(
"If true, stream raw PCM audio chunks as they are decoded. "
"Requires response_format='pcm'. Speed adjustment is not supported when streaming."
"Legacy streaming switch; equivalent to stream_format='audio'. "
"Requires response_format='pcm' or 'wav'. Speed adjustment is not supported when streaming."
),
)
@@ -158,8 +164,6 @@ class OpenAICreateSpeechRequest(BaseModel):
@field_validator("stream_format")
@classmethod
def validate_stream_format(cls, v: str) -> str:
if v == "sse":
raise ValueError("'sse' is not a supported stream_format yet. Please use 'audio'.")
return v
@field_validator("ref_audio", mode="before")
@@ -287,20 +291,28 @@ class OpenAICreateSpeechRequest(BaseModel):
raise ValueError("'speaker_embedding' and 'ref_audio' are mutually exclusive")
return self
def is_raw_audio_stream(self) -> bool:
return self.stream or self.stream_format == "audio"
def is_sse_stream(self) -> bool:
return self.stream_format == "sse" and not self.is_raw_audio_stream()
def is_streaming(self) -> bool:
return self.is_raw_audio_stream() or self.stream_format == "sse"
@model_validator(mode="after")
def validate_streaming_constraints(self) -> "OpenAICreateSpeechRequest":
if self.stream:
if self.is_streaming():
if self.response_format not in ("pcm", "wav"):
raise ValueError(
"Streaming (stream=true) requires response_format='pcm' or 'wav'. "
"Streaming (stream=true, stream_format='audio', or stream_format='sse') "
"requires response_format='pcm' or 'wav'. "
f"Got response_format='{self.response_format}'."
)
if self.speed is None:
self.speed = 1.0
elif self.speed != 1.0:
raise ValueError(
"Speed adjustment is not supported when streaming (stream=true). Set speed=1.0 or omit it."
)
raise ValueError("Speed adjustment is not supported when streaming. Set speed=1.0 or omit it.")
return self
@@ -356,7 +368,6 @@ class CreateAudio(BaseModel):
sample_rate: int = 24000
response_format: str = "wav"
speed: float = 1.0
stream_format: Literal["sse", "audio"] | None = "audio"
base64_encode: bool = True
class Config: