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