openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.63.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/beta/threads/test_messages.py

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