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