openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
dfdcf571ced31f7859cd1871be39e2fb3af6bafa

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/beta/test_assistants.py

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