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