openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/api_resources/beta/test_threads.py
1012lines · modecode
| 1 | # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 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.types.beta import ( |
| 13 | Thread, |
| 14 | ThreadDeleted, |
| 15 | ) |
| 16 | from openai.types.beta.threads import Run |
| 17 | |
| 18 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 19 | |
| 20 | |
| 21 | class TestThreads: |
| 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 | thread = client.beta.threads.create() |
| 27 | assert_matches_type(Thread, thread, path=["response"]) |
| 28 | |
| 29 | @parametrize |
| 30 | def test_method_create_with_all_params(self, client: OpenAI) -> None: |
| 31 | thread = client.beta.threads.create( |
| 32 | messages=[ |
| 33 | { |
| 34 | "role": "user", |
| 35 | "content": "x", |
| 36 | "attachments": [ |
| 37 | { |
| 38 | "file_id": "string", |
| 39 | "add_to": ["file_search", "code_interpreter"], |
| 40 | }, |
| 41 | { |
| 42 | "file_id": "string", |
| 43 | "add_to": ["file_search", "code_interpreter"], |
| 44 | }, |
| 45 | { |
| 46 | "file_id": "string", |
| 47 | "add_to": ["file_search", "code_interpreter"], |
| 48 | }, |
| 49 | ], |
| 50 | "metadata": {}, |
| 51 | }, |
| 52 | { |
| 53 | "role": "user", |
| 54 | "content": "x", |
| 55 | "attachments": [ |
| 56 | { |
| 57 | "file_id": "string", |
| 58 | "add_to": ["file_search", "code_interpreter"], |
| 59 | }, |
| 60 | { |
| 61 | "file_id": "string", |
| 62 | "add_to": ["file_search", "code_interpreter"], |
| 63 | }, |
| 64 | { |
| 65 | "file_id": "string", |
| 66 | "add_to": ["file_search", "code_interpreter"], |
| 67 | }, |
| 68 | ], |
| 69 | "metadata": {}, |
| 70 | }, |
| 71 | { |
| 72 | "role": "user", |
| 73 | "content": "x", |
| 74 | "attachments": [ |
| 75 | { |
| 76 | "file_id": "string", |
| 77 | "add_to": ["file_search", "code_interpreter"], |
| 78 | }, |
| 79 | { |
| 80 | "file_id": "string", |
| 81 | "add_to": ["file_search", "code_interpreter"], |
| 82 | }, |
| 83 | { |
| 84 | "file_id": "string", |
| 85 | "add_to": ["file_search", "code_interpreter"], |
| 86 | }, |
| 87 | ], |
| 88 | "metadata": {}, |
| 89 | }, |
| 90 | ], |
| 91 | metadata={}, |
| 92 | tool_resources={ |
| 93 | "code_interpreter": {"file_ids": ["string", "string", "string"]}, |
| 94 | "file_search": { |
| 95 | "vector_store_ids": ["string"], |
| 96 | "vector_stores": [ |
| 97 | { |
| 98 | "file_ids": ["string", "string", "string"], |
| 99 | "metadata": {}, |
| 100 | } |
| 101 | ], |
| 102 | }, |
| 103 | }, |
| 104 | ) |
| 105 | assert_matches_type(Thread, thread, path=["response"]) |
| 106 | |
| 107 | @parametrize |
| 108 | def test_raw_response_create(self, client: OpenAI) -> None: |
| 109 | response = client.beta.threads.with_raw_response.create() |
| 110 | |
| 111 | assert response.is_closed is True |
| 112 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 113 | thread = response.parse() |
| 114 | assert_matches_type(Thread, thread, path=["response"]) |
| 115 | |
| 116 | @parametrize |
| 117 | def test_streaming_response_create(self, client: OpenAI) -> None: |
| 118 | with client.beta.threads.with_streaming_response.create() as response: |
| 119 | assert not response.is_closed |
| 120 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 121 | |
| 122 | thread = response.parse() |
| 123 | assert_matches_type(Thread, thread, path=["response"]) |
| 124 | |
| 125 | assert cast(Any, response.is_closed) is True |
| 126 | |
| 127 | @parametrize |
| 128 | def test_method_retrieve(self, client: OpenAI) -> None: |
| 129 | thread = client.beta.threads.retrieve( |
| 130 | "string", |
| 131 | ) |
| 132 | assert_matches_type(Thread, thread, path=["response"]) |
| 133 | |
| 134 | @parametrize |
| 135 | def test_raw_response_retrieve(self, client: OpenAI) -> None: |
| 136 | response = client.beta.threads.with_raw_response.retrieve( |
| 137 | "string", |
| 138 | ) |
| 139 | |
| 140 | assert response.is_closed is True |
| 141 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 142 | thread = response.parse() |
| 143 | assert_matches_type(Thread, thread, path=["response"]) |
| 144 | |
| 145 | @parametrize |
| 146 | def test_streaming_response_retrieve(self, client: OpenAI) -> None: |
| 147 | with client.beta.threads.with_streaming_response.retrieve( |
| 148 | "string", |
| 149 | ) as response: |
| 150 | assert not response.is_closed |
| 151 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 152 | |
| 153 | thread = response.parse() |
| 154 | assert_matches_type(Thread, thread, path=["response"]) |
| 155 | |
| 156 | assert cast(Any, response.is_closed) is True |
| 157 | |
| 158 | @parametrize |
| 159 | def test_path_params_retrieve(self, client: OpenAI) -> None: |
| 160 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"): |
| 161 | client.beta.threads.with_raw_response.retrieve( |
| 162 | "", |
| 163 | ) |
| 164 | |
| 165 | @parametrize |
| 166 | def test_method_update(self, client: OpenAI) -> None: |
| 167 | thread = client.beta.threads.update( |
| 168 | "string", |
| 169 | ) |
| 170 | assert_matches_type(Thread, thread, path=["response"]) |
| 171 | |
| 172 | @parametrize |
| 173 | def test_method_update_with_all_params(self, client: OpenAI) -> None: |
| 174 | thread = client.beta.threads.update( |
| 175 | "string", |
| 176 | metadata={}, |
| 177 | tool_resources={ |
| 178 | "code_interpreter": {"file_ids": ["string", "string", "string"]}, |
| 179 | "file_search": {"vector_store_ids": ["string"]}, |
| 180 | }, |
| 181 | ) |
| 182 | assert_matches_type(Thread, thread, path=["response"]) |
| 183 | |
| 184 | @parametrize |
| 185 | def test_raw_response_update(self, client: OpenAI) -> None: |
| 186 | response = client.beta.threads.with_raw_response.update( |
| 187 | "string", |
| 188 | ) |
| 189 | |
| 190 | assert response.is_closed is True |
| 191 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 192 | thread = response.parse() |
| 193 | assert_matches_type(Thread, thread, path=["response"]) |
| 194 | |
| 195 | @parametrize |
| 196 | def test_streaming_response_update(self, client: OpenAI) -> None: |
| 197 | with client.beta.threads.with_streaming_response.update( |
| 198 | "string", |
| 199 | ) as response: |
| 200 | assert not response.is_closed |
| 201 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 202 | |
| 203 | thread = response.parse() |
| 204 | assert_matches_type(Thread, thread, path=["response"]) |
| 205 | |
| 206 | assert cast(Any, response.is_closed) is True |
| 207 | |
| 208 | @parametrize |
| 209 | def test_path_params_update(self, client: OpenAI) -> None: |
| 210 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"): |
| 211 | client.beta.threads.with_raw_response.update( |
| 212 | "", |
| 213 | ) |
| 214 | |
| 215 | @parametrize |
| 216 | def test_method_delete(self, client: OpenAI) -> None: |
| 217 | thread = client.beta.threads.delete( |
| 218 | "string", |
| 219 | ) |
| 220 | assert_matches_type(ThreadDeleted, thread, path=["response"]) |
| 221 | |
| 222 | @parametrize |
| 223 | def test_raw_response_delete(self, client: OpenAI) -> None: |
| 224 | response = client.beta.threads.with_raw_response.delete( |
| 225 | "string", |
| 226 | ) |
| 227 | |
| 228 | assert response.is_closed is True |
| 229 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 230 | thread = response.parse() |
| 231 | assert_matches_type(ThreadDeleted, thread, path=["response"]) |
| 232 | |
| 233 | @parametrize |
| 234 | def test_streaming_response_delete(self, client: OpenAI) -> None: |
| 235 | with client.beta.threads.with_streaming_response.delete( |
| 236 | "string", |
| 237 | ) as response: |
| 238 | assert not response.is_closed |
| 239 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 240 | |
| 241 | thread = response.parse() |
| 242 | assert_matches_type(ThreadDeleted, thread, 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 `thread_id` but received ''"): |
| 249 | client.beta.threads.with_raw_response.delete( |
| 250 | "", |
| 251 | ) |
| 252 | |
| 253 | @parametrize |
| 254 | def test_method_create_and_run_overload_1(self, client: OpenAI) -> None: |
| 255 | thread = client.beta.threads.create_and_run( |
| 256 | assistant_id="string", |
| 257 | ) |
| 258 | assert_matches_type(Run, thread, path=["response"]) |
| 259 | |
| 260 | @parametrize |
| 261 | def test_method_create_and_run_with_all_params_overload_1(self, client: OpenAI) -> None: |
| 262 | thread = client.beta.threads.create_and_run( |
| 263 | assistant_id="string", |
| 264 | instructions="string", |
| 265 | max_completion_tokens=256, |
| 266 | max_prompt_tokens=256, |
| 267 | metadata={}, |
| 268 | model="gpt-4-turbo", |
| 269 | response_format="none", |
| 270 | stream=False, |
| 271 | temperature=1, |
| 272 | thread={ |
| 273 | "messages": [ |
| 274 | { |
| 275 | "role": "user", |
| 276 | "content": "x", |
| 277 | "attachments": [ |
| 278 | { |
| 279 | "file_id": "string", |
| 280 | "add_to": ["file_search", "code_interpreter"], |
| 281 | }, |
| 282 | { |
| 283 | "file_id": "string", |
| 284 | "add_to": ["file_search", "code_interpreter"], |
| 285 | }, |
| 286 | { |
| 287 | "file_id": "string", |
| 288 | "add_to": ["file_search", "code_interpreter"], |
| 289 | }, |
| 290 | ], |
| 291 | "metadata": {}, |
| 292 | }, |
| 293 | { |
| 294 | "role": "user", |
| 295 | "content": "x", |
| 296 | "attachments": [ |
| 297 | { |
| 298 | "file_id": "string", |
| 299 | "add_to": ["file_search", "code_interpreter"], |
| 300 | }, |
| 301 | { |
| 302 | "file_id": "string", |
| 303 | "add_to": ["file_search", "code_interpreter"], |
| 304 | }, |
| 305 | { |
| 306 | "file_id": "string", |
| 307 | "add_to": ["file_search", "code_interpreter"], |
| 308 | }, |
| 309 | ], |
| 310 | "metadata": {}, |
| 311 | }, |
| 312 | { |
| 313 | "role": "user", |
| 314 | "content": "x", |
| 315 | "attachments": [ |
| 316 | { |
| 317 | "file_id": "string", |
| 318 | "add_to": ["file_search", "code_interpreter"], |
| 319 | }, |
| 320 | { |
| 321 | "file_id": "string", |
| 322 | "add_to": ["file_search", "code_interpreter"], |
| 323 | }, |
| 324 | { |
| 325 | "file_id": "string", |
| 326 | "add_to": ["file_search", "code_interpreter"], |
| 327 | }, |
| 328 | ], |
| 329 | "metadata": {}, |
| 330 | }, |
| 331 | ], |
| 332 | "tool_resources": { |
| 333 | "code_interpreter": {"file_ids": ["string", "string", "string"]}, |
| 334 | "file_search": { |
| 335 | "vector_store_ids": ["string"], |
| 336 | "vector_stores": [ |
| 337 | { |
| 338 | "file_ids": ["string", "string", "string"], |
| 339 | "metadata": {}, |
| 340 | } |
| 341 | ], |
| 342 | }, |
| 343 | }, |
| 344 | "metadata": {}, |
| 345 | }, |
| 346 | tool_choice="none", |
| 347 | tool_resources={ |
| 348 | "code_interpreter": {"file_ids": ["string", "string", "string"]}, |
| 349 | "file_search": {"vector_store_ids": ["string"]}, |
| 350 | }, |
| 351 | tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}], |
| 352 | top_p=1, |
| 353 | truncation_strategy={ |
| 354 | "type": "auto", |
| 355 | "last_messages": 1, |
| 356 | }, |
| 357 | ) |
| 358 | assert_matches_type(Run, thread, path=["response"]) |
| 359 | |
| 360 | @parametrize |
| 361 | def test_raw_response_create_and_run_overload_1(self, client: OpenAI) -> None: |
| 362 | response = client.beta.threads.with_raw_response.create_and_run( |
| 363 | assistant_id="string", |
| 364 | ) |
| 365 | |
| 366 | assert response.is_closed is True |
| 367 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 368 | thread = response.parse() |
| 369 | assert_matches_type(Run, thread, path=["response"]) |
| 370 | |
| 371 | @parametrize |
| 372 | def test_streaming_response_create_and_run_overload_1(self, client: OpenAI) -> None: |
| 373 | with client.beta.threads.with_streaming_response.create_and_run( |
| 374 | assistant_id="string", |
| 375 | ) as response: |
| 376 | assert not response.is_closed |
| 377 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 378 | |
| 379 | thread = response.parse() |
| 380 | assert_matches_type(Run, thread, path=["response"]) |
| 381 | |
| 382 | assert cast(Any, response.is_closed) is True |
| 383 | |
| 384 | @parametrize |
| 385 | def test_method_create_and_run_overload_2(self, client: OpenAI) -> None: |
| 386 | thread_stream = client.beta.threads.create_and_run( |
| 387 | assistant_id="string", |
| 388 | stream=True, |
| 389 | ) |
| 390 | thread_stream.response.close() |
| 391 | |
| 392 | @parametrize |
| 393 | def test_method_create_and_run_with_all_params_overload_2(self, client: OpenAI) -> None: |
| 394 | thread_stream = client.beta.threads.create_and_run( |
| 395 | assistant_id="string", |
| 396 | stream=True, |
| 397 | instructions="string", |
| 398 | max_completion_tokens=256, |
| 399 | max_prompt_tokens=256, |
| 400 | metadata={}, |
| 401 | model="gpt-4-turbo", |
| 402 | response_format="none", |
| 403 | temperature=1, |
| 404 | thread={ |
| 405 | "messages": [ |
| 406 | { |
| 407 | "role": "user", |
| 408 | "content": "x", |
| 409 | "attachments": [ |
| 410 | { |
| 411 | "file_id": "string", |
| 412 | "add_to": ["file_search", "code_interpreter"], |
| 413 | }, |
| 414 | { |
| 415 | "file_id": "string", |
| 416 | "add_to": ["file_search", "code_interpreter"], |
| 417 | }, |
| 418 | { |
| 419 | "file_id": "string", |
| 420 | "add_to": ["file_search", "code_interpreter"], |
| 421 | }, |
| 422 | ], |
| 423 | "metadata": {}, |
| 424 | }, |
| 425 | { |
| 426 | "role": "user", |
| 427 | "content": "x", |
| 428 | "attachments": [ |
| 429 | { |
| 430 | "file_id": "string", |
| 431 | "add_to": ["file_search", "code_interpreter"], |
| 432 | }, |
| 433 | { |
| 434 | "file_id": "string", |
| 435 | "add_to": ["file_search", "code_interpreter"], |
| 436 | }, |
| 437 | { |
| 438 | "file_id": "string", |
| 439 | "add_to": ["file_search", "code_interpreter"], |
| 440 | }, |
| 441 | ], |
| 442 | "metadata": {}, |
| 443 | }, |
| 444 | { |
| 445 | "role": "user", |
| 446 | "content": "x", |
| 447 | "attachments": [ |
| 448 | { |
| 449 | "file_id": "string", |
| 450 | "add_to": ["file_search", "code_interpreter"], |
| 451 | }, |
| 452 | { |
| 453 | "file_id": "string", |
| 454 | "add_to": ["file_search", "code_interpreter"], |
| 455 | }, |
| 456 | { |
| 457 | "file_id": "string", |
| 458 | "add_to": ["file_search", "code_interpreter"], |
| 459 | }, |
| 460 | ], |
| 461 | "metadata": {}, |
| 462 | }, |
| 463 | ], |
| 464 | "tool_resources": { |
| 465 | "code_interpreter": {"file_ids": ["string", "string", "string"]}, |
| 466 | "file_search": { |
| 467 | "vector_store_ids": ["string"], |
| 468 | "vector_stores": [ |
| 469 | { |
| 470 | "file_ids": ["string", "string", "string"], |
| 471 | "metadata": {}, |
| 472 | } |
| 473 | ], |
| 474 | }, |
| 475 | }, |
| 476 | "metadata": {}, |
| 477 | }, |
| 478 | tool_choice="none", |
| 479 | tool_resources={ |
| 480 | "code_interpreter": {"file_ids": ["string", "string", "string"]}, |
| 481 | "file_search": {"vector_store_ids": ["string"]}, |
| 482 | }, |
| 483 | tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}], |
| 484 | top_p=1, |
| 485 | truncation_strategy={ |
| 486 | "type": "auto", |
| 487 | "last_messages": 1, |
| 488 | }, |
| 489 | ) |
| 490 | thread_stream.response.close() |
| 491 | |
| 492 | @parametrize |
| 493 | def test_raw_response_create_and_run_overload_2(self, client: OpenAI) -> None: |
| 494 | response = client.beta.threads.with_raw_response.create_and_run( |
| 495 | assistant_id="string", |
| 496 | stream=True, |
| 497 | ) |
| 498 | |
| 499 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 500 | stream = response.parse() |
| 501 | stream.close() |
| 502 | |
| 503 | @parametrize |
| 504 | def test_streaming_response_create_and_run_overload_2(self, client: OpenAI) -> None: |
| 505 | with client.beta.threads.with_streaming_response.create_and_run( |
| 506 | assistant_id="string", |
| 507 | stream=True, |
| 508 | ) as response: |
| 509 | assert not response.is_closed |
| 510 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 511 | |
| 512 | stream = response.parse() |
| 513 | stream.close() |
| 514 | |
| 515 | assert cast(Any, response.is_closed) is True |
| 516 | |
| 517 | |
| 518 | class TestAsyncThreads: |
| 519 | parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 520 | |
| 521 | @parametrize |
| 522 | async def test_method_create(self, async_client: AsyncOpenAI) -> None: |
| 523 | thread = await async_client.beta.threads.create() |
| 524 | assert_matches_type(Thread, thread, path=["response"]) |
| 525 | |
| 526 | @parametrize |
| 527 | async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 528 | thread = await async_client.beta.threads.create( |
| 529 | messages=[ |
| 530 | { |
| 531 | "role": "user", |
| 532 | "content": "x", |
| 533 | "attachments": [ |
| 534 | { |
| 535 | "file_id": "string", |
| 536 | "add_to": ["file_search", "code_interpreter"], |
| 537 | }, |
| 538 | { |
| 539 | "file_id": "string", |
| 540 | "add_to": ["file_search", "code_interpreter"], |
| 541 | }, |
| 542 | { |
| 543 | "file_id": "string", |
| 544 | "add_to": ["file_search", "code_interpreter"], |
| 545 | }, |
| 546 | ], |
| 547 | "metadata": {}, |
| 548 | }, |
| 549 | { |
| 550 | "role": "user", |
| 551 | "content": "x", |
| 552 | "attachments": [ |
| 553 | { |
| 554 | "file_id": "string", |
| 555 | "add_to": ["file_search", "code_interpreter"], |
| 556 | }, |
| 557 | { |
| 558 | "file_id": "string", |
| 559 | "add_to": ["file_search", "code_interpreter"], |
| 560 | }, |
| 561 | { |
| 562 | "file_id": "string", |
| 563 | "add_to": ["file_search", "code_interpreter"], |
| 564 | }, |
| 565 | ], |
| 566 | "metadata": {}, |
| 567 | }, |
| 568 | { |
| 569 | "role": "user", |
| 570 | "content": "x", |
| 571 | "attachments": [ |
| 572 | { |
| 573 | "file_id": "string", |
| 574 | "add_to": ["file_search", "code_interpreter"], |
| 575 | }, |
| 576 | { |
| 577 | "file_id": "string", |
| 578 | "add_to": ["file_search", "code_interpreter"], |
| 579 | }, |
| 580 | { |
| 581 | "file_id": "string", |
| 582 | "add_to": ["file_search", "code_interpreter"], |
| 583 | }, |
| 584 | ], |
| 585 | "metadata": {}, |
| 586 | }, |
| 587 | ], |
| 588 | metadata={}, |
| 589 | tool_resources={ |
| 590 | "code_interpreter": {"file_ids": ["string", "string", "string"]}, |
| 591 | "file_search": { |
| 592 | "vector_store_ids": ["string"], |
| 593 | "vector_stores": [ |
| 594 | { |
| 595 | "file_ids": ["string", "string", "string"], |
| 596 | "metadata": {}, |
| 597 | } |
| 598 | ], |
| 599 | }, |
| 600 | }, |
| 601 | ) |
| 602 | assert_matches_type(Thread, thread, path=["response"]) |
| 603 | |
| 604 | @parametrize |
| 605 | async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None: |
| 606 | response = await async_client.beta.threads.with_raw_response.create() |
| 607 | |
| 608 | assert response.is_closed is True |
| 609 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 610 | thread = response.parse() |
| 611 | assert_matches_type(Thread, thread, path=["response"]) |
| 612 | |
| 613 | @parametrize |
| 614 | async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None: |
| 615 | async with async_client.beta.threads.with_streaming_response.create() as response: |
| 616 | assert not response.is_closed |
| 617 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 618 | |
| 619 | thread = await response.parse() |
| 620 | assert_matches_type(Thread, thread, path=["response"]) |
| 621 | |
| 622 | assert cast(Any, response.is_closed) is True |
| 623 | |
| 624 | @parametrize |
| 625 | async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 626 | thread = await async_client.beta.threads.retrieve( |
| 627 | "string", |
| 628 | ) |
| 629 | assert_matches_type(Thread, thread, path=["response"]) |
| 630 | |
| 631 | @parametrize |
| 632 | async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 633 | response = await async_client.beta.threads.with_raw_response.retrieve( |
| 634 | "string", |
| 635 | ) |
| 636 | |
| 637 | assert response.is_closed is True |
| 638 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 639 | thread = response.parse() |
| 640 | assert_matches_type(Thread, thread, path=["response"]) |
| 641 | |
| 642 | @parametrize |
| 643 | async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 644 | async with async_client.beta.threads.with_streaming_response.retrieve( |
| 645 | "string", |
| 646 | ) as response: |
| 647 | assert not response.is_closed |
| 648 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 649 | |
| 650 | thread = await response.parse() |
| 651 | assert_matches_type(Thread, thread, path=["response"]) |
| 652 | |
| 653 | assert cast(Any, response.is_closed) is True |
| 654 | |
| 655 | @parametrize |
| 656 | async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 657 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"): |
| 658 | await async_client.beta.threads.with_raw_response.retrieve( |
| 659 | "", |
| 660 | ) |
| 661 | |
| 662 | @parametrize |
| 663 | async def test_method_update(self, async_client: AsyncOpenAI) -> None: |
| 664 | thread = await async_client.beta.threads.update( |
| 665 | "string", |
| 666 | ) |
| 667 | assert_matches_type(Thread, thread, path=["response"]) |
| 668 | |
| 669 | @parametrize |
| 670 | async def test_method_update_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 671 | thread = await async_client.beta.threads.update( |
| 672 | "string", |
| 673 | metadata={}, |
| 674 | tool_resources={ |
| 675 | "code_interpreter": {"file_ids": ["string", "string", "string"]}, |
| 676 | "file_search": {"vector_store_ids": ["string"]}, |
| 677 | }, |
| 678 | ) |
| 679 | assert_matches_type(Thread, thread, path=["response"]) |
| 680 | |
| 681 | @parametrize |
| 682 | async def test_raw_response_update(self, async_client: AsyncOpenAI) -> None: |
| 683 | response = await async_client.beta.threads.with_raw_response.update( |
| 684 | "string", |
| 685 | ) |
| 686 | |
| 687 | assert response.is_closed is True |
| 688 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 689 | thread = response.parse() |
| 690 | assert_matches_type(Thread, thread, path=["response"]) |
| 691 | |
| 692 | @parametrize |
| 693 | async def test_streaming_response_update(self, async_client: AsyncOpenAI) -> None: |
| 694 | async with async_client.beta.threads.with_streaming_response.update( |
| 695 | "string", |
| 696 | ) as response: |
| 697 | assert not response.is_closed |
| 698 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 699 | |
| 700 | thread = await response.parse() |
| 701 | assert_matches_type(Thread, thread, path=["response"]) |
| 702 | |
| 703 | assert cast(Any, response.is_closed) is True |
| 704 | |
| 705 | @parametrize |
| 706 | async def test_path_params_update(self, async_client: AsyncOpenAI) -> None: |
| 707 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"): |
| 708 | await async_client.beta.threads.with_raw_response.update( |
| 709 | "", |
| 710 | ) |
| 711 | |
| 712 | @parametrize |
| 713 | async def test_method_delete(self, async_client: AsyncOpenAI) -> None: |
| 714 | thread = await async_client.beta.threads.delete( |
| 715 | "string", |
| 716 | ) |
| 717 | assert_matches_type(ThreadDeleted, thread, path=["response"]) |
| 718 | |
| 719 | @parametrize |
| 720 | async def test_raw_response_delete(self, async_client: AsyncOpenAI) -> None: |
| 721 | response = await async_client.beta.threads.with_raw_response.delete( |
| 722 | "string", |
| 723 | ) |
| 724 | |
| 725 | assert response.is_closed is True |
| 726 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 727 | thread = response.parse() |
| 728 | assert_matches_type(ThreadDeleted, thread, path=["response"]) |
| 729 | |
| 730 | @parametrize |
| 731 | async def test_streaming_response_delete(self, async_client: AsyncOpenAI) -> None: |
| 732 | async with async_client.beta.threads.with_streaming_response.delete( |
| 733 | "string", |
| 734 | ) as response: |
| 735 | assert not response.is_closed |
| 736 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 737 | |
| 738 | thread = await response.parse() |
| 739 | assert_matches_type(ThreadDeleted, thread, path=["response"]) |
| 740 | |
| 741 | assert cast(Any, response.is_closed) is True |
| 742 | |
| 743 | @parametrize |
| 744 | async def test_path_params_delete(self, async_client: AsyncOpenAI) -> None: |
| 745 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"): |
| 746 | await async_client.beta.threads.with_raw_response.delete( |
| 747 | "", |
| 748 | ) |
| 749 | |
| 750 | @parametrize |
| 751 | async def test_method_create_and_run_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 752 | thread = await async_client.beta.threads.create_and_run( |
| 753 | assistant_id="string", |
| 754 | ) |
| 755 | assert_matches_type(Run, thread, path=["response"]) |
| 756 | |
| 757 | @parametrize |
| 758 | async def test_method_create_and_run_with_all_params_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 759 | thread = await async_client.beta.threads.create_and_run( |
| 760 | assistant_id="string", |
| 761 | instructions="string", |
| 762 | max_completion_tokens=256, |
| 763 | max_prompt_tokens=256, |
| 764 | metadata={}, |
| 765 | model="gpt-4-turbo", |
| 766 | response_format="none", |
| 767 | stream=False, |
| 768 | temperature=1, |
| 769 | thread={ |
| 770 | "messages": [ |
| 771 | { |
| 772 | "role": "user", |
| 773 | "content": "x", |
| 774 | "attachments": [ |
| 775 | { |
| 776 | "file_id": "string", |
| 777 | "add_to": ["file_search", "code_interpreter"], |
| 778 | }, |
| 779 | { |
| 780 | "file_id": "string", |
| 781 | "add_to": ["file_search", "code_interpreter"], |
| 782 | }, |
| 783 | { |
| 784 | "file_id": "string", |
| 785 | "add_to": ["file_search", "code_interpreter"], |
| 786 | }, |
| 787 | ], |
| 788 | "metadata": {}, |
| 789 | }, |
| 790 | { |
| 791 | "role": "user", |
| 792 | "content": "x", |
| 793 | "attachments": [ |
| 794 | { |
| 795 | "file_id": "string", |
| 796 | "add_to": ["file_search", "code_interpreter"], |
| 797 | }, |
| 798 | { |
| 799 | "file_id": "string", |
| 800 | "add_to": ["file_search", "code_interpreter"], |
| 801 | }, |
| 802 | { |
| 803 | "file_id": "string", |
| 804 | "add_to": ["file_search", "code_interpreter"], |
| 805 | }, |
| 806 | ], |
| 807 | "metadata": {}, |
| 808 | }, |
| 809 | { |
| 810 | "role": "user", |
| 811 | "content": "x", |
| 812 | "attachments": [ |
| 813 | { |
| 814 | "file_id": "string", |
| 815 | "add_to": ["file_search", "code_interpreter"], |
| 816 | }, |
| 817 | { |
| 818 | "file_id": "string", |
| 819 | "add_to": ["file_search", "code_interpreter"], |
| 820 | }, |
| 821 | { |
| 822 | "file_id": "string", |
| 823 | "add_to": ["file_search", "code_interpreter"], |
| 824 | }, |
| 825 | ], |
| 826 | "metadata": {}, |
| 827 | }, |
| 828 | ], |
| 829 | "tool_resources": { |
| 830 | "code_interpreter": {"file_ids": ["string", "string", "string"]}, |
| 831 | "file_search": { |
| 832 | "vector_store_ids": ["string"], |
| 833 | "vector_stores": [ |
| 834 | { |
| 835 | "file_ids": ["string", "string", "string"], |
| 836 | "metadata": {}, |
| 837 | } |
| 838 | ], |
| 839 | }, |
| 840 | }, |
| 841 | "metadata": {}, |
| 842 | }, |
| 843 | tool_choice="none", |
| 844 | tool_resources={ |
| 845 | "code_interpreter": {"file_ids": ["string", "string", "string"]}, |
| 846 | "file_search": {"vector_store_ids": ["string"]}, |
| 847 | }, |
| 848 | tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}], |
| 849 | top_p=1, |
| 850 | truncation_strategy={ |
| 851 | "type": "auto", |
| 852 | "last_messages": 1, |
| 853 | }, |
| 854 | ) |
| 855 | assert_matches_type(Run, thread, path=["response"]) |
| 856 | |
| 857 | @parametrize |
| 858 | async def test_raw_response_create_and_run_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 859 | response = await async_client.beta.threads.with_raw_response.create_and_run( |
| 860 | assistant_id="string", |
| 861 | ) |
| 862 | |
| 863 | assert response.is_closed is True |
| 864 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 865 | thread = response.parse() |
| 866 | assert_matches_type(Run, thread, path=["response"]) |
| 867 | |
| 868 | @parametrize |
| 869 | async def test_streaming_response_create_and_run_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 870 | async with async_client.beta.threads.with_streaming_response.create_and_run( |
| 871 | assistant_id="string", |
| 872 | ) as response: |
| 873 | assert not response.is_closed |
| 874 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 875 | |
| 876 | thread = await response.parse() |
| 877 | assert_matches_type(Run, thread, path=["response"]) |
| 878 | |
| 879 | assert cast(Any, response.is_closed) is True |
| 880 | |
| 881 | @parametrize |
| 882 | async def test_method_create_and_run_overload_2(self, async_client: AsyncOpenAI) -> None: |
| 883 | thread_stream = await async_client.beta.threads.create_and_run( |
| 884 | assistant_id="string", |
| 885 | stream=True, |
| 886 | ) |
| 887 | await thread_stream.response.aclose() |
| 888 | |
| 889 | @parametrize |
| 890 | async def test_method_create_and_run_with_all_params_overload_2(self, async_client: AsyncOpenAI) -> None: |
| 891 | thread_stream = await async_client.beta.threads.create_and_run( |
| 892 | assistant_id="string", |
| 893 | stream=True, |
| 894 | instructions="string", |
| 895 | max_completion_tokens=256, |
| 896 | max_prompt_tokens=256, |
| 897 | metadata={}, |
| 898 | model="gpt-4-turbo", |
| 899 | response_format="none", |
| 900 | temperature=1, |
| 901 | thread={ |
| 902 | "messages": [ |
| 903 | { |
| 904 | "role": "user", |
| 905 | "content": "x", |
| 906 | "attachments": [ |
| 907 | { |
| 908 | "file_id": "string", |
| 909 | "add_to": ["file_search", "code_interpreter"], |
| 910 | }, |
| 911 | { |
| 912 | "file_id": "string", |
| 913 | "add_to": ["file_search", "code_interpreter"], |
| 914 | }, |
| 915 | { |
| 916 | "file_id": "string", |
| 917 | "add_to": ["file_search", "code_interpreter"], |
| 918 | }, |
| 919 | ], |
| 920 | "metadata": {}, |
| 921 | }, |
| 922 | { |
| 923 | "role": "user", |
| 924 | "content": "x", |
| 925 | "attachments": [ |
| 926 | { |
| 927 | "file_id": "string", |
| 928 | "add_to": ["file_search", "code_interpreter"], |
| 929 | }, |
| 930 | { |
| 931 | "file_id": "string", |
| 932 | "add_to": ["file_search", "code_interpreter"], |
| 933 | }, |
| 934 | { |
| 935 | "file_id": "string", |
| 936 | "add_to": ["file_search", "code_interpreter"], |
| 937 | }, |
| 938 | ], |
| 939 | "metadata": {}, |
| 940 | }, |
| 941 | { |
| 942 | "role": "user", |
| 943 | "content": "x", |
| 944 | "attachments": [ |
| 945 | { |
| 946 | "file_id": "string", |
| 947 | "add_to": ["file_search", "code_interpreter"], |
| 948 | }, |
| 949 | { |
| 950 | "file_id": "string", |
| 951 | "add_to": ["file_search", "code_interpreter"], |
| 952 | }, |
| 953 | { |
| 954 | "file_id": "string", |
| 955 | "add_to": ["file_search", "code_interpreter"], |
| 956 | }, |
| 957 | ], |
| 958 | "metadata": {}, |
| 959 | }, |
| 960 | ], |
| 961 | "tool_resources": { |
| 962 | "code_interpreter": {"file_ids": ["string", "string", "string"]}, |
| 963 | "file_search": { |
| 964 | "vector_store_ids": ["string"], |
| 965 | "vector_stores": [ |
| 966 | { |
| 967 | "file_ids": ["string", "string", "string"], |
| 968 | "metadata": {}, |
| 969 | } |
| 970 | ], |
| 971 | }, |
| 972 | }, |
| 973 | "metadata": {}, |
| 974 | }, |
| 975 | tool_choice="none", |
| 976 | tool_resources={ |
| 977 | "code_interpreter": {"file_ids": ["string", "string", "string"]}, |
| 978 | "file_search": {"vector_store_ids": ["string"]}, |
| 979 | }, |
| 980 | tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}], |
| 981 | top_p=1, |
| 982 | truncation_strategy={ |
| 983 | "type": "auto", |
| 984 | "last_messages": 1, |
| 985 | }, |
| 986 | ) |
| 987 | await thread_stream.response.aclose() |
| 988 | |
| 989 | @parametrize |
| 990 | async def test_raw_response_create_and_run_overload_2(self, async_client: AsyncOpenAI) -> None: |
| 991 | response = await async_client.beta.threads.with_raw_response.create_and_run( |
| 992 | assistant_id="string", |
| 993 | stream=True, |
| 994 | ) |
| 995 | |
| 996 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 997 | stream = response.parse() |
| 998 | await stream.close() |
| 999 | |
| 1000 | @parametrize |
| 1001 | async def test_streaming_response_create_and_run_overload_2(self, async_client: AsyncOpenAI) -> None: |
| 1002 | async with async_client.beta.threads.with_streaming_response.create_and_run( |
| 1003 | assistant_id="string", |
| 1004 | stream=True, |
| 1005 | ) as response: |
| 1006 | assert not response.is_closed |
| 1007 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 1008 | |
| 1009 | stream = await response.parse() |
| 1010 | await stream.close() |
| 1011 | |
| 1012 | assert cast(Any, response.is_closed) is True |
| 1013 | |