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