openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.100.2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/api_resources/beta/realtime/test_sessions.py

166lines · modecode

1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
3from __future__ import annotations
4
5import os
6from typing import Any, cast
7
8import pytest
9
10from openai import OpenAI, AsyncOpenAI
11from tests.utils import assert_matches_type
12from openai.types.beta.realtime import SessionCreateResponse
13
14base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
15
16
17class 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 client_secret={
29 "expires_after": {
30 "anchor": "created_at",
31 "seconds": 0,
32 }
33 },
34 input_audio_format="pcm16",
35 input_audio_noise_reduction={"type": "near_field"},
36 input_audio_transcription={
37 "language": "language",
38 "model": "model",
39 "prompt": "prompt",
40 },
41 instructions="instructions",
42 max_response_output_tokens=0,
43 modalities=["text"],
44 model="gpt-4o-realtime-preview",
45 output_audio_format="pcm16",
46 speed=0.25,
47 temperature=0,
48 tool_choice="tool_choice",
49 tools=[
50 {
51 "description": "description",
52 "name": "name",
53 "parameters": {},
54 "type": "function",
55 }
56 ],
57 tracing="auto",
58 turn_detection={
59 "create_response": True,
60 "eagerness": "low",
61 "interrupt_response": True,
62 "prefix_padding_ms": 0,
63 "silence_duration_ms": 0,
64 "threshold": 0,
65 "type": "server_vad",
66 },
67 voice="ash",
68 )
69 assert_matches_type(SessionCreateResponse, session, path=["response"])
70
71 @parametrize
72 def test_raw_response_create(self, client: OpenAI) -> None:
73 response = client.beta.realtime.sessions.with_raw_response.create()
74
75 assert response.is_closed is True
76 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
77 session = response.parse()
78 assert_matches_type(SessionCreateResponse, session, path=["response"])
79
80 @parametrize
81 def test_streaming_response_create(self, client: OpenAI) -> None:
82 with client.beta.realtime.sessions.with_streaming_response.create() as response:
83 assert not response.is_closed
84 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
85
86 session = response.parse()
87 assert_matches_type(SessionCreateResponse, session, path=["response"])
88
89 assert cast(Any, response.is_closed) is True
90
91
92class TestAsyncSessions:
93 parametrize = pytest.mark.parametrize(
94 "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
95 )
96
97 @parametrize
98 async def test_method_create(self, async_client: AsyncOpenAI) -> None:
99 session = await async_client.beta.realtime.sessions.create()
100 assert_matches_type(SessionCreateResponse, session, path=["response"])
101
102 @parametrize
103 async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
104 session = await async_client.beta.realtime.sessions.create(
105 client_secret={
106 "expires_after": {
107 "anchor": "created_at",
108 "seconds": 0,
109 }
110 },
111 input_audio_format="pcm16",
112 input_audio_noise_reduction={"type": "near_field"},
113 input_audio_transcription={
114 "language": "language",
115 "model": "model",
116 "prompt": "prompt",
117 },
118 instructions="instructions",
119 max_response_output_tokens=0,
120 modalities=["text"],
121 model="gpt-4o-realtime-preview",
122 output_audio_format="pcm16",
123 speed=0.25,
124 temperature=0,
125 tool_choice="tool_choice",
126 tools=[
127 {
128 "description": "description",
129 "name": "name",
130 "parameters": {},
131 "type": "function",
132 }
133 ],
134 tracing="auto",
135 turn_detection={
136 "create_response": True,
137 "eagerness": "low",
138 "interrupt_response": True,
139 "prefix_padding_ms": 0,
140 "silence_duration_ms": 0,
141 "threshold": 0,
142 "type": "server_vad",
143 },
144 voice="ash",
145 )
146 assert_matches_type(SessionCreateResponse, session, path=["response"])
147
148 @parametrize
149 async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
150 response = await async_client.beta.realtime.sessions.with_raw_response.create()
151
152 assert response.is_closed is True
153 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
154 session = response.parse()
155 assert_matches_type(SessionCreateResponse, session, path=["response"])
156
157 @parametrize
158 async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
159 async with async_client.beta.realtime.sessions.with_streaming_response.create() as response:
160 assert not response.is_closed
161 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
162
163 session = await response.parse()
164 assert_matches_type(SessionCreateResponse, session, path=["response"])
165
166 assert cast(Any, response.is_closed) is True
167