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