openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/audio/test_transcriptions.py
116lines · 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 | |
| 21 | def test_method_create(self, client: OpenAI) -> None: | |
| 22 | transcription = client.audio.transcriptions.create( | |
| 23 | file=b"raw file contents", | |
| 24 | model="whisper-1", | |
| 25 | ) | |
adb6da3aRobert Craigie1 years ago | 26 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
08b8179aDavid Schnurr2 years ago | 27 | |
| 28 | @parametrize | |
| 29 | def test_method_create_with_all_params(self, client: OpenAI) -> None: | |
| 30 | transcription = client.audio.transcriptions.create( | |
| 31 | file=b"raw file contents", | |
| 32 | model="whisper-1", | |
| 33 | language="string", | |
| 34 | prompt="string", | |
| 35 | response_format="json", | |
| 36 | temperature=0, | |
dd19d4f9Stainless Bot1 years ago | 37 | timestamp_granularities=["word"], |
08b8179aDavid Schnurr2 years ago | 38 | ) |
adb6da3aRobert Craigie1 years ago | 39 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
08b8179aDavid Schnurr2 years ago | 40 | |
| 41 | @parametrize | |
| 42 | def test_raw_response_create(self, client: OpenAI) -> None: | |
| 43 | response = client.audio.transcriptions.with_raw_response.create( | |
| 44 | file=b"raw file contents", | |
| 45 | model="whisper-1", | |
| 46 | ) | |
86379b44Stainless Bot2 years ago | 47 | |
| 48 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 49 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 50 | transcription = response.parse() | |
adb6da3aRobert Craigie1 years ago | 51 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
08b8179aDavid Schnurr2 years ago | 52 | |
86379b44Stainless Bot2 years ago | 53 | @parametrize |
| 54 | def test_streaming_response_create(self, client: OpenAI) -> None: | |
| 55 | with client.audio.transcriptions.with_streaming_response.create( | |
| 56 | file=b"raw file contents", | |
| 57 | model="whisper-1", | |
| 58 | ) as response: | |
| 59 | assert not response.is_closed | |
| 60 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 61 | | |
| 62 | transcription = response.parse() | |
adb6da3aRobert Craigie1 years ago | 63 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
86379b44Stainless Bot2 years ago | 64 | |
| 65 | assert cast(Any, response.is_closed) is True | |
| 66 | | |
08b8179aDavid Schnurr2 years ago | 67 | |
| 68 | class TestAsyncTranscriptions: | |
98d779fbStainless Bot2 years ago | 69 | parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) |
08b8179aDavid Schnurr2 years ago | 70 | |
| 71 | @parametrize | |
98d779fbStainless Bot2 years ago | 72 | async def test_method_create(self, async_client: AsyncOpenAI) -> None: |
| 73 | transcription = await async_client.audio.transcriptions.create( | |
08b8179aDavid Schnurr2 years ago | 74 | file=b"raw file contents", |
| 75 | model="whisper-1", | |
| 76 | ) | |
adb6da3aRobert Craigie1 years ago | 77 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
08b8179aDavid Schnurr2 years ago | 78 | |
| 79 | @parametrize | |
98d779fbStainless Bot2 years ago | 80 | async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 81 | transcription = await async_client.audio.transcriptions.create( | |
08b8179aDavid Schnurr2 years ago | 82 | file=b"raw file contents", |
| 83 | model="whisper-1", | |
| 84 | language="string", | |
| 85 | prompt="string", | |
| 86 | response_format="json", | |
| 87 | temperature=0, | |
dd19d4f9Stainless Bot1 years ago | 88 | timestamp_granularities=["word"], |
08b8179aDavid Schnurr2 years ago | 89 | ) |
adb6da3aRobert Craigie1 years ago | 90 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
08b8179aDavid Schnurr2 years ago | 91 | |
| 92 | @parametrize | |
98d779fbStainless Bot2 years ago | 93 | async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None: |
| 94 | response = await async_client.audio.transcriptions.with_raw_response.create( | |
08b8179aDavid Schnurr2 years ago | 95 | file=b"raw file contents", |
| 96 | model="whisper-1", | |
| 97 | ) | |
86379b44Stainless Bot2 years ago | 98 | |
| 99 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 100 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 101 | transcription = response.parse() | |
adb6da3aRobert Craigie1 years ago | 102 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
86379b44Stainless Bot2 years ago | 103 | |
| 104 | @parametrize | |
98d779fbStainless Bot2 years ago | 105 | async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None: |
| 106 | async with async_client.audio.transcriptions.with_streaming_response.create( | |
86379b44Stainless Bot2 years ago | 107 | file=b"raw file contents", |
| 108 | model="whisper-1", | |
| 109 | ) as response: | |
| 110 | assert not response.is_closed | |
| 111 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 112 | | |
| 113 | transcription = await response.parse() | |
adb6da3aRobert Craigie1 years ago | 114 | assert_matches_type(TranscriptionCreateResponse, transcription, path=["response"]) |
86379b44Stainless Bot2 years ago | 115 | |
| 116 | assert cast(Any, response.is_closed) is True |