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