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