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