openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/api_resources/audio/test_transcriptions.py
228lines · modecode
| 1 | # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import os |
| 6 | from typing import Any, cast |
| 7 | |
| 8 | import pytest |
| 9 | |
| 10 | from openai import OpenAI, AsyncOpenAI |
| 11 | from tests.utils import assert_matches_type |
| 12 | from openai.types.audio import TranscriptionCreateResponse |
| 13 | |
| 14 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 15 | |
| 16 | |
| 17 | class TestTranscriptions: |
| 18 | parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 19 | |
| 20 | @parametrize |
| 21 | def test_method_create_overload_1(self, client: OpenAI) -> None: |
| 22 | transcription = client.audio.transcriptions.create( |
| 23 | file=b"raw file contents", |
| 24 | model="gpt-4o-transcribe", |
| 25 | ) |
| 26 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
| 27 | |
| 28 | @parametrize |
| 29 | def test_method_create_with_all_params_overload_1(self, client: OpenAI) -> None: |
| 30 | transcription = client.audio.transcriptions.create( |
| 31 | file=b"raw file contents", |
| 32 | model="gpt-4o-transcribe", |
| 33 | chunking_strategy="auto", |
| 34 | include=["logprobs"], |
| 35 | language="language", |
| 36 | prompt="prompt", |
| 37 | response_format="json", |
| 38 | stream=False, |
| 39 | temperature=0, |
| 40 | timestamp_granularities=["word"], |
| 41 | ) |
| 42 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
| 43 | |
| 44 | @parametrize |
| 45 | def test_raw_response_create_overload_1(self, client: OpenAI) -> None: |
| 46 | response = client.audio.transcriptions.with_raw_response.create( |
| 47 | file=b"raw file contents", |
| 48 | model="gpt-4o-transcribe", |
| 49 | ) |
| 50 | |
| 51 | assert response.is_closed is True |
| 52 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 53 | transcription = response.parse() |
| 54 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
| 55 | |
| 56 | @parametrize |
| 57 | def test_streaming_response_create_overload_1(self, client: OpenAI) -> None: |
| 58 | with client.audio.transcriptions.with_streaming_response.create( |
| 59 | file=b"raw file contents", |
| 60 | model="gpt-4o-transcribe", |
| 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() |
| 66 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
| 67 | |
| 68 | assert cast(Any, response.is_closed) is True |
| 69 | |
| 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, |
| 85 | chunking_strategy="auto", |
| 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 | |
| 122 | |
| 123 | class TestAsyncTranscriptions: |
| 124 | parametrize = pytest.mark.parametrize( |
| 125 | "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"] |
| 126 | ) |
| 127 | |
| 128 | @parametrize |
| 129 | async def test_method_create_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 130 | transcription = await async_client.audio.transcriptions.create( |
| 131 | file=b"raw file contents", |
| 132 | model="gpt-4o-transcribe", |
| 133 | ) |
| 134 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
| 135 | |
| 136 | @parametrize |
| 137 | async def test_method_create_with_all_params_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 138 | transcription = await async_client.audio.transcriptions.create( |
| 139 | file=b"raw file contents", |
| 140 | model="gpt-4o-transcribe", |
| 141 | chunking_strategy="auto", |
| 142 | include=["logprobs"], |
| 143 | language="language", |
| 144 | prompt="prompt", |
| 145 | response_format="json", |
| 146 | stream=False, |
| 147 | temperature=0, |
| 148 | timestamp_granularities=["word"], |
| 149 | ) |
| 150 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
| 151 | |
| 152 | @parametrize |
| 153 | async def test_raw_response_create_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 154 | response = await async_client.audio.transcriptions.with_raw_response.create( |
| 155 | file=b"raw file contents", |
| 156 | model="gpt-4o-transcribe", |
| 157 | ) |
| 158 | |
| 159 | assert response.is_closed is True |
| 160 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 161 | transcription = response.parse() |
| 162 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
| 163 | |
| 164 | @parametrize |
| 165 | async def test_streaming_response_create_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 166 | async with async_client.audio.transcriptions.with_streaming_response.create( |
| 167 | file=b"raw file contents", |
| 168 | model="gpt-4o-transcribe", |
| 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() |
| 174 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
| 175 | |
| 176 | assert cast(Any, response.is_closed) is True |
| 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, |
| 193 | chunking_strategy="auto", |
| 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 |
| 229 | |