openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.86.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/beta/threads/test_messages.py

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