openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.86.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/beta/realtime/test_sessions.py

164lines · 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_at": {
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("async_client", [False, True], indirect=True, ids=["loose", "strict"])
94
95 @parametrize
96 async def test_method_create(self, async_client: AsyncOpenAI) -> None:
97 session = await async_client.beta.realtime.sessions.create()
98 assert_matches_type(SessionCreateResponse, session, path=["response"])
99
100 @parametrize
101 async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
102 session = await async_client.beta.realtime.sessions.create(
103 client_secret={
104 "expires_at": {
105 "anchor": "created_at",
106 "seconds": 0,
107 }
108 },
109 input_audio_format="pcm16",
110 input_audio_noise_reduction={"type": "near_field"},
111 input_audio_transcription={
112 "language": "language",
113 "model": "model",
114 "prompt": "prompt",
115 },
116 instructions="instructions",
117 max_response_output_tokens=0,
118 modalities=["text"],
119 model="gpt-4o-realtime-preview",
120 output_audio_format="pcm16",
121 speed=0.25,
122 temperature=0,
123 tool_choice="tool_choice",
124 tools=[
125 {
126 "description": "description",
127 "name": "name",
128 "parameters": {},
129 "type": "function",
130 }
131 ],
132 tracing="auto",
133 turn_detection={
134 "create_response": True,
135 "eagerness": "low",
136 "interrupt_response": True,
137 "prefix_padding_ms": 0,
138 "silence_duration_ms": 0,
139 "threshold": 0,
140 "type": "server_vad",
141 },
142 voice="ash",
143 )
144 assert_matches_type(SessionCreateResponse, session, path=["response"])
145
146 @parametrize
147 async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
148 response = await async_client.beta.realtime.sessions.with_raw_response.create()
149
150 assert response.is_closed is True
151 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
152 session = response.parse()
153 assert_matches_type(SessionCreateResponse, session, path=["response"])
154
155 @parametrize
156 async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
157 async with async_client.beta.realtime.sessions.with_streaming_response.create() as response:
158 assert not response.is_closed
159 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
160
161 session = await response.parse()
162 assert_matches_type(SessionCreateResponse, session, path=["response"])
163
164 assert cast(Any, response.is_closed) is True
165