openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
db069cdacaf7ff8d7c1308db9f2465d38252ea1e

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/beta/test_threads.py

678lines · 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 "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 max_completion_tokens=256,
211 max_prompt_tokens=256,
212 metadata={},
213 model="gpt-4-turbo",
214 response_format="none",
215 stream=False,
216 temperature=1,
217 thread={
218 "messages": [
219 {
220 "role": "user",
221 "content": "x",
222 "file_ids": ["string"],
223 "metadata": {},
224 },
225 {
226 "role": "user",
227 "content": "x",
228 "file_ids": ["string"],
229 "metadata": {},
230 },
231 {
232 "role": "user",
233 "content": "x",
234 "file_ids": ["string"],
235 "metadata": {},
236 },
237 ],
238 "metadata": {},
239 },
240 tool_choice="none",
241 tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}],
242 truncation_strategy={
243 "type": "auto",
244 "last_messages": 1,
245 },
246 )
247 assert_matches_type(Run, thread, path=["response"])
248
249 @parametrize
250 def test_raw_response_create_and_run_overload_1(self, client: OpenAI) -> None:
251 response = client.beta.threads.with_raw_response.create_and_run(
252 assistant_id="string",
253 )
254
255 assert response.is_closed is True
256 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
257 thread = response.parse()
258 assert_matches_type(Run, thread, path=["response"])
259
260 @parametrize
261 def test_streaming_response_create_and_run_overload_1(self, client: OpenAI) -> None:
262 with client.beta.threads.with_streaming_response.create_and_run(
263 assistant_id="string",
264 ) as response:
265 assert not response.is_closed
266 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
267
268 thread = response.parse()
269 assert_matches_type(Run, thread, path=["response"])
270
271 assert cast(Any, response.is_closed) is True
272
273 @parametrize
274 def test_method_create_and_run_overload_2(self, client: OpenAI) -> None:
275 thread_stream = client.beta.threads.create_and_run(
276 assistant_id="string",
277 stream=True,
278 )
279 thread_stream.response.close()
280
281 @parametrize
282 def test_method_create_and_run_with_all_params_overload_2(self, client: OpenAI) -> None:
283 thread_stream = client.beta.threads.create_and_run(
284 assistant_id="string",
285 stream=True,
286 instructions="string",
287 max_completion_tokens=256,
288 max_prompt_tokens=256,
289 metadata={},
290 model="gpt-4-turbo",
291 response_format="none",
292 temperature=1,
293 thread={
294 "messages": [
295 {
296 "role": "user",
297 "content": "x",
298 "file_ids": ["string"],
299 "metadata": {},
300 },
301 {
302 "role": "user",
303 "content": "x",
304 "file_ids": ["string"],
305 "metadata": {},
306 },
307 {
308 "role": "user",
309 "content": "x",
310 "file_ids": ["string"],
311 "metadata": {},
312 },
313 ],
314 "metadata": {},
315 },
316 tool_choice="none",
317 tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}],
318 truncation_strategy={
319 "type": "auto",
320 "last_messages": 1,
321 },
322 )
323 thread_stream.response.close()
324
325 @parametrize
326 def test_raw_response_create_and_run_overload_2(self, client: OpenAI) -> None:
327 response = client.beta.threads.with_raw_response.create_and_run(
328 assistant_id="string",
329 stream=True,
330 )
331
332 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
333 stream = response.parse()
334 stream.close()
335
336 @parametrize
337 def test_streaming_response_create_and_run_overload_2(self, client: OpenAI) -> None:
338 with client.beta.threads.with_streaming_response.create_and_run(
339 assistant_id="string",
340 stream=True,
341 ) as response:
342 assert not response.is_closed
343 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
344
345 stream = response.parse()
346 stream.close()
347
348 assert cast(Any, response.is_closed) is True
349
350
351class TestAsyncThreads:
352 parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
353
354 @parametrize
355 async def test_method_create(self, async_client: AsyncOpenAI) -> None:
356 thread = await async_client.beta.threads.create()
357 assert_matches_type(Thread, thread, path=["response"])
358
359 @parametrize
360 async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
361 thread = await async_client.beta.threads.create(
362 messages=[
363 {
364 "role": "user",
365 "content": "x",
366 "file_ids": ["string"],
367 "metadata": {},
368 },
369 {
370 "role": "user",
371 "content": "x",
372 "file_ids": ["string"],
373 "metadata": {},
374 },
375 {
376 "role": "user",
377 "content": "x",
378 "file_ids": ["string"],
379 "metadata": {},
380 },
381 ],
382 metadata={},
383 )
384 assert_matches_type(Thread, thread, path=["response"])
385
386 @parametrize
387 async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
388 response = await async_client.beta.threads.with_raw_response.create()
389
390 assert response.is_closed is True
391 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
392 thread = response.parse()
393 assert_matches_type(Thread, thread, path=["response"])
394
395 @parametrize
396 async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
397 async with async_client.beta.threads.with_streaming_response.create() as response:
398 assert not response.is_closed
399 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
400
401 thread = await response.parse()
402 assert_matches_type(Thread, thread, path=["response"])
403
404 assert cast(Any, response.is_closed) is True
405
406 @parametrize
407 async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None:
408 thread = await async_client.beta.threads.retrieve(
409 "string",
410 )
411 assert_matches_type(Thread, thread, path=["response"])
412
413 @parametrize
414 async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None:
415 response = await async_client.beta.threads.with_raw_response.retrieve(
416 "string",
417 )
418
419 assert response.is_closed is True
420 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
421 thread = response.parse()
422 assert_matches_type(Thread, thread, path=["response"])
423
424 @parametrize
425 async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None:
426 async with async_client.beta.threads.with_streaming_response.retrieve(
427 "string",
428 ) as response:
429 assert not response.is_closed
430 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
431
432 thread = await response.parse()
433 assert_matches_type(Thread, thread, path=["response"])
434
435 assert cast(Any, response.is_closed) is True
436
437 @parametrize
438 async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
439 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
440 await async_client.beta.threads.with_raw_response.retrieve(
441 "",
442 )
443
444 @parametrize
445 async def test_method_update(self, async_client: AsyncOpenAI) -> None:
446 thread = await async_client.beta.threads.update(
447 "string",
448 )
449 assert_matches_type(Thread, thread, path=["response"])
450
451 @parametrize
452 async def test_method_update_with_all_params(self, async_client: AsyncOpenAI) -> None:
453 thread = await async_client.beta.threads.update(
454 "string",
455 metadata={},
456 )
457 assert_matches_type(Thread, thread, path=["response"])
458
459 @parametrize
460 async def test_raw_response_update(self, async_client: AsyncOpenAI) -> None:
461 response = await async_client.beta.threads.with_raw_response.update(
462 "string",
463 )
464
465 assert response.is_closed is True
466 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
467 thread = response.parse()
468 assert_matches_type(Thread, thread, path=["response"])
469
470 @parametrize
471 async def test_streaming_response_update(self, async_client: AsyncOpenAI) -> None:
472 async with async_client.beta.threads.with_streaming_response.update(
473 "string",
474 ) as response:
475 assert not response.is_closed
476 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
477
478 thread = await response.parse()
479 assert_matches_type(Thread, thread, path=["response"])
480
481 assert cast(Any, response.is_closed) is True
482
483 @parametrize
484 async def test_path_params_update(self, async_client: AsyncOpenAI) -> None:
485 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
486 await async_client.beta.threads.with_raw_response.update(
487 "",
488 )
489
490 @parametrize
491 async def test_method_delete(self, async_client: AsyncOpenAI) -> None:
492 thread = await async_client.beta.threads.delete(
493 "string",
494 )
495 assert_matches_type(ThreadDeleted, thread, path=["response"])
496
497 @parametrize
498 async def test_raw_response_delete(self, async_client: AsyncOpenAI) -> None:
499 response = await async_client.beta.threads.with_raw_response.delete(
500 "string",
501 )
502
503 assert response.is_closed is True
504 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
505 thread = response.parse()
506 assert_matches_type(ThreadDeleted, thread, path=["response"])
507
508 @parametrize
509 async def test_streaming_response_delete(self, async_client: AsyncOpenAI) -> None:
510 async with async_client.beta.threads.with_streaming_response.delete(
511 "string",
512 ) as response:
513 assert not response.is_closed
514 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
515
516 thread = await response.parse()
517 assert_matches_type(ThreadDeleted, thread, path=["response"])
518
519 assert cast(Any, response.is_closed) is True
520
521 @parametrize
522 async def test_path_params_delete(self, async_client: AsyncOpenAI) -> None:
523 with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
524 await async_client.beta.threads.with_raw_response.delete(
525 "",
526 )
527
528 @parametrize
529 async def test_method_create_and_run_overload_1(self, async_client: AsyncOpenAI) -> None:
530 thread = await async_client.beta.threads.create_and_run(
531 assistant_id="string",
532 )
533 assert_matches_type(Run, thread, path=["response"])
534
535 @parametrize
536 async def test_method_create_and_run_with_all_params_overload_1(self, async_client: AsyncOpenAI) -> None:
537 thread = await async_client.beta.threads.create_and_run(
538 assistant_id="string",
539 instructions="string",
540 max_completion_tokens=256,
541 max_prompt_tokens=256,
542 metadata={},
543 model="gpt-4-turbo",
544 response_format="none",
545 stream=False,
546 temperature=1,
547 thread={
548 "messages": [
549 {
550 "role": "user",
551 "content": "x",
552 "file_ids": ["string"],
553 "metadata": {},
554 },
555 {
556 "role": "user",
557 "content": "x",
558 "file_ids": ["string"],
559 "metadata": {},
560 },
561 {
562 "role": "user",
563 "content": "x",
564 "file_ids": ["string"],
565 "metadata": {},
566 },
567 ],
568 "metadata": {},
569 },
570 tool_choice="none",
571 tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}],
572 truncation_strategy={
573 "type": "auto",
574 "last_messages": 1,
575 },
576 )
577 assert_matches_type(Run, thread, path=["response"])
578
579 @parametrize
580 async def test_raw_response_create_and_run_overload_1(self, async_client: AsyncOpenAI) -> None:
581 response = await async_client.beta.threads.with_raw_response.create_and_run(
582 assistant_id="string",
583 )
584
585 assert response.is_closed is True
586 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
587 thread = response.parse()
588 assert_matches_type(Run, thread, path=["response"])
589
590 @parametrize
591 async def test_streaming_response_create_and_run_overload_1(self, async_client: AsyncOpenAI) -> None:
592 async with async_client.beta.threads.with_streaming_response.create_and_run(
593 assistant_id="string",
594 ) as response:
595 assert not response.is_closed
596 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
597
598 thread = await response.parse()
599 assert_matches_type(Run, thread, path=["response"])
600
601 assert cast(Any, response.is_closed) is True
602
603 @parametrize
604 async def test_method_create_and_run_overload_2(self, async_client: AsyncOpenAI) -> None:
605 thread_stream = await async_client.beta.threads.create_and_run(
606 assistant_id="string",
607 stream=True,
608 )
609 await thread_stream.response.aclose()
610
611 @parametrize
612 async def test_method_create_and_run_with_all_params_overload_2(self, async_client: AsyncOpenAI) -> None:
613 thread_stream = await async_client.beta.threads.create_and_run(
614 assistant_id="string",
615 stream=True,
616 instructions="string",
617 max_completion_tokens=256,
618 max_prompt_tokens=256,
619 metadata={},
620 model="gpt-4-turbo",
621 response_format="none",
622 temperature=1,
623 thread={
624 "messages": [
625 {
626 "role": "user",
627 "content": "x",
628 "file_ids": ["string"],
629 "metadata": {},
630 },
631 {
632 "role": "user",
633 "content": "x",
634 "file_ids": ["string"],
635 "metadata": {},
636 },
637 {
638 "role": "user",
639 "content": "x",
640 "file_ids": ["string"],
641 "metadata": {},
642 },
643 ],
644 "metadata": {},
645 },
646 tool_choice="none",
647 tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}],
648 truncation_strategy={
649 "type": "auto",
650 "last_messages": 1,
651 },
652 )
653 await thread_stream.response.aclose()
654
655 @parametrize
656 async def test_raw_response_create_and_run_overload_2(self, async_client: AsyncOpenAI) -> None:
657 response = await async_client.beta.threads.with_raw_response.create_and_run(
658 assistant_id="string",
659 stream=True,
660 )
661
662 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
663 stream = response.parse()
664 await stream.close()
665
666 @parametrize
667 async def test_streaming_response_create_and_run_overload_2(self, async_client: AsyncOpenAI) -> None:
668 async with async_client.beta.threads.with_streaming_response.create_and_run(
669 assistant_id="string",
670 stream=True,
671 ) as response:
672 assert not response.is_closed
673 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
674
675 stream = await response.parse()
676 await stream.close()
677
678 assert cast(Any, response.is_closed) is True
679