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