openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
dev/dkundel/audio-helpers

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/beta/realtime/test_sessions.py

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