openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.59.8

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/beta/realtime/test_sessions.py

146lines · 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 model="gpt-4o-realtime-preview",
24 )
25 assert_matches_type(SessionCreateResponse, session, path=["response"])
26
27 @parametrize
28 def test_method_create_with_all_params(self, client: OpenAI) -> None:
29 session = client.beta.realtime.sessions.create(
30 model="gpt-4o-realtime-preview",
31 input_audio_format="pcm16",
32 input_audio_transcription={"model": "model"},
33 instructions="instructions",
34 max_response_output_tokens=0,
35 modalities=["text"],
36 output_audio_format="pcm16",
37 temperature=0,
38 tool_choice="tool_choice",
39 tools=[
40 {
41 "description": "description",
42 "name": "name",
43 "parameters": {},
44 "type": "function",
45 }
46 ],
47 turn_detection={
48 "create_response": True,
49 "prefix_padding_ms": 0,
50 "silence_duration_ms": 0,
51 "threshold": 0,
52 "type": "type",
53 },
54 voice="alloy",
55 )
56 assert_matches_type(SessionCreateResponse, session, path=["response"])
57
58 @parametrize
59 def test_raw_response_create(self, client: OpenAI) -> None:
60 response = client.beta.realtime.sessions.with_raw_response.create(
61 model="gpt-4o-realtime-preview",
62 )
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(
72 model="gpt-4o-realtime-preview",
73 ) as response:
74 assert not response.is_closed
75 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
76
77 session = response.parse()
78 assert_matches_type(SessionCreateResponse, session, path=["response"])
79
80 assert cast(Any, response.is_closed) is True
81
82
83class TestAsyncSessions:
84 parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
85
86 @parametrize
87 async def test_method_create(self, async_client: AsyncOpenAI) -> None:
88 session = await async_client.beta.realtime.sessions.create(
89 model="gpt-4o-realtime-preview",
90 )
91 assert_matches_type(SessionCreateResponse, session, path=["response"])
92
93 @parametrize
94 async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
95 session = await async_client.beta.realtime.sessions.create(
96 model="gpt-4o-realtime-preview",
97 input_audio_format="pcm16",
98 input_audio_transcription={"model": "model"},
99 instructions="instructions",
100 max_response_output_tokens=0,
101 modalities=["text"],
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 model="gpt-4o-realtime-preview",
128 )
129
130 assert response.is_closed is True
131 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
132 session = response.parse()
133 assert_matches_type(SessionCreateResponse, session, path=["response"])
134
135 @parametrize
136 async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
137 async with async_client.beta.realtime.sessions.with_streaming_response.create(
138 model="gpt-4o-realtime-preview",
139 ) as response:
140 assert not response.is_closed
141 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
142
143 session = await response.parse()
144 assert_matches_type(SessionCreateResponse, session, path=["response"])
145
146 assert cast(Any, response.is_closed) is True
147