openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/realtime/test_client_secrets.py
210lines · modeblame
3d3d16abstainless-app[bot]9 months ago | 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.realtime import ClientSecretCreateResponse | |
| 13 | | |
| 14 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") | |
| 15 | | |
| 16 | | |
| 17 | class TestClientSecrets: | |
| 18 | parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) | |
| 19 | | |
| 20 | @parametrize | |
| 21 | def test_method_create(self, client: OpenAI) -> None: | |
| 22 | client_secret = client.realtime.client_secrets.create() | |
| 23 | assert_matches_type(ClientSecretCreateResponse, client_secret, path=["response"]) | |
| 24 | | |
| 25 | @parametrize | |
| 26 | def test_method_create_with_all_params(self, client: OpenAI) -> None: | |
| 27 | client_secret = client.realtime.client_secrets.create( | |
| 28 | expires_after={ | |
| 29 | "anchor": "created_at", | |
| 30 | "seconds": 10, | |
| 31 | }, | |
| 32 | session={ | |
| 33 | "type": "realtime", | |
| 34 | "audio": { | |
| 35 | "input": { | |
0296375fstainless-app[bot]9 months ago | 36 | "format": { |
| 37 | "rate": 24000, | |
| 38 | "type": "audio/pcm", | |
| 39 | }, | |
3d3d16abstainless-app[bot]9 months ago | 40 | "noise_reduction": {"type": "near_field"}, |
| 41 | "transcription": { | |
8fe0ab87stainless-app[bot]1 months ago | 42 | "delay": "minimal", |
3d3d16abstainless-app[bot]9 months ago | 43 | "language": "language", |
e507a4ebstainless-app[bot]2 months ago | 44 | "model": "whisper-1", |
3d3d16abstainless-app[bot]9 months ago | 45 | "prompt": "prompt", |
| 46 | }, | |
| 47 | "turn_detection": { | |
4756247cstainless-app[bot]9 months ago | 48 | "type": "server_vad", |
3d3d16abstainless-app[bot]9 months ago | 49 | "create_response": True, |
4756247cstainless-app[bot]9 months ago | 50 | "idle_timeout_ms": 5000, |
3d3d16abstainless-app[bot]9 months ago | 51 | "interrupt_response": True, |
| 52 | "prefix_padding_ms": 0, | |
| 53 | "silence_duration_ms": 0, | |
| 54 | "threshold": 0, | |
| 55 | }, | |
| 56 | }, | |
| 57 | "output": { | |
0296375fstainless-app[bot]9 months ago | 58 | "format": { |
| 59 | "rate": 24000, | |
| 60 | "type": "audio/pcm", | |
| 61 | }, | |
3d3d16abstainless-app[bot]9 months ago | 62 | "speed": 0.25, |
e507a4ebstainless-app[bot]2 months ago | 63 | "voice": "alloy", |
3d3d16abstainless-app[bot]9 months ago | 64 | }, |
| 65 | }, | |
| 66 | "include": ["item.input_audio_transcription.logprobs"], | |
| 67 | "instructions": "instructions", | |
e507a4ebstainless-app[bot]2 months ago | 68 | "max_output_tokens": "inf", |
| 69 | "model": "gpt-realtime", | |
3d3d16abstainless-app[bot]9 months ago | 70 | "output_modalities": ["text"], |
8fe0ab87stainless-app[bot]1 months ago | 71 | "parallel_tool_calls": True, |
3d3d16abstainless-app[bot]9 months ago | 72 | "prompt": { |
| 73 | "id": "id", | |
| 74 | "variables": {"foo": "string"}, | |
| 75 | "version": "version", | |
| 76 | }, | |
8fe0ab87stainless-app[bot]1 months ago | 77 | "reasoning": {"effort": "minimal"}, |
3d3d16abstainless-app[bot]9 months ago | 78 | "tool_choice": "none", |
| 79 | "tools": [ | |
| 80 | { | |
| 81 | "description": "description", | |
| 82 | "name": "name", | |
| 83 | "parameters": {}, | |
| 84 | "type": "function", | |
| 85 | } | |
| 86 | ], | |
| 87 | "tracing": "auto", | |
| 88 | "truncation": "auto", | |
| 89 | }, | |
| 90 | ) | |
| 91 | assert_matches_type(ClientSecretCreateResponse, client_secret, path=["response"]) | |
| 92 | | |
| 93 | @parametrize | |
| 94 | def test_raw_response_create(self, client: OpenAI) -> None: | |
| 95 | response = client.realtime.client_secrets.with_raw_response.create() | |
| 96 | | |
| 97 | assert response.is_closed is True | |
| 98 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 99 | client_secret = response.parse() | |
| 100 | assert_matches_type(ClientSecretCreateResponse, client_secret, path=["response"]) | |
| 101 | | |
| 102 | @parametrize | |
| 103 | def test_streaming_response_create(self, client: OpenAI) -> None: | |
| 104 | with client.realtime.client_secrets.with_streaming_response.create() as response: | |
| 105 | assert not response.is_closed | |
| 106 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 107 | | |
| 108 | client_secret = response.parse() | |
| 109 | assert_matches_type(ClientSecretCreateResponse, client_secret, path=["response"]) | |
| 110 | | |
| 111 | assert cast(Any, response.is_closed) is True | |
| 112 | | |
| 113 | | |
| 114 | class TestAsyncClientSecrets: | |
| 115 | parametrize = pytest.mark.parametrize( | |
| 116 | "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"] | |
| 117 | ) | |
| 118 | | |
| 119 | @parametrize | |
| 120 | async def test_method_create(self, async_client: AsyncOpenAI) -> None: | |
| 121 | client_secret = await async_client.realtime.client_secrets.create() | |
| 122 | assert_matches_type(ClientSecretCreateResponse, client_secret, path=["response"]) | |
| 123 | | |
| 124 | @parametrize | |
| 125 | async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None: | |
| 126 | client_secret = await async_client.realtime.client_secrets.create( | |
| 127 | expires_after={ | |
| 128 | "anchor": "created_at", | |
| 129 | "seconds": 10, | |
| 130 | }, | |
| 131 | session={ | |
| 132 | "type": "realtime", | |
| 133 | "audio": { | |
| 134 | "input": { | |
0296375fstainless-app[bot]9 months ago | 135 | "format": { |
| 136 | "rate": 24000, | |
| 137 | "type": "audio/pcm", | |
| 138 | }, | |
3d3d16abstainless-app[bot]9 months ago | 139 | "noise_reduction": {"type": "near_field"}, |
| 140 | "transcription": { | |
8fe0ab87stainless-app[bot]1 months ago | 141 | "delay": "minimal", |
3d3d16abstainless-app[bot]9 months ago | 142 | "language": "language", |
e507a4ebstainless-app[bot]2 months ago | 143 | "model": "whisper-1", |
3d3d16abstainless-app[bot]9 months ago | 144 | "prompt": "prompt", |
| 145 | }, | |
| 146 | "turn_detection": { | |
4756247cstainless-app[bot]9 months ago | 147 | "type": "server_vad", |
3d3d16abstainless-app[bot]9 months ago | 148 | "create_response": True, |
4756247cstainless-app[bot]9 months ago | 149 | "idle_timeout_ms": 5000, |
3d3d16abstainless-app[bot]9 months ago | 150 | "interrupt_response": True, |
| 151 | "prefix_padding_ms": 0, | |
| 152 | "silence_duration_ms": 0, | |
| 153 | "threshold": 0, | |
| 154 | }, | |
| 155 | }, | |
| 156 | "output": { | |
0296375fstainless-app[bot]9 months ago | 157 | "format": { |
| 158 | "rate": 24000, | |
| 159 | "type": "audio/pcm", | |
| 160 | }, | |
3d3d16abstainless-app[bot]9 months ago | 161 | "speed": 0.25, |
e507a4ebstainless-app[bot]2 months ago | 162 | "voice": "alloy", |
3d3d16abstainless-app[bot]9 months ago | 163 | }, |
| 164 | }, | |
| 165 | "include": ["item.input_audio_transcription.logprobs"], | |
| 166 | "instructions": "instructions", | |
e507a4ebstainless-app[bot]2 months ago | 167 | "max_output_tokens": "inf", |
| 168 | "model": "gpt-realtime", | |
3d3d16abstainless-app[bot]9 months ago | 169 | "output_modalities": ["text"], |
8fe0ab87stainless-app[bot]1 months ago | 170 | "parallel_tool_calls": True, |
3d3d16abstainless-app[bot]9 months ago | 171 | "prompt": { |
| 172 | "id": "id", | |
| 173 | "variables": {"foo": "string"}, | |
| 174 | "version": "version", | |
| 175 | }, | |
8fe0ab87stainless-app[bot]1 months ago | 176 | "reasoning": {"effort": "minimal"}, |
3d3d16abstainless-app[bot]9 months ago | 177 | "tool_choice": "none", |
| 178 | "tools": [ | |
| 179 | { | |
| 180 | "description": "description", | |
| 181 | "name": "name", | |
| 182 | "parameters": {}, | |
| 183 | "type": "function", | |
| 184 | } | |
| 185 | ], | |
| 186 | "tracing": "auto", | |
| 187 | "truncation": "auto", | |
| 188 | }, | |
| 189 | ) | |
| 190 | assert_matches_type(ClientSecretCreateResponse, client_secret, path=["response"]) | |
| 191 | | |
| 192 | @parametrize | |
| 193 | async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None: | |
| 194 | response = await async_client.realtime.client_secrets.with_raw_response.create() | |
| 195 | | |
| 196 | assert response.is_closed is True | |
| 197 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 198 | client_secret = response.parse() | |
| 199 | assert_matches_type(ClientSecretCreateResponse, client_secret, path=["response"]) | |
| 200 | | |
| 201 | @parametrize | |
| 202 | async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None: | |
| 203 | async with async_client.realtime.client_secrets.with_streaming_response.create() as response: | |
| 204 | assert not response.is_closed | |
| 205 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 206 | | |
| 207 | client_secret = await response.parse() | |
| 208 | assert_matches_type(ClientSecretCreateResponse, client_secret, path=["response"]) | |
| 209 | | |
| 210 | assert cast(Any, response.is_closed) is True |