openai/openai-python

Public

mirrored fromhttps://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.63.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/beta/test_assistants.py

486lines · 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.pagination import SyncCursorPage, AsyncCursorPage
13from openai.types.beta import (
14 Assistant,
15 AssistantDeleted,
16)
17
18base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
19
20
21class TestAssistants:
22 parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
23
24 @parametrize
25 def test_method_create(self, client: OpenAI) -> None:
26 assistant = client.beta.assistants.create(
27 model="gpt-4o",
28 )
29 assert_matches_type(Assistant, assistant, path=["response"])
30
31 @parametrize
32 def test_method_create_with_all_params(self, client: OpenAI) -> None:
33 assistant = client.beta.assistants.create(
34 model="gpt-4o",
35 description="description",
36 instructions="instructions",
37 metadata={"foo": "string"},
38 name="name",
39 reasoning_effort="low",
40 response_format="auto",
41 temperature=1,
42 tool_resources={
43 "code_interpreter": {"file_ids": ["string"]},
44 "file_search": {
45 "vector_store_ids": ["string"],
46 "vector_stores": [
47 {
48 "chunking_strategy": {"type": "auto"},
49 "file_ids": ["string"],
50 "metadata": {"foo": "string"},
51 }
52 ],
53 },
54 },
55 tools=[{"type": "code_interpreter"}],
56 top_p=1,
57 )
58 assert_matches_type(Assistant, assistant, path=["response"])
59
60 @parametrize
61 def test_raw_response_create(self, client: OpenAI) -> None:
62 response = client.beta.assistants.with_raw_response.create(
63 model="gpt-4o",
64 )
65
66 assert response.is_closed is True
67 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
68 assistant = response.parse()
69 assert_matches_type(Assistant, assistant, path=["response"])
70
71 @parametrize
72 def test_streaming_response_create(self, client: OpenAI) -> None:
73 with client.beta.assistants.with_streaming_response.create(
74 model="gpt-4o",
75 ) as response:
76 assert not response.is_closed
77 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
78
79 assistant = response.parse()
80 assert_matches_type(Assistant, assistant, path=["response"])
81
82 assert cast(Any, response.is_closed) is True
83
84 @parametrize
85 def test_method_retrieve(self, client: OpenAI) -> None:
86 assistant = client.beta.assistants.retrieve(
87 "assistant_id",
88 )
89 assert_matches_type(Assistant, assistant, path=["response"])
90
91 @parametrize
92 def test_raw_response_retrieve(self, client: OpenAI) -> None:
93 response = client.beta.assistants.with_raw_response.retrieve(
94 "assistant_id",
95 )
96
97 assert response.is_closed is True
98 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
99 assistant = response.parse()
100 assert_matches_type(Assistant, assistant, path=["response"])
101
102 @parametrize
103 def test_streaming_response_retrieve(self, client: OpenAI) -> None:
104 with client.beta.assistants.with_streaming_response.retrieve(
105 "assistant_id",
106 ) as response:
107 assert not response.is_closed
108 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
109
110 assistant = response.parse()
111 assert_matches_type(Assistant, assistant, path=["response"])
112
113 assert cast(Any, response.is_closed) is True
114
115 @parametrize
116 def test_path_params_retrieve(self, client: OpenAI) -> None:
117 with pytest.raises(ValueError, match=r"Expected a non-empty value for `assistant_id` but received ''"):
118 client.beta.assistants.with_raw_response.retrieve(
119 "",
120 )
121
122 @parametrize
123 def test_method_update(self, client: OpenAI) -> None:
124 assistant = client.beta.assistants.update(
125 assistant_id="assistant_id",
126 )
127 assert_matches_type(Assistant, assistant, path=["response"])
128
129 @parametrize
130 def test_method_update_with_all_params(self, client: OpenAI) -> None:
131 assistant = client.beta.assistants.update(
132 assistant_id="assistant_id",
133 description="description",
134 instructions="instructions",
135 metadata={"foo": "string"},
136 model="string",
137 name="name",
138 reasoning_effort="low",
139 response_format="auto",
140 temperature=1,
141 tool_resources={
142 "code_interpreter": {"file_ids": ["string"]},
143 "file_search": {"vector_store_ids": ["string"]},
144 },
145 tools=[{"type": "code_interpreter"}],
146 top_p=1,
147 )
148 assert_matches_type(Assistant, assistant, path=["response"])
149
150 @parametrize
151 def test_raw_response_update(self, client: OpenAI) -> None:
152 response = client.beta.assistants.with_raw_response.update(
153 assistant_id="assistant_id",
154 )
155
156 assert response.is_closed is True
157 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
158 assistant = response.parse()
159 assert_matches_type(Assistant, assistant, path=["response"])
160
161 @parametrize
162 def test_streaming_response_update(self, client: OpenAI) -> None:
163 with client.beta.assistants.with_streaming_response.update(
164 assistant_id="assistant_id",
165 ) as response:
166 assert not response.is_closed
167 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
168
169 assistant = response.parse()
170 assert_matches_type(Assistant, assistant, path=["response"])
171
172 assert cast(Any, response.is_closed) is True
173
174 @parametrize
175 def test_path_params_update(self, client: OpenAI) -> None:
176 with pytest.raises(ValueError, match=r"Expected a non-empty value for `assistant_id` but received ''"):
177 client.beta.assistants.with_raw_response.update(
178 assistant_id="",
179 )
180
181 @parametrize
182 def test_method_list(self, client: OpenAI) -> None:
183 assistant = client.beta.assistants.list()
184 assert_matches_type(SyncCursorPage[Assistant], assistant, path=["response"])
185
186 @parametrize
187 def test_method_list_with_all_params(self, client: OpenAI) -> None:
188 assistant = client.beta.assistants.list(
189 after="after",
190 before="before",
191 limit=0,
192 order="asc",
193 )
194 assert_matches_type(SyncCursorPage[Assistant], assistant, path=["response"])
195
196 @parametrize
197 def test_raw_response_list(self, client: OpenAI) -> None:
198 response = client.beta.assistants.with_raw_response.list()
199
200 assert response.is_closed is True
201 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
202 assistant = response.parse()
203 assert_matches_type(SyncCursorPage[Assistant], assistant, path=["response"])
204
205 @parametrize
206 def test_streaming_response_list(self, client: OpenAI) -> None:
207 with client.beta.assistants.with_streaming_response.list() as response:
208 assert not response.is_closed
209 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
210
211 assistant = response.parse()
212 assert_matches_type(SyncCursorPage[Assistant], assistant, path=["response"])
213
214 assert cast(Any, response.is_closed) is True
215
216 @parametrize
217 def test_method_delete(self, client: OpenAI) -> None:
218 assistant = client.beta.assistants.delete(
219 "assistant_id",
220 )
221 assert_matches_type(AssistantDeleted, assistant, path=["response"])
222
223 @parametrize
224 def test_raw_response_delete(self, client: OpenAI) -> None:
225 response = client.beta.assistants.with_raw_response.delete(
226 "assistant_id",
227 )
228
229 assert response.is_closed is True
230 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
231 assistant = response.parse()
232 assert_matches_type(AssistantDeleted, assistant, path=["response"])
233
234 @parametrize
235 def test_streaming_response_delete(self, client: OpenAI) -> None:
236 with client.beta.assistants.with_streaming_response.delete(
237 "assistant_id",
238 ) as response:
239 assert not response.is_closed
240 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
241
242 assistant = response.parse()
243 assert_matches_type(AssistantDeleted, assistant, path=["response"])
244
245 assert cast(Any, response.is_closed) is True
246
247 @parametrize
248 def test_path_params_delete(self, client: OpenAI) -> None:
249 with pytest.raises(ValueError, match=r"Expected a non-empty value for `assistant_id` but received ''"):
250 client.beta.assistants.with_raw_response.delete(
251 "",
252 )
253
254
255class TestAsyncAssistants:
256 parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
257
258 @parametrize
259 async def test_method_create(self, async_client: AsyncOpenAI) -> None:
260 assistant = await async_client.beta.assistants.create(
261 model="gpt-4o",
262 )
263 assert_matches_type(Assistant, assistant, path=["response"])
264
265 @parametrize
266 async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
267 assistant = await async_client.beta.assistants.create(
268 model="gpt-4o",
269 description="description",
270 instructions="instructions",
271 metadata={"foo": "string"},
272 name="name",
273 reasoning_effort="low",
274 response_format="auto",
275 temperature=1,
276 tool_resources={
277 "code_interpreter": {"file_ids": ["string"]},
278 "file_search": {
279 "vector_store_ids": ["string"],
280 "vector_stores": [
281 {
282 "chunking_strategy": {"type": "auto"},
283 "file_ids": ["string"],
284 "metadata": {"foo": "string"},
285 }
286 ],
287 },
288 },
289 tools=[{"type": "code_interpreter"}],
290 top_p=1,
291 )
292 assert_matches_type(Assistant, assistant, path=["response"])
293
294 @parametrize
295 async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
296 response = await async_client.beta.assistants.with_raw_response.create(
297 model="gpt-4o",
298 )
299
300 assert response.is_closed is True
301 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
302 assistant = response.parse()
303 assert_matches_type(Assistant, assistant, path=["response"])
304
305 @parametrize
306 async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
307 async with async_client.beta.assistants.with_streaming_response.create(
308 model="gpt-4o",
309 ) as response:
310 assert not response.is_closed
311 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
312
313 assistant = await response.parse()
314 assert_matches_type(Assistant, assistant, path=["response"])
315
316 assert cast(Any, response.is_closed) is True
317
318 @parametrize
319 async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None:
320 assistant = await async_client.beta.assistants.retrieve(
321 "assistant_id",
322 )
323 assert_matches_type(Assistant, assistant, path=["response"])
324
325 @parametrize
326 async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None:
327 response = await async_client.beta.assistants.with_raw_response.retrieve(
328 "assistant_id",
329 )
330
331 assert response.is_closed is True
332 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
333 assistant = response.parse()
334 assert_matches_type(Assistant, assistant, path=["response"])
335
336 @parametrize
337 async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None:
338 async with async_client.beta.assistants.with_streaming_response.retrieve(
339 "assistant_id",
340 ) as response:
341 assert not response.is_closed
342 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
343
344 assistant = await response.parse()
345 assert_matches_type(Assistant, assistant, path=["response"])
346
347 assert cast(Any, response.is_closed) is True
348
349 @parametrize
350 async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
351 with pytest.raises(ValueError, match=r"Expected a non-empty value for `assistant_id` but received ''"):
352 await async_client.beta.assistants.with_raw_response.retrieve(
353 "",
354 )
355
356 @parametrize
357 async def test_method_update(self, async_client: AsyncOpenAI) -> None:
358 assistant = await async_client.beta.assistants.update(
359 assistant_id="assistant_id",
360 )
361 assert_matches_type(Assistant, assistant, path=["response"])
362
363 @parametrize
364 async def test_method_update_with_all_params(self, async_client: AsyncOpenAI) -> None:
365 assistant = await async_client.beta.assistants.update(
366 assistant_id="assistant_id",
367 description="description",
368 instructions="instructions",
369 metadata={"foo": "string"},
370 model="string",
371 name="name",
372 reasoning_effort="low",
373 response_format="auto",
374 temperature=1,
375 tool_resources={
376 "code_interpreter": {"file_ids": ["string"]},
377 "file_search": {"vector_store_ids": ["string"]},
378 },
379 tools=[{"type": "code_interpreter"}],
380 top_p=1,
381 )
382 assert_matches_type(Assistant, assistant, path=["response"])
383
384 @parametrize
385 async def test_raw_response_update(self, async_client: AsyncOpenAI) -> None:
386 response = await async_client.beta.assistants.with_raw_response.update(
387 assistant_id="assistant_id",
388 )
389
390 assert response.is_closed is True
391 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
392 assistant = response.parse()
393 assert_matches_type(Assistant, assistant, path=["response"])
394
395 @parametrize
396 async def test_streaming_response_update(self, async_client: AsyncOpenAI) -> None:
397 async with async_client.beta.assistants.with_streaming_response.update(
398 assistant_id="assistant_id",
399 ) as response:
400 assert not response.is_closed
401 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
402
403 assistant = await response.parse()
404 assert_matches_type(Assistant, assistant, path=["response"])
405
406 assert cast(Any, response.is_closed) is True
407
408 @parametrize
409 async def test_path_params_update(self, async_client: AsyncOpenAI) -> None:
410 with pytest.raises(ValueError, match=r"Expected a non-empty value for `assistant_id` but received ''"):
411 await async_client.beta.assistants.with_raw_response.update(
412 assistant_id="",
413 )
414
415 @parametrize
416 async def test_method_list(self, async_client: AsyncOpenAI) -> None:
417 assistant = await async_client.beta.assistants.list()
418 assert_matches_type(AsyncCursorPage[Assistant], assistant, path=["response"])
419
420 @parametrize
421 async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None:
422 assistant = await async_client.beta.assistants.list(
423 after="after",
424 before="before",
425 limit=0,
426 order="asc",
427 )
428 assert_matches_type(AsyncCursorPage[Assistant], assistant, path=["response"])
429
430 @parametrize
431 async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None:
432 response = await async_client.beta.assistants.with_raw_response.list()
433
434 assert response.is_closed is True
435 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
436 assistant = response.parse()
437 assert_matches_type(AsyncCursorPage[Assistant], assistant, path=["response"])
438
439 @parametrize
440 async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None:
441 async with async_client.beta.assistants.with_streaming_response.list() as response:
442 assert not response.is_closed
443 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
444
445 assistant = await response.parse()
446 assert_matches_type(AsyncCursorPage[Assistant], assistant, path=["response"])
447
448 assert cast(Any, response.is_closed) is True
449
450 @parametrize
451 async def test_method_delete(self, async_client: AsyncOpenAI) -> None:
452 assistant = await async_client.beta.assistants.delete(
453 "assistant_id",
454 )
455 assert_matches_type(AssistantDeleted, assistant, path=["response"])
456
457 @parametrize
458 async def test_raw_response_delete(self, async_client: AsyncOpenAI) -> None:
459 response = await async_client.beta.assistants.with_raw_response.delete(
460 "assistant_id",
461 )
462
463 assert response.is_closed is True
464 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
465 assistant = response.parse()
466 assert_matches_type(AssistantDeleted, assistant, path=["response"])
467
468 @parametrize
469 async def test_streaming_response_delete(self, async_client: AsyncOpenAI) -> None:
470 async with async_client.beta.assistants.with_streaming_response.delete(
471 "assistant_id",
472 ) as response:
473 assert not response.is_closed
474 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
475
476 assistant = await response.parse()
477 assert_matches_type(AssistantDeleted, assistant, path=["response"])
478
479 assert cast(Any, response.is_closed) is True
480
481 @parametrize
482 async def test_path_params_delete(self, async_client: AsyncOpenAI) -> None:
483 with pytest.raises(ValueError, match=r"Expected a non-empty value for `assistant_id` but received ''"):
484 await async_client.beta.assistants.with_raw_response.delete(
485 "",
486 )