openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
dfdcf571ced31f7859cd1871be39e2fb3af6bafa

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/beta/threads/test_messages.py

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