2
2
3
3
from __future__ import annotations
4
4
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
7
8
8
9
import httpx
9
10
24
25
from ...types .audio_model import AudioModel
25
26
from ...types .audio .transcription import Transcription
26
27
from ...types .audio_response_format import AudioResponseFormat
28
+ from ...types .audio .transcription_verbose import TranscriptionVerbose
27
29
28
30
__all__ = ["Transcriptions" , "AsyncTranscriptions" ]
29
31
32
+ log : logging .Logger = logging .getLogger ("openai.audio.transcriptions" )
33
+
30
34
31
35
class Transcriptions (SyncAPIResource ):
32
36
@cached_property
@@ -48,14 +52,53 @@ def with_streaming_response(self) -> TranscriptionsWithStreamingResponse:
48
52
"""
49
53
return TranscriptionsWithStreamingResponse (self )
50
54
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
51
94
def create (
52
95
self ,
53
96
* ,
54
97
file : FileTypes ,
55
98
model : Union [str , AudioModel ],
99
+ response_format : Literal ["text" , "srt" , "vtt" ],
56
100
language : str | NotGiven = NOT_GIVEN ,
57
101
prompt : str | NotGiven = NOT_GIVEN ,
58
- response_format : AudioResponseFormat | NotGiven = NOT_GIVEN ,
59
102
temperature : float | NotGiven = NOT_GIVEN ,
60
103
timestamp_granularities : List [Literal ["word" , "segment" ]] | NotGiven = NOT_GIVEN ,
61
104
# 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(
64
107
extra_query : Query | None = None ,
65
108
extra_body : Body | None = None ,
66
109
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 :
68
129
"""
69
130
Transcribes audio into the input language.
70
131
@@ -124,14 +185,14 @@ def create(
124
185
# sent to the server will contain a `boundary` parameter, e.g.
125
186
# multipart/form-data; boundary=---abc--
126
187
extra_headers = {"Content-Type" : "multipart/form-data" , ** (extra_headers or {})}
127
- return self ._post (
188
+ return self ._post ( # type: ignore[return-value]
128
189
"/audio/transcriptions" ,
129
190
body = maybe_transform (body , transcription_create_params .TranscriptionCreateParams ),
130
191
files = files ,
131
192
options = make_request_options (
132
193
extra_headers = extra_headers , extra_query = extra_query , extra_body = extra_body , timeout = timeout
133
194
),
134
- cast_to = Transcription ,
195
+ cast_to = _get_response_format_type ( response_format ) ,
135
196
)
136
197
137
198
@@ -155,14 +216,15 @@ def with_streaming_response(self) -> AsyncTranscriptionsWithStreamingResponse:
155
216
"""
156
217
return AsyncTranscriptionsWithStreamingResponse (self )
157
218
219
+ @overload
158
220
async def create (
159
221
self ,
160
222
* ,
161
223
file : FileTypes ,
162
224
model : Union [str , AudioModel ],
225
+ response_format : Union [Literal ["json" ], NotGiven ] = NOT_GIVEN ,
163
226
language : str | NotGiven = NOT_GIVEN ,
164
227
prompt : str | NotGiven = NOT_GIVEN ,
165
- response_format : AudioResponseFormat | NotGiven = NOT_GIVEN ,
166
228
temperature : float | NotGiven = NOT_GIVEN ,
167
229
timestamp_granularities : List [Literal ["word" , "segment" ]] | NotGiven = NOT_GIVEN ,
168
230
# 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(
171
233
extra_query : Query | None = None ,
172
234
extra_body : Body | None = None ,
173
235
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 :
175
293
"""
176
294
Transcribes audio into the input language.
177
295
@@ -238,7 +356,7 @@ async def create(
238
356
options = make_request_options (
239
357
extra_headers = extra_headers , extra_query = extra_query , extra_body = extra_body , timeout = timeout
240
358
),
241
- cast_to = Transcription ,
359
+ cast_to = _get_response_format_type ( response_format ) ,
242
360
)
243
361
244
362
@@ -276,3 +394,22 @@ def __init__(self, transcriptions: AsyncTranscriptions) -> None:
276
394
self .create = async_to_streamed_response_wrapper (
277
395
transcriptions .create ,
278
396
)
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