openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.14.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/beta/test_threads.py

642lines · modecode

1# File generated from our OpenAPI spec by Stainless.
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 import (
13 Thread,
14 ThreadDeleted,
15)
16from openai.types.beta.threads import Run
17
18base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
19
20
21class TestThreads:
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 thread = client.beta.threads.create()
27 assert_matches_type(Thread, thread, path=["response"])
28
29 @parametrize
30 def test_method_create_with_all_params(self, client: OpenAI) -> None:
31 thread = client.beta.threads.create(
32 messages=[
33 {
34 "role": "user",
35 "content": "x",
36 "file_ids": ["string"],
37 "metadata": {},
38 },
39 {
40 "role": "user",
41 "content": "x",
42 "file_ids": ["string"],
43 "metadata": {},
44 },
45 {
46 "role": "user",
47 "content": "x",
48 "file_ids": ["string"],
49 "metadata": {},
50 },
51 ],
52 metadata={},
53 )
54 assert_matches_type(Thread, thread, path=["response"])
55
56 @parametrize
57 def test_raw_response_create(self, client: OpenAI) -> None:
58 response = client.beta.threads.with_raw_response.create()
59
60 assert response.is_closed is True
61 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
62 thread = response.parse()
63 assert_matches_type(Thread, thread, path=["response"])
64
65 @parametrize
66 def test_streaming_response_create(self, client: OpenAI) -> None:
67 with client.beta.threads.with_streaming_response.create() as response:
68 assert not response.is_closed
69 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
70
71 thread = response.parse()
72 assert_matches_type(Thread, thread, path=["response"])
73
74 assert cast(Any, response.is_closed) is True
75
76 @parametrize
77 def test_method_retrieve(self, client: OpenAI) -> None:
78 thread = client.beta.threads.retrieve(
79 "string",
80 )
81 assert_matches_type(Thread, thread, path=["response"])
82
83 @parametrize
84 def test_raw_response_retrieve(self, client: OpenAI) -> None:
85 response = client.beta.threads.with_raw_response.retrieve(
86 "string",
87 )
88
89 assert response.is_closed is True
90 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
91 thread = response.parse()
92 assert_matches_type(Thread, thread, path=["response"])
93
94 @parametrize
95 def test_streaming_response_retrieve(self, client: OpenAI) -> None:
96 with client.beta.threads.with_streaming_response.retrieve(
97 "string",
98 ) as response:
99 assert not response.is_closed
100 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
101
102 thread = response.parse()
103 assert_matches_type(Thread, thread, path=["response"])
104
105 assert cast(Any, response.is_closed) is True
106
107 @parametrize
108 def test_path_params_retrieve(self, client: OpenAI) -> None:
109 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
110 client.beta.threads.with_raw_response.retrieve(
111 "",
112 )
113
114 @parametrize
115 def test_method_update(self, client: OpenAI) -> None:
116 thread = client.beta.threads.update(
117 "string",
118 )
119 assert_matches_type(Thread, thread, path=["response"])
120
121 @parametrize
122 def test_method_update_with_all_params(self, client: OpenAI) -> None:
123 thread = client.beta.threads.update(
124 "string",
125 metadata={},
126 )
127 assert_matches_type(Thread, thread, path=["response"])
128
129 @parametrize
130 def test_raw_response_update(self, client: OpenAI) -> None:
131 response = client.beta.threads.with_raw_response.update(
132 "string",
133 )
134
135 assert response.is_closed is True
136 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
137 thread = response.parse()
138 assert_matches_type(Thread, thread, path=["response"])
139
140 @parametrize
141 def test_streaming_response_update(self, client: OpenAI) -> None:
142 with client.beta.threads.with_streaming_response.update(
143 "string",
144 ) as response:
145 assert not response.is_closed
146 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
147
148 thread = response.parse()
149 assert_matches_type(Thread, thread, path=["response"])
150
151 assert cast(Any, response.is_closed) is True
152
153 @parametrize
154 def test_path_params_update(self, client: OpenAI) -> None:
155 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
156 client.beta.threads.with_raw_response.update(
157 "",
158 )
159
160 @parametrize
161 def test_method_delete(self, client: OpenAI) -> None:
162 thread = client.beta.threads.delete(
163 "string",
164 )
165 assert_matches_type(ThreadDeleted, thread, path=["response"])
166
167 @parametrize
168 def test_raw_response_delete(self, client: OpenAI) -> None:
169 response = client.beta.threads.with_raw_response.delete(
170 "string",
171 )
172
173 assert response.is_closed is True
174 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
175 thread = response.parse()
176 assert_matches_type(ThreadDeleted, thread, path=["response"])
177
178 @parametrize
179 def test_streaming_response_delete(self, client: OpenAI) -> None:
180 with client.beta.threads.with_streaming_response.delete(
181 "string",
182 ) as response:
183 assert not response.is_closed
184 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
185
186 thread = response.parse()
187 assert_matches_type(ThreadDeleted, thread, path=["response"])
188
189 assert cast(Any, response.is_closed) is True
190
191 @parametrize
192 def test_path_params_delete(self, client: OpenAI) -> None:
193 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
194 client.beta.threads.with_raw_response.delete(
195 "",
196 )
197
198 @parametrize
199 def test_method_create_and_run_overload_1(self, client: OpenAI) -> None:
200 thread = client.beta.threads.create_and_run(
201 assistant_id="string",
202 )
203 assert_matches_type(Run, thread, path=["response"])
204
205 @parametrize
206 def test_method_create_and_run_with_all_params_overload_1(self, client: OpenAI) -> None:
207 thread = client.beta.threads.create_and_run(
208 assistant_id="string",
209 instructions="string",
210 metadata={},
211 model="string",
212 stream=False,
213 thread={
214 "messages": [
215 {
216 "role": "user",
217 "content": "x",
218 "file_ids": ["string"],
219 "metadata": {},
220 },
221 {
222 "role": "user",
223 "content": "x",
224 "file_ids": ["string"],
225 "metadata": {},
226 },
227 {
228 "role": "user",
229 "content": "x",
230 "file_ids": ["string"],
231 "metadata": {},
232 },
233 ],
234 "metadata": {},
235 },
236 tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}],
237 )
238 assert_matches_type(Run, thread, path=["response"])
239
240 @parametrize
241 def test_raw_response_create_and_run_overload_1(self, client: OpenAI) -> None:
242 response = client.beta.threads.with_raw_response.create_and_run(
243 assistant_id="string",
244 )
245
246 assert response.is_closed is True
247 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
248 thread = response.parse()
249 assert_matches_type(Run, thread, path=["response"])
250
251 @parametrize
252 def test_streaming_response_create_and_run_overload_1(self, client: OpenAI) -> None:
253 with client.beta.threads.with_streaming_response.create_and_run(
254 assistant_id="string",
255 ) as response:
256 assert not response.is_closed
257 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
258
259 thread = response.parse()
260 assert_matches_type(Run, thread, path=["response"])
261
262 assert cast(Any, response.is_closed) is True
263
264 @parametrize
265 def test_method_create_and_run_overload_2(self, client: OpenAI) -> None:
266 thread_stream = client.beta.threads.create_and_run(
267 assistant_id="string",
268 stream=True,
269 )
270 thread_stream.response.close()
271
272 @parametrize
273 def test_method_create_and_run_with_all_params_overload_2(self, client: OpenAI) -> None:
274 thread_stream = client.beta.threads.create_and_run(
275 assistant_id="string",
276 stream=True,
277 instructions="string",
278 metadata={},
279 model="string",
280 thread={
281 "messages": [
282 {
283 "role": "user",
284 "content": "x",
285 "file_ids": ["string"],
286 "metadata": {},
287 },
288 {
289 "role": "user",
290 "content": "x",
291 "file_ids": ["string"],
292 "metadata": {},
293 },
294 {
295 "role": "user",
296 "content": "x",
297 "file_ids": ["string"],
298 "metadata": {},
299 },
300 ],
301 "metadata": {},
302 },
303 tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}],
304 )
305 thread_stream.response.close()
306
307 @parametrize
308 def test_raw_response_create_and_run_overload_2(self, client: OpenAI) -> None:
309 response = client.beta.threads.with_raw_response.create_and_run(
310 assistant_id="string",
311 stream=True,
312 )
313
314 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
315 stream = response.parse()
316 stream.close()
317
318 @parametrize
319 def test_streaming_response_create_and_run_overload_2(self, client: OpenAI) -> None:
320 with client.beta.threads.with_streaming_response.create_and_run(
321 assistant_id="string",
322 stream=True,
323 ) as response:
324 assert not response.is_closed
325 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
326
327 stream = response.parse()
328 stream.close()
329
330 assert cast(Any, response.is_closed) is True
331
332
333class TestAsyncThreads:
334 parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
335
336 @parametrize
337 async def test_method_create(self, async_client: AsyncOpenAI) -> None:
338 thread = await async_client.beta.threads.create()
339 assert_matches_type(Thread, thread, path=["response"])
340
341 @parametrize
342 async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
343 thread = await async_client.beta.threads.create(
344 messages=[
345 {
346 "role": "user",
347 "content": "x",
348 "file_ids": ["string"],
349 "metadata": {},
350 },
351 {
352 "role": "user",
353 "content": "x",
354 "file_ids": ["string"],
355 "metadata": {},
356 },
357 {
358 "role": "user",
359 "content": "x",
360 "file_ids": ["string"],
361 "metadata": {},
362 },
363 ],
364 metadata={},
365 )
366 assert_matches_type(Thread, thread, path=["response"])
367
368 @parametrize
369 async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
370 response = await async_client.beta.threads.with_raw_response.create()
371
372 assert response.is_closed is True
373 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
374 thread = response.parse()
375 assert_matches_type(Thread, thread, path=["response"])
376
377 @parametrize
378 async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
379 async with async_client.beta.threads.with_streaming_response.create() as response:
380 assert not response.is_closed
381 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
382
383 thread = await response.parse()
384 assert_matches_type(Thread, thread, path=["response"])
385
386 assert cast(Any, response.is_closed) is True
387
388 @parametrize
389 async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None:
390 thread = await async_client.beta.threads.retrieve(
391 "string",
392 )
393 assert_matches_type(Thread, thread, path=["response"])
394
395 @parametrize
396 async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None:
397 response = await async_client.beta.threads.with_raw_response.retrieve(
398 "string",
399 )
400
401 assert response.is_closed is True
402 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
403 thread = response.parse()
404 assert_matches_type(Thread, thread, path=["response"])
405
406 @parametrize
407 async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None:
408 async with async_client.beta.threads.with_streaming_response.retrieve(
409 "string",
410 ) as response:
411 assert not response.is_closed
412 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
413
414 thread = await response.parse()
415 assert_matches_type(Thread, thread, path=["response"])
416
417 assert cast(Any, response.is_closed) is True
418
419 @parametrize
420 async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
421 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
422 await async_client.beta.threads.with_raw_response.retrieve(
423 "",
424 )
425
426 @parametrize
427 async def test_method_update(self, async_client: AsyncOpenAI) -> None:
428 thread = await async_client.beta.threads.update(
429 "string",
430 )
431 assert_matches_type(Thread, thread, path=["response"])
432
433 @parametrize
434 async def test_method_update_with_all_params(self, async_client: AsyncOpenAI) -> None:
435 thread = await async_client.beta.threads.update(
436 "string",
437 metadata={},
438 )
439 assert_matches_type(Thread, thread, path=["response"])
440
441 @parametrize
442 async def test_raw_response_update(self, async_client: AsyncOpenAI) -> None:
443 response = await async_client.beta.threads.with_raw_response.update(
444 "string",
445 )
446
447 assert response.is_closed is True
448 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
449 thread = response.parse()
450 assert_matches_type(Thread, thread, path=["response"])
451
452 @parametrize
453 async def test_streaming_response_update(self, async_client: AsyncOpenAI) -> None:
454 async with async_client.beta.threads.with_streaming_response.update(
455 "string",
456 ) as response:
457 assert not response.is_closed
458 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
459
460 thread = await response.parse()
461 assert_matches_type(Thread, thread, path=["response"])
462
463 assert cast(Any, response.is_closed) is True
464
465 @parametrize
466 async def test_path_params_update(self, async_client: AsyncOpenAI) -> None:
467 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
468 await async_client.beta.threads.with_raw_response.update(
469 "",
470 )
471
472 @parametrize
473 async def test_method_delete(self, async_client: AsyncOpenAI) -> None:
474 thread = await async_client.beta.threads.delete(
475 "string",
476 )
477 assert_matches_type(ThreadDeleted, thread, path=["response"])
478
479 @parametrize
480 async def test_raw_response_delete(self, async_client: AsyncOpenAI) -> None:
481 response = await async_client.beta.threads.with_raw_response.delete(
482 "string",
483 )
484
485 assert response.is_closed is True
486 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
487 thread = response.parse()
488 assert_matches_type(ThreadDeleted, thread, path=["response"])
489
490 @parametrize
491 async def test_streaming_response_delete(self, async_client: AsyncOpenAI) -> None:
492 async with async_client.beta.threads.with_streaming_response.delete(
493 "string",
494 ) as response:
495 assert not response.is_closed
496 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
497
498 thread = await response.parse()
499 assert_matches_type(ThreadDeleted, thread, path=["response"])
500
501 assert cast(Any, response.is_closed) is True
502
503 @parametrize
504 async def test_path_params_delete(self, async_client: AsyncOpenAI) -> None:
505 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
506 await async_client.beta.threads.with_raw_response.delete(
507 "",
508 )
509
510 @parametrize
511 async def test_method_create_and_run_overload_1(self, async_client: AsyncOpenAI) -> None:
512 thread = await async_client.beta.threads.create_and_run(
513 assistant_id="string",
514 )
515 assert_matches_type(Run, thread, path=["response"])
516
517 @parametrize
518 async def test_method_create_and_run_with_all_params_overload_1(self, async_client: AsyncOpenAI) -> None:
519 thread = await async_client.beta.threads.create_and_run(
520 assistant_id="string",
521 instructions="string",
522 metadata={},
523 model="string",
524 stream=False,
525 thread={
526 "messages": [
527 {
528 "role": "user",
529 "content": "x",
530 "file_ids": ["string"],
531 "metadata": {},
532 },
533 {
534 "role": "user",
535 "content": "x",
536 "file_ids": ["string"],
537 "metadata": {},
538 },
539 {
540 "role": "user",
541 "content": "x",
542 "file_ids": ["string"],
543 "metadata": {},
544 },
545 ],
546 "metadata": {},
547 },
548 tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}],
549 )
550 assert_matches_type(Run, thread, path=["response"])
551
552 @parametrize
553 async def test_raw_response_create_and_run_overload_1(self, async_client: AsyncOpenAI) -> None:
554 response = await async_client.beta.threads.with_raw_response.create_and_run(
555 assistant_id="string",
556 )
557
558 assert response.is_closed is True
559 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
560 thread = response.parse()
561 assert_matches_type(Run, thread, path=["response"])
562
563 @parametrize
564 async def test_streaming_response_create_and_run_overload_1(self, async_client: AsyncOpenAI) -> None:
565 async with async_client.beta.threads.with_streaming_response.create_and_run(
566 assistant_id="string",
567 ) as response:
568 assert not response.is_closed
569 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
570
571 thread = await response.parse()
572 assert_matches_type(Run, thread, path=["response"])
573
574 assert cast(Any, response.is_closed) is True
575
576 @parametrize
577 async def test_method_create_and_run_overload_2(self, async_client: AsyncOpenAI) -> None:
578 thread_stream = await async_client.beta.threads.create_and_run(
579 assistant_id="string",
580 stream=True,
581 )
582 await thread_stream.response.aclose()
583
584 @parametrize
585 async def test_method_create_and_run_with_all_params_overload_2(self, async_client: AsyncOpenAI) -> None:
586 thread_stream = await async_client.beta.threads.create_and_run(
587 assistant_id="string",
588 stream=True,
589 instructions="string",
590 metadata={},
591 model="string",
592 thread={
593 "messages": [
594 {
595 "role": "user",
596 "content": "x",
597 "file_ids": ["string"],
598 "metadata": {},
599 },
600 {
601 "role": "user",
602 "content": "x",
603 "file_ids": ["string"],
604 "metadata": {},
605 },
606 {
607 "role": "user",
608 "content": "x",
609 "file_ids": ["string"],
610 "metadata": {},
611 },
612 ],
613 "metadata": {},
614 },
615 tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}],
616 )
617 await thread_stream.response.aclose()
618
619 @parametrize
620 async def test_raw_response_create_and_run_overload_2(self, async_client: AsyncOpenAI) -> None:
621 response = await async_client.beta.threads.with_raw_response.create_and_run(
622 assistant_id="string",
623 stream=True,
624 )
625
626 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
627 stream = response.parse()
628 await stream.close()
629
630 @parametrize
631 async def test_streaming_response_create_and_run_overload_2(self, async_client: AsyncOpenAI) -> None:
632 async with async_client.beta.threads.with_streaming_response.create_and_run(
633 assistant_id="string",
634 stream=True,
635 ) as response:
636 assert not response.is_closed
637 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
638
639 stream = await response.parse()
640 await stream.close()
641
642 assert cast(Any, response.is_closed) is True
643