Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 76c1f3f

Browse files
committedSep 27, 2024·
fix(audio): correct types for transcriptions / translations (#1755)
fix
1 parent 7a5632f commit 76c1f3f

17 files changed

+543
-43
lines changed
 

‎.stats.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
configured_endpoints: 68
2-
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/openai-17ddd746c775ca4d4fbe64e5621ac30756ef09c061ff6313190b6ec162222d4c.yml
2+
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/openai-71e58a77027c67e003fdd1b1ac8ac11557d8bfabc7666d1a827c6b1ca8ab98b5.yml

‎api.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -122,24 +122,30 @@ from openai.types import AudioModel, AudioResponseFormat
122122
Types:
123123

124124
```python
125-
from openai.types.audio import Transcription
125+
from openai.types.audio import (
126+
Transcription,
127+
TranscriptionSegment,
128+
TranscriptionVerbose,
129+
TranscriptionWord,
130+
TranscriptionCreateResponse,
131+
)
126132
```
127133

128134
Methods:
129135

130-
- <code title="post /audio/transcriptions">client.audio.transcriptions.<a href="./src/openai/resources/audio/transcriptions.py">create</a>(\*\*<a href="src/openai/types/audio/transcription_create_params.py">params</a>) -> <a href="./src/openai/types/audio/transcription.py">Transcription</a></code>
136+
- <code title="post /audio/transcriptions">client.audio.transcriptions.<a href="./src/openai/resources/audio/transcriptions.py">create</a>(\*\*<a href="src/openai/types/audio/transcription_create_params.py">params</a>) -> <a href="./src/openai/types/audio/transcription_create_response.py">TranscriptionCreateResponse</a></code>
131137

132138
## Translations
133139

134140
Types:
135141

136142
```python
137-
from openai.types.audio import Translation
143+
from openai.types.audio import Translation, TranslationVerbose, TranslationCreateResponse
138144
```
139145

140146
Methods:
141147

142-
- <code title="post /audio/translations">client.audio.translations.<a href="./src/openai/resources/audio/translations.py">create</a>(\*\*<a href="src/openai/types/audio/translation_create_params.py">params</a>) -> <a href="./src/openai/types/audio/translation.py">Translation</a></code>
148+
- <code title="post /audio/translations">client.audio.translations.<a href="./src/openai/resources/audio/translations.py">create</a>(\*\*<a href="src/openai/types/audio/translation_create_params.py">params</a>) -> <a href="./src/openai/types/audio/translation_create_response.py">TranslationCreateResponse</a></code>
143149

144150
## Speech
145151

‎src/openai/_utils/_reflection.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def assert_signatures_in_sync(
1515
check_func: Callable[..., Any],
1616
*,
1717
exclude_params: set[str] = set(),
18+
description: str = "",
1819
) -> None:
1920
"""Ensure that the signature of the second function matches the first."""
2021

@@ -39,4 +40,6 @@ def assert_signatures_in_sync(
3940
continue
4041

4142
if errors:
42-
raise AssertionError(f"{len(errors)} errors encountered when comparing signatures:\n\n" + "\n\n".join(errors))
43+
raise AssertionError(
44+
f"{len(errors)} errors encountered when comparing signatures{description}:\n\n" + "\n\n".join(errors)
45+
)

‎src/openai/resources/audio/transcriptions.py

+146-9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
from __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

89
import httpx
910

@@ -24,9 +25,12 @@
2425
from ...types.audio_model import AudioModel
2526
from ...types.audio.transcription import Transcription
2627
from ...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

3135
class 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

Comments
 (0)
Please sign in to comment.