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 SyncConversationCursorPage, AsyncConversationCursorPage
 13from openai.types.conversations import (
 14    Conversation,
 15    ConversationItem,
 16    ConversationItemList,
 17)
 18
 19base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
 20
 21
 22class TestItems:
 23    parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
 24
 25    @parametrize
 26    def test_method_create(self, client: OpenAI) -> None:
 27        item = client.conversations.items.create(
 28            conversation_id="conv_123",
 29            items=[
 30                {
 31                    "content": "string",
 32                    "role": "user",
 33                    "type": "message",
 34                }
 35            ],
 36        )
 37        assert_matches_type(ConversationItemList, item, path=["response"])
 38
 39    @parametrize
 40    def test_method_create_with_all_params(self, client: OpenAI) -> None:
 41        item = client.conversations.items.create(
 42            conversation_id="conv_123",
 43            items=[
 44                {
 45                    "content": "string",
 46                    "role": "user",
 47                    "type": "message",
 48                }
 49            ],
 50            include=["file_search_call.results"],
 51        )
 52        assert_matches_type(ConversationItemList, item, path=["response"])
 53
 54    @parametrize
 55    def test_raw_response_create(self, client: OpenAI) -> None:
 56        response = client.conversations.items.with_raw_response.create(
 57            conversation_id="conv_123",
 58            items=[
 59                {
 60                    "content": "string",
 61                    "role": "user",
 62                    "type": "message",
 63                }
 64            ],
 65        )
 66
 67        assert response.is_closed is True
 68        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
 69        item = response.parse()
 70        assert_matches_type(ConversationItemList, item, path=["response"])
 71
 72    @parametrize
 73    def test_streaming_response_create(self, client: OpenAI) -> None:
 74        with client.conversations.items.with_streaming_response.create(
 75            conversation_id="conv_123",
 76            items=[
 77                {
 78                    "content": "string",
 79                    "role": "user",
 80                    "type": "message",
 81                }
 82            ],
 83        ) as response:
 84            assert not response.is_closed
 85            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
 86
 87            item = response.parse()
 88            assert_matches_type(ConversationItemList, item, path=["response"])
 89
 90        assert cast(Any, response.is_closed) is True
 91
 92    @parametrize
 93    def test_path_params_create(self, client: OpenAI) -> None:
 94        with pytest.raises(ValueError, match=r"Expected a non-empty value for `conversation_id` but received ''"):
 95            client.conversations.items.with_raw_response.create(
 96                conversation_id="",
 97                items=[
 98                    {
 99                        "content": "string",
100                        "role": "user",
101                        "type": "message",
102                    }
103                ],
104            )
105
106    @parametrize
107    def test_method_retrieve(self, client: OpenAI) -> None:
108        item = client.conversations.items.retrieve(
109            item_id="msg_abc",
110            conversation_id="conv_123",
111        )
112        assert_matches_type(ConversationItem, item, path=["response"])
113
114    @parametrize
115    def test_method_retrieve_with_all_params(self, client: OpenAI) -> None:
116        item = client.conversations.items.retrieve(
117            item_id="msg_abc",
118            conversation_id="conv_123",
119            include=["file_search_call.results"],
120        )
121        assert_matches_type(ConversationItem, item, path=["response"])
122
123    @parametrize
124    def test_raw_response_retrieve(self, client: OpenAI) -> None:
125        response = client.conversations.items.with_raw_response.retrieve(
126            item_id="msg_abc",
127            conversation_id="conv_123",
128        )
129
130        assert response.is_closed is True
131        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
132        item = response.parse()
133        assert_matches_type(ConversationItem, item, path=["response"])
134
135    @parametrize
136    def test_streaming_response_retrieve(self, client: OpenAI) -> None:
137        with client.conversations.items.with_streaming_response.retrieve(
138            item_id="msg_abc",
139            conversation_id="conv_123",
140        ) as response:
141            assert not response.is_closed
142            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
143
144            item = response.parse()
145            assert_matches_type(ConversationItem, item, path=["response"])
146
147        assert cast(Any, response.is_closed) is True
148
149    @parametrize
150    def test_path_params_retrieve(self, client: OpenAI) -> None:
151        with pytest.raises(ValueError, match=r"Expected a non-empty value for `conversation_id` but received ''"):
152            client.conversations.items.with_raw_response.retrieve(
153                item_id="msg_abc",
154                conversation_id="",
155            )
156
157        with pytest.raises(ValueError, match=r"Expected a non-empty value for `item_id` but received ''"):
158            client.conversations.items.with_raw_response.retrieve(
159                item_id="",
160                conversation_id="conv_123",
161            )
162
163    @parametrize
164    def test_method_list(self, client: OpenAI) -> None:
165        item = client.conversations.items.list(
166            conversation_id="conv_123",
167        )
168        assert_matches_type(SyncConversationCursorPage[ConversationItem], item, path=["response"])
169
170    @parametrize
171    def test_method_list_with_all_params(self, client: OpenAI) -> None:
172        item = client.conversations.items.list(
173            conversation_id="conv_123",
174            after="after",
175            include=["file_search_call.results"],
176            limit=0,
177            order="asc",
178        )
179        assert_matches_type(SyncConversationCursorPage[ConversationItem], item, path=["response"])
180
181    @parametrize
182    def test_raw_response_list(self, client: OpenAI) -> None:
183        response = client.conversations.items.with_raw_response.list(
184            conversation_id="conv_123",
185        )
186
187        assert response.is_closed is True
188        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
189        item = response.parse()
190        assert_matches_type(SyncConversationCursorPage[ConversationItem], item, path=["response"])
191
192    @parametrize
193    def test_streaming_response_list(self, client: OpenAI) -> None:
194        with client.conversations.items.with_streaming_response.list(
195            conversation_id="conv_123",
196        ) as response:
197            assert not response.is_closed
198            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
199
200            item = response.parse()
201            assert_matches_type(SyncConversationCursorPage[ConversationItem], item, path=["response"])
202
203        assert cast(Any, response.is_closed) is True
204
205    @parametrize
206    def test_path_params_list(self, client: OpenAI) -> None:
207        with pytest.raises(ValueError, match=r"Expected a non-empty value for `conversation_id` but received ''"):
208            client.conversations.items.with_raw_response.list(
209                conversation_id="",
210            )
211
212    @parametrize
213    def test_method_delete(self, client: OpenAI) -> None:
214        item = client.conversations.items.delete(
215            item_id="msg_abc",
216            conversation_id="conv_123",
217        )
218        assert_matches_type(Conversation, item, path=["response"])
219
220    @parametrize
221    def test_raw_response_delete(self, client: OpenAI) -> None:
222        response = client.conversations.items.with_raw_response.delete(
223            item_id="msg_abc",
224            conversation_id="conv_123",
225        )
226
227        assert response.is_closed is True
228        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
229        item = response.parse()
230        assert_matches_type(Conversation, item, path=["response"])
231
232    @parametrize
233    def test_streaming_response_delete(self, client: OpenAI) -> None:
234        with client.conversations.items.with_streaming_response.delete(
235            item_id="msg_abc",
236            conversation_id="conv_123",
237        ) as response:
238            assert not response.is_closed
239            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
240
241            item = response.parse()
242            assert_matches_type(Conversation, item, path=["response"])
243
244        assert cast(Any, response.is_closed) is True
245
246    @parametrize
247    def test_path_params_delete(self, client: OpenAI) -> None:
248        with pytest.raises(ValueError, match=r"Expected a non-empty value for `conversation_id` but received ''"):
249            client.conversations.items.with_raw_response.delete(
250                item_id="msg_abc",
251                conversation_id="",
252            )
253
254        with pytest.raises(ValueError, match=r"Expected a non-empty value for `item_id` but received ''"):
255            client.conversations.items.with_raw_response.delete(
256                item_id="",
257                conversation_id="conv_123",
258            )
259
260
261class TestAsyncItems:
262    parametrize = pytest.mark.parametrize(
263        "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
264    )
265
266    @parametrize
267    async def test_method_create(self, async_client: AsyncOpenAI) -> None:
268        item = await async_client.conversations.items.create(
269            conversation_id="conv_123",
270            items=[
271                {
272                    "content": "string",
273                    "role": "user",
274                    "type": "message",
275                }
276            ],
277        )
278        assert_matches_type(ConversationItemList, item, path=["response"])
279
280    @parametrize
281    async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
282        item = await async_client.conversations.items.create(
283            conversation_id="conv_123",
284            items=[
285                {
286                    "content": "string",
287                    "role": "user",
288                    "type": "message",
289                }
290            ],
291            include=["file_search_call.results"],
292        )
293        assert_matches_type(ConversationItemList, item, path=["response"])
294
295    @parametrize
296    async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
297        response = await async_client.conversations.items.with_raw_response.create(
298            conversation_id="conv_123",
299            items=[
300                {
301                    "content": "string",
302                    "role": "user",
303                    "type": "message",
304                }
305            ],
306        )
307
308        assert response.is_closed is True
309        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
310        item = response.parse()
311        assert_matches_type(ConversationItemList, item, path=["response"])
312
313    @parametrize
314    async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
315        async with async_client.conversations.items.with_streaming_response.create(
316            conversation_id="conv_123",
317            items=[
318                {
319                    "content": "string",
320                    "role": "user",
321                    "type": "message",
322                }
323            ],
324        ) as response:
325            assert not response.is_closed
326            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
327
328            item = await response.parse()
329            assert_matches_type(ConversationItemList, item, path=["response"])
330
331        assert cast(Any, response.is_closed) is True
332
333    @parametrize
334    async def test_path_params_create(self, async_client: AsyncOpenAI) -> None:
335        with pytest.raises(ValueError, match=r"Expected a non-empty value for `conversation_id` but received ''"):
336            await async_client.conversations.items.with_raw_response.create(
337                conversation_id="",
338                items=[
339                    {
340                        "content": "string",
341                        "role": "user",
342                        "type": "message",
343                    }
344                ],
345            )
346
347    @parametrize
348    async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None:
349        item = await async_client.conversations.items.retrieve(
350            item_id="msg_abc",
351            conversation_id="conv_123",
352        )
353        assert_matches_type(ConversationItem, item, path=["response"])
354
355    @parametrize
356    async def test_method_retrieve_with_all_params(self, async_client: AsyncOpenAI) -> None:
357        item = await async_client.conversations.items.retrieve(
358            item_id="msg_abc",
359            conversation_id="conv_123",
360            include=["file_search_call.results"],
361        )
362        assert_matches_type(ConversationItem, item, path=["response"])
363
364    @parametrize
365    async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None:
366        response = await async_client.conversations.items.with_raw_response.retrieve(
367            item_id="msg_abc",
368            conversation_id="conv_123",
369        )
370
371        assert response.is_closed is True
372        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
373        item = response.parse()
374        assert_matches_type(ConversationItem, item, path=["response"])
375
376    @parametrize
377    async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None:
378        async with async_client.conversations.items.with_streaming_response.retrieve(
379            item_id="msg_abc",
380            conversation_id="conv_123",
381        ) as response:
382            assert not response.is_closed
383            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
384
385            item = await response.parse()
386            assert_matches_type(ConversationItem, item, path=["response"])
387
388        assert cast(Any, response.is_closed) is True
389
390    @parametrize
391    async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
392        with pytest.raises(ValueError, match=r"Expected a non-empty value for `conversation_id` but received ''"):
393            await async_client.conversations.items.with_raw_response.retrieve(
394                item_id="msg_abc",
395                conversation_id="",
396            )
397
398        with pytest.raises(ValueError, match=r"Expected a non-empty value for `item_id` but received ''"):
399            await async_client.conversations.items.with_raw_response.retrieve(
400                item_id="",
401                conversation_id="conv_123",
402            )
403
404    @parametrize
405    async def test_method_list(self, async_client: AsyncOpenAI) -> None:
406        item = await async_client.conversations.items.list(
407            conversation_id="conv_123",
408        )
409        assert_matches_type(AsyncConversationCursorPage[ConversationItem], item, path=["response"])
410
411    @parametrize
412    async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None:
413        item = await async_client.conversations.items.list(
414            conversation_id="conv_123",
415            after="after",
416            include=["file_search_call.results"],
417            limit=0,
418            order="asc",
419        )
420        assert_matches_type(AsyncConversationCursorPage[ConversationItem], item, path=["response"])
421
422    @parametrize
423    async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None:
424        response = await async_client.conversations.items.with_raw_response.list(
425            conversation_id="conv_123",
426        )
427
428        assert response.is_closed is True
429        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
430        item = response.parse()
431        assert_matches_type(AsyncConversationCursorPage[ConversationItem], item, path=["response"])
432
433    @parametrize
434    async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None:
435        async with async_client.conversations.items.with_streaming_response.list(
436            conversation_id="conv_123",
437        ) as response:
438            assert not response.is_closed
439            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
440
441            item = await response.parse()
442            assert_matches_type(AsyncConversationCursorPage[ConversationItem], item, path=["response"])
443
444        assert cast(Any, response.is_closed) is True
445
446    @parametrize
447    async def test_path_params_list(self, async_client: AsyncOpenAI) -> None:
448        with pytest.raises(ValueError, match=r"Expected a non-empty value for `conversation_id` but received ''"):
449            await async_client.conversations.items.with_raw_response.list(
450                conversation_id="",
451            )
452
453    @parametrize
454    async def test_method_delete(self, async_client: AsyncOpenAI) -> None:
455        item = await async_client.conversations.items.delete(
456            item_id="msg_abc",
457            conversation_id="conv_123",
458        )
459        assert_matches_type(Conversation, item, path=["response"])
460
461    @parametrize
462    async def test_raw_response_delete(self, async_client: AsyncOpenAI) -> None:
463        response = await async_client.conversations.items.with_raw_response.delete(
464            item_id="msg_abc",
465            conversation_id="conv_123",
466        )
467
468        assert response.is_closed is True
469        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
470        item = response.parse()
471        assert_matches_type(Conversation, item, path=["response"])
472
473    @parametrize
474    async def test_streaming_response_delete(self, async_client: AsyncOpenAI) -> None:
475        async with async_client.conversations.items.with_streaming_response.delete(
476            item_id="msg_abc",
477            conversation_id="conv_123",
478        ) as response:
479            assert not response.is_closed
480            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
481
482            item = await response.parse()
483            assert_matches_type(Conversation, item, path=["response"])
484
485        assert cast(Any, response.is_closed) is True
486
487    @parametrize
488    async def test_path_params_delete(self, async_client: AsyncOpenAI) -> None:
489        with pytest.raises(ValueError, match=r"Expected a non-empty value for `conversation_id` but received ''"):
490            await async_client.conversations.items.with_raw_response.delete(
491                item_id="msg_abc",
492                conversation_id="",
493            )
494
495        with pytest.raises(ValueError, match=r"Expected a non-empty value for `item_id` but received ''"):
496            await async_client.conversations.items.with_raw_response.delete(
497                item_id="",
498                conversation_id="conv_123",
499            )