openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.61.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/beta/test_threads.py

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