22
33from __future__ import annotations
44
5- from typing import List , Union , Mapping , cast
6- from typing_extensions import Literal
5+ import logging
6+ from typing import TYPE_CHECKING , List , Union , Mapping , cast
7+ from typing_extensions import Literal , overload , assert_never
78
89import httpx
910
2425from ...types .audio_model import AudioModel
2526from ...types .audio .transcription import Transcription
2627from ...types .audio_response_format import AudioResponseFormat
28+ from ...types .audio .transcription_verbose import TranscriptionVerbose
2729
2830__all__ = ["Transcriptions" , "AsyncTranscriptions" ]
2931
32+ log : logging .Logger = logging .getLogger ("openai.audio.transcriptions" )
33+
3034
3135class Transcriptions (SyncAPIResource ):
3236 @cached_property
@@ -48,14 +52,53 @@ def with_streaming_response(self) -> TranscriptionsWithStreamingResponse:
4852 """
4953 return TranscriptionsWithStreamingResponse (self )
5054
55+ @overload
56+ def create (
57+ self ,
58+ * ,
59+ file : FileTypes ,
60+ model : Union [str , AudioModel ],
61+ response_format : Union [Literal ["json" ], NotGiven ] = NOT_GIVEN ,
62+ language : str | NotGiven = NOT_GIVEN ,
63+ prompt : str | NotGiven = NOT_GIVEN ,
64+ temperature : float | NotGiven = NOT_GIVEN ,
65+ timestamp_granularities : List [Literal ["word" , "segment" ]] | NotGiven = NOT_GIVEN ,
66+ # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
67+ # The extra values given here take precedence over values defined on the client or passed to this method.
68+ extra_headers : Headers | None = None ,
69+ extra_query : Query | None = None ,
70+ extra_body : Body | None = None ,
71+ timeout : float | httpx .Timeout | None | NotGiven = NOT_GIVEN ,
72+ ) -> Transcription : ...
73+
74+ @overload
75+ def create (
76+ self ,
77+ * ,
78+ file : FileTypes ,
79+ model : Union [str , AudioModel ],
80+ response_format : Literal ["verbose_json" ],
81+ language : str | NotGiven = NOT_GIVEN ,
82+ prompt : str | NotGiven = NOT_GIVEN ,
83+ temperature : float | NotGiven = NOT_GIVEN ,
84+ timestamp_granularities : List [Literal ["word" , "segment" ]] | NotGiven = NOT_GIVEN ,
85+ # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
86+ # The extra values given here take precedence over values defined on the client or passed to this method.
87+ extra_headers : Headers | None = None ,
88+ extra_query : Query | None = None ,
89+ extra_body : Body | None = None ,
90+ timeout : float | httpx .Timeout | None | NotGiven = NOT_GIVEN ,
91+ ) -> TranscriptionVerbose : ...
92+
93+ @overload
5194 def create (
5295 self ,
5396 * ,
5497 file : FileTypes ,
5598 model : Union [str , AudioModel ],
99+ response_format : Literal ["text" , "srt" , "vtt" ],
56100 language : str | NotGiven = NOT_GIVEN ,
57101 prompt : str | NotGiven = NOT_GIVEN ,
58- response_format : AudioResponseFormat | NotGiven = NOT_GIVEN ,
59102 temperature : float | NotGiven = NOT_GIVEN ,
60103 timestamp_granularities : List [Literal ["word" , "segment" ]] | NotGiven = NOT_GIVEN ,
61104 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
@@ -64,7 +107,25 @@ def create(
64107 extra_query : Query | None = None ,
65108 extra_body : Body | None = None ,
66109 timeout : float | httpx .Timeout | None | NotGiven = NOT_GIVEN ,
67- ) -> Transcription :
110+ ) -> str : ...
111+
112+ def create (
113+ self ,
114+ * ,
115+ file : FileTypes ,
116+ model : Union [str , AudioModel ],
117+ language : str | NotGiven = NOT_GIVEN ,
118+ prompt : str | NotGiven = NOT_GIVEN ,
119+ response_format : Union [AudioResponseFormat , NotGiven ] = NOT_GIVEN ,
120+ temperature : float | NotGiven = NOT_GIVEN ,
121+ timestamp_granularities : List [Literal ["word" , "segment" ]] | NotGiven = NOT_GIVEN ,
122+ # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
123+ # The extra values given here take precedence over values defined on the client or passed to this method.
124+ extra_headers : Headers | None = None ,
125+ extra_query : Query | None = None ,
126+ extra_body : Body | None = None ,
127+ timeout : float | httpx .Timeout | None | NotGiven = NOT_GIVEN ,
128+ ) -> Transcription | TranscriptionVerbose | str :
68129 """
69130 Transcribes audio into the input language.
70131
@@ -124,14 +185,14 @@ def create(
124185 # sent to the server will contain a `boundary` parameter, e.g.
125186 # multipart/form-data; boundary=---abc--
126187 extra_headers = {"Content-Type" : "multipart/form-data" , ** (extra_headers or {})}
127- return self ._post (
188+ return self ._post ( # type: ignore[return-value]
128189 "/audio/transcriptions" ,
129190 body = maybe_transform (body , transcription_create_params .TranscriptionCreateParams ),
130191 files = files ,
131192 options = make_request_options (
132193 extra_headers = extra_headers , extra_query = extra_query , extra_body = extra_body , timeout = timeout
133194 ),
134- cast_to = Transcription ,
195+ cast_to = _get_response_format_type ( response_format ) ,
135196 )
136197
137198
@@ -155,14 +216,15 @@ def with_streaming_response(self) -> AsyncTranscriptionsWithStreamingResponse:
155216 """
156217 return AsyncTranscriptionsWithStreamingResponse (self )
157218
219+ @overload
158220 async def create (
159221 self ,
160222 * ,
161223 file : FileTypes ,
162224 model : Union [str , AudioModel ],
225+ response_format : Union [Literal ["json" ], NotGiven ] = NOT_GIVEN ,
163226 language : str | NotGiven = NOT_GIVEN ,
164227 prompt : str | NotGiven = NOT_GIVEN ,
165- response_format : AudioResponseFormat | NotGiven = NOT_GIVEN ,
166228 temperature : float | NotGiven = NOT_GIVEN ,
167229 timestamp_granularities : List [Literal ["word" , "segment" ]] | NotGiven = NOT_GIVEN ,
168230 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
@@ -171,7 +233,63 @@ async def create(
171233 extra_query : Query | None = None ,
172234 extra_body : Body | None = None ,
173235 timeout : float | httpx .Timeout | None | NotGiven = NOT_GIVEN ,
174- ) -> Transcription :
236+ ) -> Transcription : ...
237+
238+ @overload
239+ async def create (
240+ self ,
241+ * ,
242+ file : FileTypes ,
243+ model : Union [str , AudioModel ],
244+ response_format : Literal ["verbose_json" ],
245+ language : str | NotGiven = NOT_GIVEN ,
246+ prompt : str | NotGiven = NOT_GIVEN ,
247+ temperature : float | NotGiven = NOT_GIVEN ,
248+ timestamp_granularities : List [Literal ["word" , "segment" ]] | NotGiven = NOT_GIVEN ,
249+ # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
250+ # The extra values given here take precedence over values defined on the client or passed to this method.
251+ extra_headers : Headers | None = None ,
252+ extra_query : Query | None = None ,
253+ extra_body : Body | None = None ,
254+ timeout : float | httpx .Timeout | None | NotGiven = NOT_GIVEN ,
255+ ) -> TranscriptionVerbose : ...
256+
257+ @overload
258+ async def create (
259+ self ,
260+ * ,
261+ file : FileTypes ,
262+ model : Union [str , AudioModel ],
263+ response_format : Literal ["text" , "srt" , "vtt" ],
264+ language : str | NotGiven = NOT_GIVEN ,
265+ prompt : str | NotGiven = NOT_GIVEN ,
266+ temperature : float | NotGiven = NOT_GIVEN ,
267+ timestamp_granularities : List [Literal ["word" , "segment" ]] | NotGiven = NOT_GIVEN ,
268+ # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
269+ # The extra values given here take precedence over values defined on the client or passed to this method.
270+ extra_headers : Headers | None = None ,
271+ extra_query : Query | None = None ,
272+ extra_body : Body | None = None ,
273+ timeout : float | httpx .Timeout | None | NotGiven = NOT_GIVEN ,
274+ ) -> str : ...
275+
276+ async def create (
277+ self ,
278+ * ,
279+ file : FileTypes ,
280+ model : Union [str , AudioModel ],
281+ language : str | NotGiven = NOT_GIVEN ,
282+ prompt : str | NotGiven = NOT_GIVEN ,
283+ response_format : Union [AudioResponseFormat , NotGiven ] = NOT_GIVEN ,
284+ temperature : float | NotGiven = NOT_GIVEN ,
285+ timestamp_granularities : List [Literal ["word" , "segment" ]] | NotGiven = NOT_GIVEN ,
286+ # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
287+ # The extra values given here take precedence over values defined on the client or passed to this method.
288+ extra_headers : Headers | None = None ,
289+ extra_query : Query | None = None ,
290+ extra_body : Body | None = None ,
291+ timeout : float | httpx .Timeout | None | NotGiven = NOT_GIVEN ,
292+ ) -> Transcription | TranscriptionVerbose | str :
175293 """
176294 Transcribes audio into the input language.
177295
@@ -238,7 +356,7 @@ async def create(
238356 options = make_request_options (
239357 extra_headers = extra_headers , extra_query = extra_query , extra_body = extra_body , timeout = timeout
240358 ),
241- cast_to = Transcription ,
359+ cast_to = _get_response_format_type ( response_format ) ,
242360 )
243361
244362
@@ -276,3 +394,22 @@ def __init__(self, transcriptions: AsyncTranscriptions) -> None:
276394 self .create = async_to_streamed_response_wrapper (
277395 transcriptions .create ,
278396 )
397+
398+
399+ def _get_response_format_type (
400+ response_format : Literal ["json" , "text" , "srt" , "verbose_json" , "vtt" ] | NotGiven ,
401+ ) -> type [Transcription | TranscriptionVerbose | str ]:
402+ if isinstance (response_format , NotGiven ) or response_format is None : # pyright: ignore[reportUnnecessaryComparison]
403+ return Transcription
404+
405+ if response_format == "json" :
406+ return Transcription
407+ elif response_format == "verbose_json" :
408+ return TranscriptionVerbose
409+ elif response_format == "srt" or response_format == "text" or response_format == "vtt" :
410+ return str
411+ elif TYPE_CHECKING : # type: ignore[unreachable]
412+ assert_never (response_format )
413+ else :
414+ log .warn ("Unexpected audio response format: %s" , response_format )
415+ return Transcription
0 commit comments