openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.78.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/beta/realtime/test_sessions.py

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