openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/api_resources/beta/realtime/test_sessions.py
142lines · 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.beta.realtime import SessionCreateResponse |
| 13 | |
| 14 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 15 | |
| 16 | |
| 17 | class TestSessions: |
| 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 | session = client.beta.realtime.sessions.create() |
| 23 | assert_matches_type(SessionCreateResponse, session, path=["response"]) |
| 24 | |
| 25 | @parametrize |
| 26 | def test_method_create_with_all_params(self, client: OpenAI) -> None: |
| 27 | session = client.beta.realtime.sessions.create( |
| 28 | input_audio_format="pcm16", |
| 29 | input_audio_transcription={ |
| 30 | "language": "language", |
| 31 | "model": "model", |
| 32 | "prompt": "prompt", |
| 33 | }, |
| 34 | instructions="instructions", |
| 35 | max_response_output_tokens=0, |
| 36 | modalities=["text"], |
| 37 | model="gpt-4o-realtime-preview", |
| 38 | output_audio_format="pcm16", |
| 39 | temperature=0, |
| 40 | tool_choice="tool_choice", |
| 41 | tools=[ |
| 42 | { |
| 43 | "description": "description", |
| 44 | "name": "name", |
| 45 | "parameters": {}, |
| 46 | "type": "function", |
| 47 | } |
| 48 | ], |
| 49 | turn_detection={ |
| 50 | "create_response": True, |
| 51 | "prefix_padding_ms": 0, |
| 52 | "silence_duration_ms": 0, |
| 53 | "threshold": 0, |
| 54 | "type": "type", |
| 55 | }, |
| 56 | voice="alloy", |
| 57 | ) |
| 58 | assert_matches_type(SessionCreateResponse, session, path=["response"]) |
| 59 | |
| 60 | @parametrize |
| 61 | def test_raw_response_create(self, client: OpenAI) -> None: |
| 62 | response = client.beta.realtime.sessions.with_raw_response.create() |
| 63 | |
| 64 | assert response.is_closed is True |
| 65 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 66 | session = response.parse() |
| 67 | assert_matches_type(SessionCreateResponse, session, path=["response"]) |
| 68 | |
| 69 | @parametrize |
| 70 | def test_streaming_response_create(self, client: OpenAI) -> None: |
| 71 | with client.beta.realtime.sessions.with_streaming_response.create() as response: |
| 72 | assert not response.is_closed |
| 73 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 74 | |
| 75 | session = response.parse() |
| 76 | assert_matches_type(SessionCreateResponse, session, path=["response"]) |
| 77 | |
| 78 | assert cast(Any, response.is_closed) is True |
| 79 | |
| 80 | |
| 81 | class TestAsyncSessions: |
| 82 | parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 83 | |
| 84 | @parametrize |
| 85 | async def test_method_create(self, async_client: AsyncOpenAI) -> None: |
| 86 | session = await async_client.beta.realtime.sessions.create() |
| 87 | assert_matches_type(SessionCreateResponse, session, path=["response"]) |
| 88 | |
| 89 | @parametrize |
| 90 | async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 91 | session = await async_client.beta.realtime.sessions.create( |
| 92 | input_audio_format="pcm16", |
| 93 | input_audio_transcription={ |
| 94 | "language": "language", |
| 95 | "model": "model", |
| 96 | "prompt": "prompt", |
| 97 | }, |
| 98 | instructions="instructions", |
| 99 | max_response_output_tokens=0, |
| 100 | modalities=["text"], |
| 101 | model="gpt-4o-realtime-preview", |
| 102 | output_audio_format="pcm16", |
| 103 | temperature=0, |
| 104 | tool_choice="tool_choice", |
| 105 | tools=[ |
| 106 | { |
| 107 | "description": "description", |
| 108 | "name": "name", |
| 109 | "parameters": {}, |
| 110 | "type": "function", |
| 111 | } |
| 112 | ], |
| 113 | turn_detection={ |
| 114 | "create_response": True, |
| 115 | "prefix_padding_ms": 0, |
| 116 | "silence_duration_ms": 0, |
| 117 | "threshold": 0, |
| 118 | "type": "type", |
| 119 | }, |
| 120 | voice="alloy", |
| 121 | ) |
| 122 | assert_matches_type(SessionCreateResponse, session, path=["response"]) |
| 123 | |
| 124 | @parametrize |
| 125 | async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None: |
| 126 | response = await async_client.beta.realtime.sessions.with_raw_response.create() |
| 127 | |
| 128 | assert response.is_closed is True |
| 129 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 130 | session = response.parse() |
| 131 | assert_matches_type(SessionCreateResponse, session, path=["response"]) |
| 132 | |
| 133 | @parametrize |
| 134 | async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None: |
| 135 | async with async_client.beta.realtime.sessions.with_streaming_response.create() as response: |
| 136 | assert not response.is_closed |
| 137 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 138 | |
| 139 | session = await response.parse() |
| 140 | assert_matches_type(SessionCreateResponse, session, path=["response"]) |
| 141 | |
| 142 | assert cast(Any, response.is_closed) is True |
| 143 | |