feat: add usage to TranscriptionResponse (text and json response_format) (#23576)

Signed-off-by: Guillaume Calmettes <gcalmettes@scaleway.com>
This commit is contained in:
Guillaume Calmettes
2025-08-26 05:26:26 -07:00
committed by GitHub
parent 384dd1b0a8
commit ebd5a77bb5
3 changed files with 32 additions and 5 deletions
+6
View File
@@ -2232,9 +2232,15 @@ class TranscriptionRequest(OpenAIBaseModel):
# Transcription response objects
class TranscriptionUsageAudio(OpenAIBaseModel):
type: Literal["duration"] = "duration"
seconds: int
class TranscriptionResponse(OpenAIBaseModel):
text: str
"""The transcribed text."""
usage: TranscriptionUsageAudio
class TranscriptionWord(OpenAIBaseModel):
+16 -1
View File
@@ -200,7 +200,22 @@ class OpenAISpeechToText(OpenAIServing):
for result_generator in list_result_generator:
async for op in result_generator:
text += op.outputs[0].text
return cast(T, response_class(text=text))
if self.task_type == "transcribe":
# add usage in TranscriptionResponse.
usage = {
"type": "duration",
# rounded up as per openAI specs
"seconds": int(math.ceil(duration_s)),
}
final_response = cast(T, response_class(text=text,
usage=usage))
else:
# no usage in response for translation task
final_response = cast(
T, response_class(text=text)) # type: ignore[call-arg]
return final_response
except asyncio.CancelledError:
return self.create_error_response("Client disconnected")
except ValueError as e: