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