openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.17.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/beta/test_threads.py

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