main
  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(
325        "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
326    )
327
328    @parametrize
329    async def test_method_create(self, async_client: AsyncOpenAI) -> None:
330        with pytest.warns(DeprecationWarning):
331            message = await async_client.beta.threads.messages.create(
332                thread_id="thread_id",
333                content="string",
334                role="user",
335            )
336
337        assert_matches_type(Message, message, path=["response"])
338
339    @parametrize
340    async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
341        with pytest.warns(DeprecationWarning):
342            message = await async_client.beta.threads.messages.create(
343                thread_id="thread_id",
344                content="string",
345                role="user",
346                attachments=[
347                    {
348                        "file_id": "file_id",
349                        "tools": [{"type": "code_interpreter"}],
350                    }
351                ],
352                metadata={"foo": "string"},
353            )
354
355        assert_matches_type(Message, message, path=["response"])
356
357    @parametrize
358    async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
359        with pytest.warns(DeprecationWarning):
360            response = await async_client.beta.threads.messages.with_raw_response.create(
361                thread_id="thread_id",
362                content="string",
363                role="user",
364            )
365
366        assert response.is_closed is True
367        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
368        message = response.parse()
369        assert_matches_type(Message, message, path=["response"])
370
371    @parametrize
372    async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
373        with pytest.warns(DeprecationWarning):
374            async with async_client.beta.threads.messages.with_streaming_response.create(
375                thread_id="thread_id",
376                content="string",
377                role="user",
378            ) as response:
379                assert not response.is_closed
380                assert response.http_request.headers.get("X-Stainless-Lang") == "python"
381
382                message = await response.parse()
383                assert_matches_type(Message, message, path=["response"])
384
385        assert cast(Any, response.is_closed) is True
386
387    @parametrize
388    async def test_path_params_create(self, async_client: AsyncOpenAI) -> None:
389        with pytest.warns(DeprecationWarning):
390            with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
391                await async_client.beta.threads.messages.with_raw_response.create(
392                    thread_id="",
393                    content="string",
394                    role="user",
395                )
396
397    @parametrize
398    async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None:
399        with pytest.warns(DeprecationWarning):
400            message = await async_client.beta.threads.messages.retrieve(
401                message_id="message_id",
402                thread_id="thread_id",
403            )
404
405        assert_matches_type(Message, message, path=["response"])
406
407    @parametrize
408    async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None:
409        with pytest.warns(DeprecationWarning):
410            response = await async_client.beta.threads.messages.with_raw_response.retrieve(
411                message_id="message_id",
412                thread_id="thread_id",
413            )
414
415        assert response.is_closed is True
416        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
417        message = response.parse()
418        assert_matches_type(Message, message, path=["response"])
419
420    @parametrize
421    async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None:
422        with pytest.warns(DeprecationWarning):
423            async with async_client.beta.threads.messages.with_streaming_response.retrieve(
424                message_id="message_id",
425                thread_id="thread_id",
426            ) as response:
427                assert not response.is_closed
428                assert response.http_request.headers.get("X-Stainless-Lang") == "python"
429
430                message = await response.parse()
431                assert_matches_type(Message, message, path=["response"])
432
433        assert cast(Any, response.is_closed) is True
434
435    @parametrize
436    async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
437        with pytest.warns(DeprecationWarning):
438            with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
439                await async_client.beta.threads.messages.with_raw_response.retrieve(
440                    message_id="message_id",
441                    thread_id="",
442                )
443
444            with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"):
445                await async_client.beta.threads.messages.with_raw_response.retrieve(
446                    message_id="",
447                    thread_id="thread_id",
448                )
449
450    @parametrize
451    async def test_method_update(self, async_client: AsyncOpenAI) -> None:
452        with pytest.warns(DeprecationWarning):
453            message = await async_client.beta.threads.messages.update(
454                message_id="message_id",
455                thread_id="thread_id",
456            )
457
458        assert_matches_type(Message, message, path=["response"])
459
460    @parametrize
461    async def test_method_update_with_all_params(self, async_client: AsyncOpenAI) -> None:
462        with pytest.warns(DeprecationWarning):
463            message = await async_client.beta.threads.messages.update(
464                message_id="message_id",
465                thread_id="thread_id",
466                metadata={"foo": "string"},
467            )
468
469        assert_matches_type(Message, message, path=["response"])
470
471    @parametrize
472    async def test_raw_response_update(self, async_client: AsyncOpenAI) -> None:
473        with pytest.warns(DeprecationWarning):
474            response = await async_client.beta.threads.messages.with_raw_response.update(
475                message_id="message_id",
476                thread_id="thread_id",
477            )
478
479        assert response.is_closed is True
480        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
481        message = response.parse()
482        assert_matches_type(Message, message, path=["response"])
483
484    @parametrize
485    async def test_streaming_response_update(self, async_client: AsyncOpenAI) -> None:
486        with pytest.warns(DeprecationWarning):
487            async with async_client.beta.threads.messages.with_streaming_response.update(
488                message_id="message_id",
489                thread_id="thread_id",
490            ) as response:
491                assert not response.is_closed
492                assert response.http_request.headers.get("X-Stainless-Lang") == "python"
493
494                message = await response.parse()
495                assert_matches_type(Message, message, path=["response"])
496
497        assert cast(Any, response.is_closed) is True
498
499    @parametrize
500    async def test_path_params_update(self, async_client: AsyncOpenAI) -> None:
501        with pytest.warns(DeprecationWarning):
502            with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
503                await async_client.beta.threads.messages.with_raw_response.update(
504                    message_id="message_id",
505                    thread_id="",
506                )
507
508            with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"):
509                await async_client.beta.threads.messages.with_raw_response.update(
510                    message_id="",
511                    thread_id="thread_id",
512                )
513
514    @parametrize
515    async def test_method_list(self, async_client: AsyncOpenAI) -> None:
516        with pytest.warns(DeprecationWarning):
517            message = await async_client.beta.threads.messages.list(
518                thread_id="thread_id",
519            )
520
521        assert_matches_type(AsyncCursorPage[Message], message, path=["response"])
522
523    @parametrize
524    async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None:
525        with pytest.warns(DeprecationWarning):
526            message = await async_client.beta.threads.messages.list(
527                thread_id="thread_id",
528                after="after",
529                before="before",
530                limit=0,
531                order="asc",
532                run_id="run_id",
533            )
534
535        assert_matches_type(AsyncCursorPage[Message], message, path=["response"])
536
537    @parametrize
538    async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None:
539        with pytest.warns(DeprecationWarning):
540            response = await async_client.beta.threads.messages.with_raw_response.list(
541                thread_id="thread_id",
542            )
543
544        assert response.is_closed is True
545        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
546        message = response.parse()
547        assert_matches_type(AsyncCursorPage[Message], message, path=["response"])
548
549    @parametrize
550    async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None:
551        with pytest.warns(DeprecationWarning):
552            async with async_client.beta.threads.messages.with_streaming_response.list(
553                thread_id="thread_id",
554            ) as response:
555                assert not response.is_closed
556                assert response.http_request.headers.get("X-Stainless-Lang") == "python"
557
558                message = await response.parse()
559                assert_matches_type(AsyncCursorPage[Message], message, path=["response"])
560
561        assert cast(Any, response.is_closed) is True
562
563    @parametrize
564    async def test_path_params_list(self, async_client: AsyncOpenAI) -> None:
565        with pytest.warns(DeprecationWarning):
566            with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
567                await async_client.beta.threads.messages.with_raw_response.list(
568                    thread_id="",
569                )
570
571    @parametrize
572    async def test_method_delete(self, async_client: AsyncOpenAI) -> None:
573        with pytest.warns(DeprecationWarning):
574            message = await async_client.beta.threads.messages.delete(
575                message_id="message_id",
576                thread_id="thread_id",
577            )
578
579        assert_matches_type(MessageDeleted, message, path=["response"])
580
581    @parametrize
582    async def test_raw_response_delete(self, async_client: AsyncOpenAI) -> None:
583        with pytest.warns(DeprecationWarning):
584            response = await async_client.beta.threads.messages.with_raw_response.delete(
585                message_id="message_id",
586                thread_id="thread_id",
587            )
588
589        assert response.is_closed is True
590        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
591        message = response.parse()
592        assert_matches_type(MessageDeleted, message, path=["response"])
593
594    @parametrize
595    async def test_streaming_response_delete(self, async_client: AsyncOpenAI) -> None:
596        with pytest.warns(DeprecationWarning):
597            async with async_client.beta.threads.messages.with_streaming_response.delete(
598                message_id="message_id",
599                thread_id="thread_id",
600            ) as response:
601                assert not response.is_closed
602                assert response.http_request.headers.get("X-Stainless-Lang") == "python"
603
604                message = await response.parse()
605                assert_matches_type(MessageDeleted, message, path=["response"])
606
607        assert cast(Any, response.is_closed) is True
608
609    @parametrize
610    async def test_path_params_delete(self, async_client: AsyncOpenAI) -> None:
611        with pytest.warns(DeprecationWarning):
612            with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
613                await async_client.beta.threads.messages.with_raw_response.delete(
614                    message_id="message_id",
615                    thread_id="",
616                )
617
618            with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"):
619                await async_client.beta.threads.messages.with_raw_response.delete(
620                    message_id="",
621                    thread_id="thread_id",
622                )