openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/audio/test_transcriptions.py
222lines · modeblame
5cfb125aStainless Bot2 years ago | 1 | # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
08b8179aDavid Schnurr2 years ago | 2 | |
| 3 | from __future__ import annotations | |
| 4 | | |
| 5 | import os | |
86379b44Stainless Bot2 years ago | 6 | from typing import Any, cast |
08b8179aDavid Schnurr2 years ago | 7 | |
| 8 | import pytest | |
| 9 | | |
| 10 | from openai import OpenAI, AsyncOpenAI | |
| 11 | from tests.utils import assert_matches_type | |
adb6da3aRobert Craigie1 years ago | 12 | from openai.types.audio import TranscriptionCreateResponse |
08b8179aDavid Schnurr2 years ago | 13 | |
| 14 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") | |
| 15 | | |
| 16 | | |
| 17 | class TestTranscriptions: | |
98d779fbStainless Bot2 years ago | 18 | parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) |
08b8179aDavid Schnurr2 years ago | 19 | |
| 20 | @parametrize | |
2b4bc759stainless-app[bot]1 years ago | 21 | def test_method_create_overload_1(self, client: OpenAI) -> None: |
08b8179aDavid Schnurr2 years ago | 22 | transcription = client.audio.transcriptions.create( |
| 23 | file=b"raw file contents", | |
2b4bc759stainless-app[bot]1 years ago | 24 | model="gpt-4o-transcribe", |
08b8179aDavid Schnurr2 years ago | 25 | ) |
adb6da3aRobert Craigie1 years ago | 26 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
08b8179aDavid Schnurr2 years ago | 27 | |
| 28 | @parametrize | |
2b4bc759stainless-app[bot]1 years ago | 29 | def test_method_create_with_all_params_overload_1(self, client: OpenAI) -> None: |
08b8179aDavid Schnurr2 years ago | 30 | transcription = client.audio.transcriptions.create( |
| 31 | file=b"raw file contents", | |
2b4bc759stainless-app[bot]1 years ago | 32 | model="gpt-4o-transcribe", |
| 33 | include=["logprobs"], | |
| 34 | language="language", | |
| 35 | prompt="prompt", | |
08b8179aDavid Schnurr2 years ago | 36 | response_format="json", |
2b4bc759stainless-app[bot]1 years ago | 37 | stream=False, |
08b8179aDavid Schnurr2 years ago | 38 | temperature=0, |
dd19d4f9Stainless Bot1 years ago | 39 | timestamp_granularities=["word"], |
08b8179aDavid Schnurr2 years ago | 40 | ) |
adb6da3aRobert Craigie1 years ago | 41 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
08b8179aDavid Schnurr2 years ago | 42 | |
| 43 | @parametrize | |
2b4bc759stainless-app[bot]1 years ago | 44 | def test_raw_response_create_overload_1(self, client: OpenAI) -> None: |
08b8179aDavid Schnurr2 years ago | 45 | response = client.audio.transcriptions.with_raw_response.create( |
| 46 | file=b"raw file contents", | |
2b4bc759stainless-app[bot]1 years ago | 47 | model="gpt-4o-transcribe", |
08b8179aDavid Schnurr2 years ago | 48 | ) |
86379b44Stainless Bot2 years ago | 49 | |
| 50 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 51 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 52 | transcription = response.parse() | |
adb6da3aRobert Craigie1 years ago | 53 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
08b8179aDavid Schnurr2 years ago | 54 | |
86379b44Stainless Bot2 years ago | 55 | @parametrize |
2b4bc759stainless-app[bot]1 years ago | 56 | def test_streaming_response_create_overload_1(self, client: OpenAI) -> None: |
86379b44Stainless Bot2 years ago | 57 | with client.audio.transcriptions.with_streaming_response.create( |
| 58 | file=b"raw file contents", | |
2b4bc759stainless-app[bot]1 years ago | 59 | model="gpt-4o-transcribe", |
86379b44Stainless Bot2 years ago | 60 | ) as response: |
| 61 | assert not response.is_closed | |
| 62 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 63 | | |
| 64 | transcription = response.parse() | |
adb6da3aRobert Craigie1 years ago | 65 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
86379b44Stainless Bot2 years ago | 66 | |
| 67 | assert cast(Any, response.is_closed) is True | |
| 68 | | |
2b4bc759stainless-app[bot]1 years ago | 69 | @parametrize |
| 70 | def test_method_create_overload_2(self, client: OpenAI) -> None: | |
| 71 | transcription_stream = client.audio.transcriptions.create( | |
| 72 | file=b"raw file contents", | |
| 73 | model="gpt-4o-transcribe", | |
| 74 | stream=True, | |
| 75 | ) | |
| 76 | transcription_stream.response.close() | |
| 77 | | |
| 78 | @parametrize | |
| 79 | def test_method_create_with_all_params_overload_2(self, client: OpenAI) -> None: | |
| 80 | transcription_stream = client.audio.transcriptions.create( | |
| 81 | file=b"raw file contents", | |
| 82 | model="gpt-4o-transcribe", | |
| 83 | stream=True, | |
| 84 | include=["logprobs"], | |
| 85 | language="language", | |
| 86 | prompt="prompt", | |
| 87 | response_format="json", | |
| 88 | temperature=0, | |
| 89 | timestamp_granularities=["word"], | |
| 90 | ) | |
| 91 | transcription_stream.response.close() | |
| 92 | | |
| 93 | @parametrize | |
| 94 | def test_raw_response_create_overload_2(self, client: OpenAI) -> None: | |
| 95 | response = client.audio.transcriptions.with_raw_response.create( | |
| 96 | file=b"raw file contents", | |
| 97 | model="gpt-4o-transcribe", | |
| 98 | stream=True, | |
| 99 | ) | |
| 100 | | |
| 101 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 102 | stream = response.parse() | |
| 103 | stream.close() | |
| 104 | | |
| 105 | @parametrize | |
| 106 | def test_streaming_response_create_overload_2(self, client: OpenAI) -> None: | |
| 107 | with client.audio.transcriptions.with_streaming_response.create( | |
| 108 | file=b"raw file contents", | |
| 109 | model="gpt-4o-transcribe", | |
| 110 | stream=True, | |
| 111 | ) as response: | |
| 112 | assert not response.is_closed | |
| 113 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 114 | | |
| 115 | stream = response.parse() | |
| 116 | stream.close() | |
| 117 | | |
| 118 | assert cast(Any, response.is_closed) is True | |
| 119 | | |
08b8179aDavid Schnurr2 years ago | 120 | |
| 121 | class TestAsyncTranscriptions: | |
98d779fbStainless Bot2 years ago | 122 | parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) |
08b8179aDavid Schnurr2 years ago | 123 | |
| 124 | @parametrize | |
2b4bc759stainless-app[bot]1 years ago | 125 | async def test_method_create_overload_1(self, async_client: AsyncOpenAI) -> None: |
98d779fbStainless Bot2 years ago | 126 | transcription = await async_client.audio.transcriptions.create( |
08b8179aDavid Schnurr2 years ago | 127 | file=b"raw file contents", |
2b4bc759stainless-app[bot]1 years ago | 128 | model="gpt-4o-transcribe", |
08b8179aDavid Schnurr2 years ago | 129 | ) |
adb6da3aRobert Craigie1 years ago | 130 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
08b8179aDavid Schnurr2 years ago | 131 | |
| 132 | @parametrize | |
2b4bc759stainless-app[bot]1 years ago | 133 | async def test_method_create_with_all_params_overload_1(self, async_client: AsyncOpenAI) -> None: |
98d779fbStainless Bot2 years ago | 134 | transcription = await async_client.audio.transcriptions.create( |
08b8179aDavid Schnurr2 years ago | 135 | file=b"raw file contents", |
2b4bc759stainless-app[bot]1 years ago | 136 | model="gpt-4o-transcribe", |
| 137 | include=["logprobs"], | |
| 138 | language="language", | |
| 139 | prompt="prompt", | |
08b8179aDavid Schnurr2 years ago | 140 | response_format="json", |
2b4bc759stainless-app[bot]1 years ago | 141 | stream=False, |
08b8179aDavid Schnurr2 years ago | 142 | temperature=0, |
dd19d4f9Stainless Bot1 years ago | 143 | timestamp_granularities=["word"], |
08b8179aDavid Schnurr2 years ago | 144 | ) |
adb6da3aRobert Craigie1 years ago | 145 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
08b8179aDavid Schnurr2 years ago | 146 | |
| 147 | @parametrize | |
2b4bc759stainless-app[bot]1 years ago | 148 | async def test_raw_response_create_overload_1(self, async_client: AsyncOpenAI) -> None: |
98d779fbStainless Bot2 years ago | 149 | response = await async_client.audio.transcriptions.with_raw_response.create( |
08b8179aDavid Schnurr2 years ago | 150 | file=b"raw file contents", |
2b4bc759stainless-app[bot]1 years ago | 151 | model="gpt-4o-transcribe", |
08b8179aDavid Schnurr2 years ago | 152 | ) |
86379b44Stainless Bot2 years ago | 153 | |
| 154 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 155 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 156 | transcription = response.parse() | |
adb6da3aRobert Craigie1 years ago | 157 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
86379b44Stainless Bot2 years ago | 158 | |
| 159 | @parametrize | |
2b4bc759stainless-app[bot]1 years ago | 160 | async def test_streaming_response_create_overload_1(self, async_client: AsyncOpenAI) -> None: |
98d779fbStainless Bot2 years ago | 161 | async with async_client.audio.transcriptions.with_streaming_response.create( |
86379b44Stainless Bot2 years ago | 162 | file=b"raw file contents", |
2b4bc759stainless-app[bot]1 years ago | 163 | model="gpt-4o-transcribe", |
86379b44Stainless Bot2 years ago | 164 | ) as response: |
| 165 | assert not response.is_closed | |
| 166 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 167 | | |
| 168 | transcription = await response.parse() | |
adb6da3aRobert Craigie1 years ago | 169 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
86379b44Stainless Bot2 years ago | 170 | |
| 171 | assert cast(Any, response.is_closed) is True | |
2b4bc759stainless-app[bot]1 years ago | 172 | |
| 173 | @parametrize | |
| 174 | async def test_method_create_overload_2(self, async_client: AsyncOpenAI) -> None: | |
| 175 | transcription_stream = await async_client.audio.transcriptions.create( | |
| 176 | file=b"raw file contents", | |
| 177 | model="gpt-4o-transcribe", | |
| 178 | stream=True, | |
| 179 | ) | |
| 180 | await transcription_stream.response.aclose() | |
| 181 | | |
| 182 | @parametrize | |
| 183 | async def test_method_create_with_all_params_overload_2(self, async_client: AsyncOpenAI) -> None: | |
| 184 | transcription_stream = await async_client.audio.transcriptions.create( | |
| 185 | file=b"raw file contents", | |
| 186 | model="gpt-4o-transcribe", | |
| 187 | stream=True, | |
| 188 | include=["logprobs"], | |
| 189 | language="language", | |
| 190 | prompt="prompt", | |
| 191 | response_format="json", | |
| 192 | temperature=0, | |
| 193 | timestamp_granularities=["word"], | |
| 194 | ) | |
| 195 | await transcription_stream.response.aclose() | |
| 196 | | |
| 197 | @parametrize | |
| 198 | async def test_raw_response_create_overload_2(self, async_client: AsyncOpenAI) -> None: | |
| 199 | response = await async_client.audio.transcriptions.with_raw_response.create( | |
| 200 | file=b"raw file contents", | |
| 201 | model="gpt-4o-transcribe", | |
| 202 | stream=True, | |
| 203 | ) | |
| 204 | | |
| 205 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 206 | stream = response.parse() | |
| 207 | await stream.close() | |
| 208 | | |
| 209 | @parametrize | |
| 210 | async def test_streaming_response_create_overload_2(self, async_client: AsyncOpenAI) -> None: | |
| 211 | async with async_client.audio.transcriptions.with_streaming_response.create( | |
| 212 | file=b"raw file contents", | |
| 213 | model="gpt-4o-transcribe", | |
| 214 | stream=True, | |
| 215 | ) as response: | |
| 216 | assert not response.is_closed | |
| 217 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 218 | | |
| 219 | stream = await response.parse() | |
| 220 | await stream.close() | |
| 221 | | |
| 222 | assert cast(Any, response.is_closed) is True |