openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/api_resources/beta/test_threads.py
468lines · 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_method_update(self, client: OpenAI) -> None: |
| 113 | thread = client.beta.threads.update( |
| 114 | "string", |
| 115 | ) |
| 116 | assert_matches_type(Thread, thread, path=["response"]) |
| 117 | |
| 118 | @parametrize |
| 119 | def test_method_update_with_all_params(self, client: OpenAI) -> None: |
| 120 | thread = client.beta.threads.update( |
| 121 | "string", |
| 122 | metadata={}, |
| 123 | ) |
| 124 | assert_matches_type(Thread, thread, path=["response"]) |
| 125 | |
| 126 | @parametrize |
| 127 | def test_raw_response_update(self, client: OpenAI) -> None: |
| 128 | response = client.beta.threads.with_raw_response.update( |
| 129 | "string", |
| 130 | ) |
| 131 | |
| 132 | assert response.is_closed is True |
| 133 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 134 | thread = response.parse() |
| 135 | assert_matches_type(Thread, thread, path=["response"]) |
| 136 | |
| 137 | @parametrize |
| 138 | def test_streaming_response_update(self, client: OpenAI) -> None: |
| 139 | with client.beta.threads.with_streaming_response.update( |
| 140 | "string", |
| 141 | ) as response: |
| 142 | assert not response.is_closed |
| 143 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 144 | |
| 145 | thread = response.parse() |
| 146 | assert_matches_type(Thread, thread, path=["response"]) |
| 147 | |
| 148 | assert cast(Any, response.is_closed) is True |
| 149 | |
| 150 | @parametrize |
| 151 | def test_method_delete(self, client: OpenAI) -> None: |
| 152 | thread = client.beta.threads.delete( |
| 153 | "string", |
| 154 | ) |
| 155 | assert_matches_type(ThreadDeleted, thread, path=["response"]) |
| 156 | |
| 157 | @parametrize |
| 158 | def test_raw_response_delete(self, client: OpenAI) -> None: |
| 159 | response = client.beta.threads.with_raw_response.delete( |
| 160 | "string", |
| 161 | ) |
| 162 | |
| 163 | assert response.is_closed is True |
| 164 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 165 | thread = response.parse() |
| 166 | assert_matches_type(ThreadDeleted, thread, path=["response"]) |
| 167 | |
| 168 | @parametrize |
| 169 | def test_streaming_response_delete(self, client: OpenAI) -> None: |
| 170 | with client.beta.threads.with_streaming_response.delete( |
| 171 | "string", |
| 172 | ) as response: |
| 173 | assert not response.is_closed |
| 174 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 175 | |
| 176 | thread = response.parse() |
| 177 | assert_matches_type(ThreadDeleted, thread, path=["response"]) |
| 178 | |
| 179 | assert cast(Any, response.is_closed) is True |
| 180 | |
| 181 | @parametrize |
| 182 | def test_method_create_and_run(self, client: OpenAI) -> None: |
| 183 | thread = client.beta.threads.create_and_run( |
| 184 | assistant_id="string", |
| 185 | ) |
| 186 | assert_matches_type(Run, thread, path=["response"]) |
| 187 | |
| 188 | @parametrize |
| 189 | def test_method_create_and_run_with_all_params(self, client: OpenAI) -> None: |
| 190 | thread = client.beta.threads.create_and_run( |
| 191 | assistant_id="string", |
| 192 | instructions="string", |
| 193 | metadata={}, |
| 194 | model="string", |
| 195 | thread={ |
| 196 | "messages": [ |
| 197 | { |
| 198 | "role": "user", |
| 199 | "content": "x", |
| 200 | "file_ids": ["string"], |
| 201 | "metadata": {}, |
| 202 | }, |
| 203 | { |
| 204 | "role": "user", |
| 205 | "content": "x", |
| 206 | "file_ids": ["string"], |
| 207 | "metadata": {}, |
| 208 | }, |
| 209 | { |
| 210 | "role": "user", |
| 211 | "content": "x", |
| 212 | "file_ids": ["string"], |
| 213 | "metadata": {}, |
| 214 | }, |
| 215 | ], |
| 216 | "metadata": {}, |
| 217 | }, |
| 218 | tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}], |
| 219 | ) |
| 220 | assert_matches_type(Run, thread, path=["response"]) |
| 221 | |
| 222 | @parametrize |
| 223 | def test_raw_response_create_and_run(self, client: OpenAI) -> None: |
| 224 | response = client.beta.threads.with_raw_response.create_and_run( |
| 225 | assistant_id="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(Run, thread, path=["response"]) |
| 232 | |
| 233 | @parametrize |
| 234 | def test_streaming_response_create_and_run(self, client: OpenAI) -> None: |
| 235 | with client.beta.threads.with_streaming_response.create_and_run( |
| 236 | assistant_id="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(Run, thread, path=["response"]) |
| 243 | |
| 244 | assert cast(Any, response.is_closed) is True |
| 245 | |
| 246 | |
| 247 | class TestAsyncThreads: |
| 248 | strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 249 | loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 250 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 251 | |
| 252 | @parametrize |
| 253 | async def test_method_create(self, client: AsyncOpenAI) -> None: |
| 254 | thread = await client.beta.threads.create() |
| 255 | assert_matches_type(Thread, thread, path=["response"]) |
| 256 | |
| 257 | @parametrize |
| 258 | async def test_method_create_with_all_params(self, client: AsyncOpenAI) -> None: |
| 259 | thread = await client.beta.threads.create( |
| 260 | messages=[ |
| 261 | { |
| 262 | "role": "user", |
| 263 | "content": "x", |
| 264 | "file_ids": ["string"], |
| 265 | "metadata": {}, |
| 266 | }, |
| 267 | { |
| 268 | "role": "user", |
| 269 | "content": "x", |
| 270 | "file_ids": ["string"], |
| 271 | "metadata": {}, |
| 272 | }, |
| 273 | { |
| 274 | "role": "user", |
| 275 | "content": "x", |
| 276 | "file_ids": ["string"], |
| 277 | "metadata": {}, |
| 278 | }, |
| 279 | ], |
| 280 | metadata={}, |
| 281 | ) |
| 282 | assert_matches_type(Thread, thread, path=["response"]) |
| 283 | |
| 284 | @parametrize |
| 285 | async def test_raw_response_create(self, client: AsyncOpenAI) -> None: |
| 286 | response = await client.beta.threads.with_raw_response.create() |
| 287 | |
| 288 | assert response.is_closed is True |
| 289 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 290 | thread = response.parse() |
| 291 | assert_matches_type(Thread, thread, path=["response"]) |
| 292 | |
| 293 | @parametrize |
| 294 | async def test_streaming_response_create(self, client: AsyncOpenAI) -> None: |
| 295 | async with client.beta.threads.with_streaming_response.create() as response: |
| 296 | assert not response.is_closed |
| 297 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 298 | |
| 299 | thread = await response.parse() |
| 300 | assert_matches_type(Thread, thread, path=["response"]) |
| 301 | |
| 302 | assert cast(Any, response.is_closed) is True |
| 303 | |
| 304 | @parametrize |
| 305 | async def test_method_retrieve(self, client: AsyncOpenAI) -> None: |
| 306 | thread = await client.beta.threads.retrieve( |
| 307 | "string", |
| 308 | ) |
| 309 | assert_matches_type(Thread, thread, path=["response"]) |
| 310 | |
| 311 | @parametrize |
| 312 | async def test_raw_response_retrieve(self, client: AsyncOpenAI) -> None: |
| 313 | response = await client.beta.threads.with_raw_response.retrieve( |
| 314 | "string", |
| 315 | ) |
| 316 | |
| 317 | assert response.is_closed is True |
| 318 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 319 | thread = response.parse() |
| 320 | assert_matches_type(Thread, thread, path=["response"]) |
| 321 | |
| 322 | @parametrize |
| 323 | async def test_streaming_response_retrieve(self, client: AsyncOpenAI) -> None: |
| 324 | async with client.beta.threads.with_streaming_response.retrieve( |
| 325 | "string", |
| 326 | ) as response: |
| 327 | assert not response.is_closed |
| 328 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 329 | |
| 330 | thread = await response.parse() |
| 331 | assert_matches_type(Thread, thread, path=["response"]) |
| 332 | |
| 333 | assert cast(Any, response.is_closed) is True |
| 334 | |
| 335 | @parametrize |
| 336 | async def test_method_update(self, client: AsyncOpenAI) -> None: |
| 337 | thread = await client.beta.threads.update( |
| 338 | "string", |
| 339 | ) |
| 340 | assert_matches_type(Thread, thread, path=["response"]) |
| 341 | |
| 342 | @parametrize |
| 343 | async def test_method_update_with_all_params(self, client: AsyncOpenAI) -> None: |
| 344 | thread = await client.beta.threads.update( |
| 345 | "string", |
| 346 | metadata={}, |
| 347 | ) |
| 348 | assert_matches_type(Thread, thread, path=["response"]) |
| 349 | |
| 350 | @parametrize |
| 351 | async def test_raw_response_update(self, client: AsyncOpenAI) -> None: |
| 352 | response = await client.beta.threads.with_raw_response.update( |
| 353 | "string", |
| 354 | ) |
| 355 | |
| 356 | assert response.is_closed is True |
| 357 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 358 | thread = response.parse() |
| 359 | assert_matches_type(Thread, thread, path=["response"]) |
| 360 | |
| 361 | @parametrize |
| 362 | async def test_streaming_response_update(self, client: AsyncOpenAI) -> None: |
| 363 | async with client.beta.threads.with_streaming_response.update( |
| 364 | "string", |
| 365 | ) as response: |
| 366 | assert not response.is_closed |
| 367 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 368 | |
| 369 | thread = await response.parse() |
| 370 | assert_matches_type(Thread, thread, path=["response"]) |
| 371 | |
| 372 | assert cast(Any, response.is_closed) is True |
| 373 | |
| 374 | @parametrize |
| 375 | async def test_method_delete(self, client: AsyncOpenAI) -> None: |
| 376 | thread = await client.beta.threads.delete( |
| 377 | "string", |
| 378 | ) |
| 379 | assert_matches_type(ThreadDeleted, thread, path=["response"]) |
| 380 | |
| 381 | @parametrize |
| 382 | async def test_raw_response_delete(self, client: AsyncOpenAI) -> None: |
| 383 | response = await client.beta.threads.with_raw_response.delete( |
| 384 | "string", |
| 385 | ) |
| 386 | |
| 387 | assert response.is_closed is True |
| 388 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 389 | thread = response.parse() |
| 390 | assert_matches_type(ThreadDeleted, thread, path=["response"]) |
| 391 | |
| 392 | @parametrize |
| 393 | async def test_streaming_response_delete(self, client: AsyncOpenAI) -> None: |
| 394 | async with client.beta.threads.with_streaming_response.delete( |
| 395 | "string", |
| 396 | ) as response: |
| 397 | assert not response.is_closed |
| 398 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 399 | |
| 400 | thread = await response.parse() |
| 401 | assert_matches_type(ThreadDeleted, thread, path=["response"]) |
| 402 | |
| 403 | assert cast(Any, response.is_closed) is True |
| 404 | |
| 405 | @parametrize |
| 406 | async def test_method_create_and_run(self, client: AsyncOpenAI) -> None: |
| 407 | thread = await client.beta.threads.create_and_run( |
| 408 | assistant_id="string", |
| 409 | ) |
| 410 | assert_matches_type(Run, thread, path=["response"]) |
| 411 | |
| 412 | @parametrize |
| 413 | async def test_method_create_and_run_with_all_params(self, client: AsyncOpenAI) -> None: |
| 414 | thread = await client.beta.threads.create_and_run( |
| 415 | assistant_id="string", |
| 416 | instructions="string", |
| 417 | metadata={}, |
| 418 | model="string", |
| 419 | thread={ |
| 420 | "messages": [ |
| 421 | { |
| 422 | "role": "user", |
| 423 | "content": "x", |
| 424 | "file_ids": ["string"], |
| 425 | "metadata": {}, |
| 426 | }, |
| 427 | { |
| 428 | "role": "user", |
| 429 | "content": "x", |
| 430 | "file_ids": ["string"], |
| 431 | "metadata": {}, |
| 432 | }, |
| 433 | { |
| 434 | "role": "user", |
| 435 | "content": "x", |
| 436 | "file_ids": ["string"], |
| 437 | "metadata": {}, |
| 438 | }, |
| 439 | ], |
| 440 | "metadata": {}, |
| 441 | }, |
| 442 | tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}], |
| 443 | ) |
| 444 | assert_matches_type(Run, thread, path=["response"]) |
| 445 | |
| 446 | @parametrize |
| 447 | async def test_raw_response_create_and_run(self, client: AsyncOpenAI) -> None: |
| 448 | response = await client.beta.threads.with_raw_response.create_and_run( |
| 449 | assistant_id="string", |
| 450 | ) |
| 451 | |
| 452 | assert response.is_closed is True |
| 453 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 454 | thread = response.parse() |
| 455 | assert_matches_type(Run, thread, path=["response"]) |
| 456 | |
| 457 | @parametrize |
| 458 | async def test_streaming_response_create_and_run(self, client: AsyncOpenAI) -> None: |
| 459 | async with client.beta.threads.with_streaming_response.create_and_run( |
| 460 | assistant_id="string", |
| 461 | ) as response: |
| 462 | assert not response.is_closed |
| 463 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 464 | |
| 465 | thread = await response.parse() |
| 466 | assert_matches_type(Run, thread, path=["response"]) |
| 467 | |
| 468 | assert cast(Any, response.is_closed) is True |
| 469 | |