openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/chat/test_completions.py
838lines · 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.chat import ( |
| 14 | ChatCompletion, |
| 15 | ChatCompletionDeleted, |
| 16 | ) |
| 17 | |
| 18 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 19 | |
| 20 | |
| 21 | class TestCompletions: |
| 22 | parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 23 | |
| 24 | @parametrize |
| 25 | def test_method_create_overload_1(self, client: OpenAI) -> None: |
| 26 | completion = client.chat.completions.create( |
| 27 | messages=[ |
| 28 | { |
| 29 | "content": "string", |
| 30 | "role": "developer", |
| 31 | } |
| 32 | ], |
| 33 | model="gpt-4o", |
| 34 | ) |
| 35 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 36 | |
| 37 | @parametrize |
| 38 | def test_method_create_with_all_params_overload_1(self, client: OpenAI) -> None: |
| 39 | completion = client.chat.completions.create( |
| 40 | messages=[ |
| 41 | { |
| 42 | "content": "string", |
| 43 | "role": "developer", |
| 44 | "name": "name", |
| 45 | } |
| 46 | ], |
| 47 | model="gpt-4o", |
| 48 | audio={ |
| 49 | "format": "wav", |
| 50 | "voice": "alloy", |
| 51 | }, |
| 52 | frequency_penalty=-2, |
| 53 | function_call="none", |
| 54 | functions=[ |
| 55 | { |
| 56 | "name": "name", |
| 57 | "description": "description", |
| 58 | "parameters": {"foo": "bar"}, |
| 59 | } |
| 60 | ], |
| 61 | logit_bias={"foo": 0}, |
| 62 | logprobs=True, |
| 63 | max_completion_tokens=0, |
| 64 | max_tokens=0, |
| 65 | metadata={"foo": "string"}, |
| 66 | modalities=["text"], |
| 67 | n=1, |
| 68 | parallel_tool_calls=True, |
| 69 | prediction={ |
| 70 | "content": "string", |
| 71 | "type": "content", |
| 72 | }, |
| 73 | presence_penalty=-2, |
| 74 | reasoning_effort="low", |
| 75 | response_format={"type": "text"}, |
| 76 | seed=-9007199254740991, |
| 77 | service_tier="auto", |
| 78 | stop="\n", |
| 79 | store=True, |
| 80 | stream=False, |
| 81 | stream_options={"include_usage": True}, |
| 82 | temperature=1, |
| 83 | tool_choice="none", |
| 84 | tools=[ |
| 85 | { |
| 86 | "function": { |
| 87 | "name": "name", |
| 88 | "description": "description", |
| 89 | "parameters": {"foo": "bar"}, |
| 90 | "strict": True, |
| 91 | }, |
| 92 | "type": "function", |
| 93 | } |
| 94 | ], |
| 95 | top_logprobs=0, |
| 96 | top_p=1, |
| 97 | user="user-1234", |
| 98 | web_search_options={ |
| 99 | "search_context_size": "low", |
| 100 | "user_location": { |
| 101 | "approximate": { |
| 102 | "city": "city", |
| 103 | "country": "country", |
| 104 | "region": "region", |
| 105 | "timezone": "timezone", |
| 106 | }, |
| 107 | "type": "approximate", |
| 108 | }, |
| 109 | }, |
| 110 | ) |
| 111 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 112 | |
| 113 | @parametrize |
| 114 | def test_raw_response_create_overload_1(self, client: OpenAI) -> None: |
| 115 | response = client.chat.completions.with_raw_response.create( |
| 116 | messages=[ |
| 117 | { |
| 118 | "content": "string", |
| 119 | "role": "developer", |
| 120 | } |
| 121 | ], |
| 122 | model="gpt-4o", |
| 123 | ) |
| 124 | |
| 125 | assert response.is_closed is True |
| 126 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 127 | completion = response.parse() |
| 128 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 129 | |
| 130 | @parametrize |
| 131 | def test_streaming_response_create_overload_1(self, client: OpenAI) -> None: |
| 132 | with client.chat.completions.with_streaming_response.create( |
| 133 | messages=[ |
| 134 | { |
| 135 | "content": "string", |
| 136 | "role": "developer", |
| 137 | } |
| 138 | ], |
| 139 | model="gpt-4o", |
| 140 | ) as response: |
| 141 | assert not response.is_closed |
| 142 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 143 | |
| 144 | completion = response.parse() |
| 145 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 146 | |
| 147 | assert cast(Any, response.is_closed) is True |
| 148 | |
| 149 | @parametrize |
| 150 | def test_method_create_overload_2(self, client: OpenAI) -> None: |
| 151 | completion_stream = client.chat.completions.create( |
| 152 | messages=[ |
| 153 | { |
| 154 | "content": "string", |
| 155 | "role": "developer", |
| 156 | } |
| 157 | ], |
| 158 | model="gpt-4o", |
| 159 | stream=True, |
| 160 | ) |
| 161 | completion_stream.response.close() |
| 162 | |
| 163 | @parametrize |
| 164 | def test_method_create_with_all_params_overload_2(self, client: OpenAI) -> None: |
| 165 | completion_stream = client.chat.completions.create( |
| 166 | messages=[ |
| 167 | { |
| 168 | "content": "string", |
| 169 | "role": "developer", |
| 170 | "name": "name", |
| 171 | } |
| 172 | ], |
| 173 | model="gpt-4o", |
| 174 | stream=True, |
| 175 | audio={ |
| 176 | "format": "wav", |
| 177 | "voice": "alloy", |
| 178 | }, |
| 179 | frequency_penalty=-2, |
| 180 | function_call="none", |
| 181 | functions=[ |
| 182 | { |
| 183 | "name": "name", |
| 184 | "description": "description", |
| 185 | "parameters": {"foo": "bar"}, |
| 186 | } |
| 187 | ], |
| 188 | logit_bias={"foo": 0}, |
| 189 | logprobs=True, |
| 190 | max_completion_tokens=0, |
| 191 | max_tokens=0, |
| 192 | metadata={"foo": "string"}, |
| 193 | modalities=["text"], |
| 194 | n=1, |
| 195 | parallel_tool_calls=True, |
| 196 | prediction={ |
| 197 | "content": "string", |
| 198 | "type": "content", |
| 199 | }, |
| 200 | presence_penalty=-2, |
| 201 | reasoning_effort="low", |
| 202 | response_format={"type": "text"}, |
| 203 | seed=-9007199254740991, |
| 204 | service_tier="auto", |
| 205 | stop="\n", |
| 206 | store=True, |
| 207 | stream_options={"include_usage": True}, |
| 208 | temperature=1, |
| 209 | tool_choice="none", |
| 210 | tools=[ |
| 211 | { |
| 212 | "function": { |
| 213 | "name": "name", |
| 214 | "description": "description", |
| 215 | "parameters": {"foo": "bar"}, |
| 216 | "strict": True, |
| 217 | }, |
| 218 | "type": "function", |
| 219 | } |
| 220 | ], |
| 221 | top_logprobs=0, |
| 222 | top_p=1, |
| 223 | user="user-1234", |
| 224 | web_search_options={ |
| 225 | "search_context_size": "low", |
| 226 | "user_location": { |
| 227 | "approximate": { |
| 228 | "city": "city", |
| 229 | "country": "country", |
| 230 | "region": "region", |
| 231 | "timezone": "timezone", |
| 232 | }, |
| 233 | "type": "approximate", |
| 234 | }, |
| 235 | }, |
| 236 | ) |
| 237 | completion_stream.response.close() |
| 238 | |
| 239 | @parametrize |
| 240 | def test_raw_response_create_overload_2(self, client: OpenAI) -> None: |
| 241 | response = client.chat.completions.with_raw_response.create( |
| 242 | messages=[ |
| 243 | { |
| 244 | "content": "string", |
| 245 | "role": "developer", |
| 246 | } |
| 247 | ], |
| 248 | model="gpt-4o", |
| 249 | stream=True, |
| 250 | ) |
| 251 | |
| 252 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 253 | stream = response.parse() |
| 254 | stream.close() |
| 255 | |
| 256 | @parametrize |
| 257 | def test_streaming_response_create_overload_2(self, client: OpenAI) -> None: |
| 258 | with client.chat.completions.with_streaming_response.create( |
| 259 | messages=[ |
| 260 | { |
| 261 | "content": "string", |
| 262 | "role": "developer", |
| 263 | } |
| 264 | ], |
| 265 | model="gpt-4o", |
| 266 | stream=True, |
| 267 | ) as response: |
| 268 | assert not response.is_closed |
| 269 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 270 | |
| 271 | stream = response.parse() |
| 272 | stream.close() |
| 273 | |
| 274 | assert cast(Any, response.is_closed) is True |
| 275 | |
| 276 | @parametrize |
| 277 | def test_method_retrieve(self, client: OpenAI) -> None: |
| 278 | completion = client.chat.completions.retrieve( |
| 279 | "completion_id", |
| 280 | ) |
| 281 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 282 | |
| 283 | @parametrize |
| 284 | def test_raw_response_retrieve(self, client: OpenAI) -> None: |
| 285 | response = client.chat.completions.with_raw_response.retrieve( |
| 286 | "completion_id", |
| 287 | ) |
| 288 | |
| 289 | assert response.is_closed is True |
| 290 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 291 | completion = response.parse() |
| 292 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 293 | |
| 294 | @parametrize |
| 295 | def test_streaming_response_retrieve(self, client: OpenAI) -> None: |
| 296 | with client.chat.completions.with_streaming_response.retrieve( |
| 297 | "completion_id", |
| 298 | ) as response: |
| 299 | assert not response.is_closed |
| 300 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 301 | |
| 302 | completion = response.parse() |
| 303 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 304 | |
| 305 | assert cast(Any, response.is_closed) is True |
| 306 | |
| 307 | @parametrize |
| 308 | def test_path_params_retrieve(self, client: OpenAI) -> None: |
| 309 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `completion_id` but received ''"): |
| 310 | client.chat.completions.with_raw_response.retrieve( |
| 311 | "", |
| 312 | ) |
| 313 | |
| 314 | @parametrize |
| 315 | def test_method_update(self, client: OpenAI) -> None: |
| 316 | completion = client.chat.completions.update( |
| 317 | completion_id="completion_id", |
| 318 | metadata={"foo": "string"}, |
| 319 | ) |
| 320 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 321 | |
| 322 | @parametrize |
| 323 | def test_raw_response_update(self, client: OpenAI) -> None: |
| 324 | response = client.chat.completions.with_raw_response.update( |
| 325 | completion_id="completion_id", |
| 326 | metadata={"foo": "string"}, |
| 327 | ) |
| 328 | |
| 329 | assert response.is_closed is True |
| 330 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 331 | completion = response.parse() |
| 332 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 333 | |
| 334 | @parametrize |
| 335 | def test_streaming_response_update(self, client: OpenAI) -> None: |
| 336 | with client.chat.completions.with_streaming_response.update( |
| 337 | completion_id="completion_id", |
| 338 | metadata={"foo": "string"}, |
| 339 | ) as response: |
| 340 | assert not response.is_closed |
| 341 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 342 | |
| 343 | completion = response.parse() |
| 344 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 345 | |
| 346 | assert cast(Any, response.is_closed) is True |
| 347 | |
| 348 | @parametrize |
| 349 | def test_path_params_update(self, client: OpenAI) -> None: |
| 350 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `completion_id` but received ''"): |
| 351 | client.chat.completions.with_raw_response.update( |
| 352 | completion_id="", |
| 353 | metadata={"foo": "string"}, |
| 354 | ) |
| 355 | |
| 356 | @parametrize |
| 357 | def test_method_list(self, client: OpenAI) -> None: |
| 358 | completion = client.chat.completions.list() |
| 359 | assert_matches_type(SyncCursorPage[ChatCompletion], completion, path=["response"]) |
| 360 | |
| 361 | @parametrize |
| 362 | def test_method_list_with_all_params(self, client: OpenAI) -> None: |
| 363 | completion = client.chat.completions.list( |
| 364 | after="after", |
| 365 | limit=0, |
| 366 | metadata={"foo": "string"}, |
| 367 | model="model", |
| 368 | order="asc", |
| 369 | ) |
| 370 | assert_matches_type(SyncCursorPage[ChatCompletion], completion, path=["response"]) |
| 371 | |
| 372 | @parametrize |
| 373 | def test_raw_response_list(self, client: OpenAI) -> None: |
| 374 | response = client.chat.completions.with_raw_response.list() |
| 375 | |
| 376 | assert response.is_closed is True |
| 377 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 378 | completion = response.parse() |
| 379 | assert_matches_type(SyncCursorPage[ChatCompletion], completion, path=["response"]) |
| 380 | |
| 381 | @parametrize |
| 382 | def test_streaming_response_list(self, client: OpenAI) -> None: |
| 383 | with client.chat.completions.with_streaming_response.list() as response: |
| 384 | assert not response.is_closed |
| 385 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 386 | |
| 387 | completion = response.parse() |
| 388 | assert_matches_type(SyncCursorPage[ChatCompletion], completion, path=["response"]) |
| 389 | |
| 390 | assert cast(Any, response.is_closed) is True |
| 391 | |
| 392 | @parametrize |
| 393 | def test_method_delete(self, client: OpenAI) -> None: |
| 394 | completion = client.chat.completions.delete( |
| 395 | "completion_id", |
| 396 | ) |
| 397 | assert_matches_type(ChatCompletionDeleted, completion, path=["response"]) |
| 398 | |
| 399 | @parametrize |
| 400 | def test_raw_response_delete(self, client: OpenAI) -> None: |
| 401 | response = client.chat.completions.with_raw_response.delete( |
| 402 | "completion_id", |
| 403 | ) |
| 404 | |
| 405 | assert response.is_closed is True |
| 406 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 407 | completion = response.parse() |
| 408 | assert_matches_type(ChatCompletionDeleted, completion, path=["response"]) |
| 409 | |
| 410 | @parametrize |
| 411 | def test_streaming_response_delete(self, client: OpenAI) -> None: |
| 412 | with client.chat.completions.with_streaming_response.delete( |
| 413 | "completion_id", |
| 414 | ) as response: |
| 415 | assert not response.is_closed |
| 416 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 417 | |
| 418 | completion = response.parse() |
| 419 | assert_matches_type(ChatCompletionDeleted, completion, path=["response"]) |
| 420 | |
| 421 | assert cast(Any, response.is_closed) is True |
| 422 | |
| 423 | @parametrize |
| 424 | def test_path_params_delete(self, client: OpenAI) -> None: |
| 425 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `completion_id` but received ''"): |
| 426 | client.chat.completions.with_raw_response.delete( |
| 427 | "", |
| 428 | ) |
| 429 | |
| 430 | |
| 431 | class TestAsyncCompletions: |
| 432 | parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 433 | |
| 434 | @parametrize |
| 435 | async def test_method_create_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 436 | completion = await async_client.chat.completions.create( |
| 437 | messages=[ |
| 438 | { |
| 439 | "content": "string", |
| 440 | "role": "developer", |
| 441 | } |
| 442 | ], |
| 443 | model="gpt-4o", |
| 444 | ) |
| 445 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 446 | |
| 447 | @parametrize |
| 448 | async def test_method_create_with_all_params_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 449 | completion = await async_client.chat.completions.create( |
| 450 | messages=[ |
| 451 | { |
| 452 | "content": "string", |
| 453 | "role": "developer", |
| 454 | "name": "name", |
| 455 | } |
| 456 | ], |
| 457 | model="gpt-4o", |
| 458 | audio={ |
| 459 | "format": "wav", |
| 460 | "voice": "alloy", |
| 461 | }, |
| 462 | frequency_penalty=-2, |
| 463 | function_call="none", |
| 464 | functions=[ |
| 465 | { |
| 466 | "name": "name", |
| 467 | "description": "description", |
| 468 | "parameters": {"foo": "bar"}, |
| 469 | } |
| 470 | ], |
| 471 | logit_bias={"foo": 0}, |
| 472 | logprobs=True, |
| 473 | max_completion_tokens=0, |
| 474 | max_tokens=0, |
| 475 | metadata={"foo": "string"}, |
| 476 | modalities=["text"], |
| 477 | n=1, |
| 478 | parallel_tool_calls=True, |
| 479 | prediction={ |
| 480 | "content": "string", |
| 481 | "type": "content", |
| 482 | }, |
| 483 | presence_penalty=-2, |
| 484 | reasoning_effort="low", |
| 485 | response_format={"type": "text"}, |
| 486 | seed=-9007199254740991, |
| 487 | service_tier="auto", |
| 488 | stop="\n", |
| 489 | store=True, |
| 490 | stream=False, |
| 491 | stream_options={"include_usage": True}, |
| 492 | temperature=1, |
| 493 | tool_choice="none", |
| 494 | tools=[ |
| 495 | { |
| 496 | "function": { |
| 497 | "name": "name", |
| 498 | "description": "description", |
| 499 | "parameters": {"foo": "bar"}, |
| 500 | "strict": True, |
| 501 | }, |
| 502 | "type": "function", |
| 503 | } |
| 504 | ], |
| 505 | top_logprobs=0, |
| 506 | top_p=1, |
| 507 | user="user-1234", |
| 508 | web_search_options={ |
| 509 | "search_context_size": "low", |
| 510 | "user_location": { |
| 511 | "approximate": { |
| 512 | "city": "city", |
| 513 | "country": "country", |
| 514 | "region": "region", |
| 515 | "timezone": "timezone", |
| 516 | }, |
| 517 | "type": "approximate", |
| 518 | }, |
| 519 | }, |
| 520 | ) |
| 521 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 522 | |
| 523 | @parametrize |
| 524 | async def test_raw_response_create_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 525 | response = await async_client.chat.completions.with_raw_response.create( |
| 526 | messages=[ |
| 527 | { |
| 528 | "content": "string", |
| 529 | "role": "developer", |
| 530 | } |
| 531 | ], |
| 532 | model="gpt-4o", |
| 533 | ) |
| 534 | |
| 535 | assert response.is_closed is True |
| 536 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 537 | completion = response.parse() |
| 538 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 539 | |
| 540 | @parametrize |
| 541 | async def test_streaming_response_create_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 542 | async with async_client.chat.completions.with_streaming_response.create( |
| 543 | messages=[ |
| 544 | { |
| 545 | "content": "string", |
| 546 | "role": "developer", |
| 547 | } |
| 548 | ], |
| 549 | model="gpt-4o", |
| 550 | ) as response: |
| 551 | assert not response.is_closed |
| 552 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 553 | |
| 554 | completion = await response.parse() |
| 555 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 556 | |
| 557 | assert cast(Any, response.is_closed) is True |
| 558 | |
| 559 | @parametrize |
| 560 | async def test_method_create_overload_2(self, async_client: AsyncOpenAI) -> None: |
| 561 | completion_stream = await async_client.chat.completions.create( |
| 562 | messages=[ |
| 563 | { |
| 564 | "content": "string", |
| 565 | "role": "developer", |
| 566 | } |
| 567 | ], |
| 568 | model="gpt-4o", |
| 569 | stream=True, |
| 570 | ) |
| 571 | await completion_stream.response.aclose() |
| 572 | |
| 573 | @parametrize |
| 574 | async def test_method_create_with_all_params_overload_2(self, async_client: AsyncOpenAI) -> None: |
| 575 | completion_stream = await async_client.chat.completions.create( |
| 576 | messages=[ |
| 577 | { |
| 578 | "content": "string", |
| 579 | "role": "developer", |
| 580 | "name": "name", |
| 581 | } |
| 582 | ], |
| 583 | model="gpt-4o", |
| 584 | stream=True, |
| 585 | audio={ |
| 586 | "format": "wav", |
| 587 | "voice": "alloy", |
| 588 | }, |
| 589 | frequency_penalty=-2, |
| 590 | function_call="none", |
| 591 | functions=[ |
| 592 | { |
| 593 | "name": "name", |
| 594 | "description": "description", |
| 595 | "parameters": {"foo": "bar"}, |
| 596 | } |
| 597 | ], |
| 598 | logit_bias={"foo": 0}, |
| 599 | logprobs=True, |
| 600 | max_completion_tokens=0, |
| 601 | max_tokens=0, |
| 602 | metadata={"foo": "string"}, |
| 603 | modalities=["text"], |
| 604 | n=1, |
| 605 | parallel_tool_calls=True, |
| 606 | prediction={ |
| 607 | "content": "string", |
| 608 | "type": "content", |
| 609 | }, |
| 610 | presence_penalty=-2, |
| 611 | reasoning_effort="low", |
| 612 | response_format={"type": "text"}, |
| 613 | seed=-9007199254740991, |
| 614 | service_tier="auto", |
| 615 | stop="\n", |
| 616 | store=True, |
| 617 | stream_options={"include_usage": True}, |
| 618 | temperature=1, |
| 619 | tool_choice="none", |
| 620 | tools=[ |
| 621 | { |
| 622 | "function": { |
| 623 | "name": "name", |
| 624 | "description": "description", |
| 625 | "parameters": {"foo": "bar"}, |
| 626 | "strict": True, |
| 627 | }, |
| 628 | "type": "function", |
| 629 | } |
| 630 | ], |
| 631 | top_logprobs=0, |
| 632 | top_p=1, |
| 633 | user="user-1234", |
| 634 | web_search_options={ |
| 635 | "search_context_size": "low", |
| 636 | "user_location": { |
| 637 | "approximate": { |
| 638 | "city": "city", |
| 639 | "country": "country", |
| 640 | "region": "region", |
| 641 | "timezone": "timezone", |
| 642 | }, |
| 643 | "type": "approximate", |
| 644 | }, |
| 645 | }, |
| 646 | ) |
| 647 | await completion_stream.response.aclose() |
| 648 | |
| 649 | @parametrize |
| 650 | async def test_raw_response_create_overload_2(self, async_client: AsyncOpenAI) -> None: |
| 651 | response = await async_client.chat.completions.with_raw_response.create( |
| 652 | messages=[ |
| 653 | { |
| 654 | "content": "string", |
| 655 | "role": "developer", |
| 656 | } |
| 657 | ], |
| 658 | model="gpt-4o", |
| 659 | stream=True, |
| 660 | ) |
| 661 | |
| 662 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 663 | stream = response.parse() |
| 664 | await stream.close() |
| 665 | |
| 666 | @parametrize |
| 667 | async def test_streaming_response_create_overload_2(self, async_client: AsyncOpenAI) -> None: |
| 668 | async with async_client.chat.completions.with_streaming_response.create( |
| 669 | messages=[ |
| 670 | { |
| 671 | "content": "string", |
| 672 | "role": "developer", |
| 673 | } |
| 674 | ], |
| 675 | model="gpt-4o", |
| 676 | stream=True, |
| 677 | ) as response: |
| 678 | assert not response.is_closed |
| 679 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 680 | |
| 681 | stream = await response.parse() |
| 682 | await stream.close() |
| 683 | |
| 684 | assert cast(Any, response.is_closed) is True |
| 685 | |
| 686 | @parametrize |
| 687 | async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 688 | completion = await async_client.chat.completions.retrieve( |
| 689 | "completion_id", |
| 690 | ) |
| 691 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 692 | |
| 693 | @parametrize |
| 694 | async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 695 | response = await async_client.chat.completions.with_raw_response.retrieve( |
| 696 | "completion_id", |
| 697 | ) |
| 698 | |
| 699 | assert response.is_closed is True |
| 700 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 701 | completion = response.parse() |
| 702 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 703 | |
| 704 | @parametrize |
| 705 | async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 706 | async with async_client.chat.completions.with_streaming_response.retrieve( |
| 707 | "completion_id", |
| 708 | ) as response: |
| 709 | assert not response.is_closed |
| 710 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 711 | |
| 712 | completion = await response.parse() |
| 713 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 714 | |
| 715 | assert cast(Any, response.is_closed) is True |
| 716 | |
| 717 | @parametrize |
| 718 | async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 719 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `completion_id` but received ''"): |
| 720 | await async_client.chat.completions.with_raw_response.retrieve( |
| 721 | "", |
| 722 | ) |
| 723 | |
| 724 | @parametrize |
| 725 | async def test_method_update(self, async_client: AsyncOpenAI) -> None: |
| 726 | completion = await async_client.chat.completions.update( |
| 727 | completion_id="completion_id", |
| 728 | metadata={"foo": "string"}, |
| 729 | ) |
| 730 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 731 | |
| 732 | @parametrize |
| 733 | async def test_raw_response_update(self, async_client: AsyncOpenAI) -> None: |
| 734 | response = await async_client.chat.completions.with_raw_response.update( |
| 735 | completion_id="completion_id", |
| 736 | metadata={"foo": "string"}, |
| 737 | ) |
| 738 | |
| 739 | assert response.is_closed is True |
| 740 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 741 | completion = response.parse() |
| 742 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 743 | |
| 744 | @parametrize |
| 745 | async def test_streaming_response_update(self, async_client: AsyncOpenAI) -> None: |
| 746 | async with async_client.chat.completions.with_streaming_response.update( |
| 747 | completion_id="completion_id", |
| 748 | metadata={"foo": "string"}, |
| 749 | ) as response: |
| 750 | assert not response.is_closed |
| 751 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 752 | |
| 753 | completion = await response.parse() |
| 754 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 755 | |
| 756 | assert cast(Any, response.is_closed) is True |
| 757 | |
| 758 | @parametrize |
| 759 | async def test_path_params_update(self, async_client: AsyncOpenAI) -> None: |
| 760 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `completion_id` but received ''"): |
| 761 | await async_client.chat.completions.with_raw_response.update( |
| 762 | completion_id="", |
| 763 | metadata={"foo": "string"}, |
| 764 | ) |
| 765 | |
| 766 | @parametrize |
| 767 | async def test_method_list(self, async_client: AsyncOpenAI) -> None: |
| 768 | completion = await async_client.chat.completions.list() |
| 769 | assert_matches_type(AsyncCursorPage[ChatCompletion], completion, path=["response"]) |
| 770 | |
| 771 | @parametrize |
| 772 | async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 773 | completion = await async_client.chat.completions.list( |
| 774 | after="after", |
| 775 | limit=0, |
| 776 | metadata={"foo": "string"}, |
| 777 | model="model", |
| 778 | order="asc", |
| 779 | ) |
| 780 | assert_matches_type(AsyncCursorPage[ChatCompletion], completion, path=["response"]) |
| 781 | |
| 782 | @parametrize |
| 783 | async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None: |
| 784 | response = await async_client.chat.completions.with_raw_response.list() |
| 785 | |
| 786 | assert response.is_closed is True |
| 787 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 788 | completion = response.parse() |
| 789 | assert_matches_type(AsyncCursorPage[ChatCompletion], completion, path=["response"]) |
| 790 | |
| 791 | @parametrize |
| 792 | async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None: |
| 793 | async with async_client.chat.completions.with_streaming_response.list() as response: |
| 794 | assert not response.is_closed |
| 795 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 796 | |
| 797 | completion = await response.parse() |
| 798 | assert_matches_type(AsyncCursorPage[ChatCompletion], completion, path=["response"]) |
| 799 | |
| 800 | assert cast(Any, response.is_closed) is True |
| 801 | |
| 802 | @parametrize |
| 803 | async def test_method_delete(self, async_client: AsyncOpenAI) -> None: |
| 804 | completion = await async_client.chat.completions.delete( |
| 805 | "completion_id", |
| 806 | ) |
| 807 | assert_matches_type(ChatCompletionDeleted, completion, path=["response"]) |
| 808 | |
| 809 | @parametrize |
| 810 | async def test_raw_response_delete(self, async_client: AsyncOpenAI) -> None: |
| 811 | response = await async_client.chat.completions.with_raw_response.delete( |
| 812 | "completion_id", |
| 813 | ) |
| 814 | |
| 815 | assert response.is_closed is True |
| 816 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 817 | completion = response.parse() |
| 818 | assert_matches_type(ChatCompletionDeleted, completion, path=["response"]) |
| 819 | |
| 820 | @parametrize |
| 821 | async def test_streaming_response_delete(self, async_client: AsyncOpenAI) -> None: |
| 822 | async with async_client.chat.completions.with_streaming_response.delete( |
| 823 | "completion_id", |
| 824 | ) as response: |
| 825 | assert not response.is_closed |
| 826 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 827 | |
| 828 | completion = await response.parse() |
| 829 | assert_matches_type(ChatCompletionDeleted, completion, path=["response"]) |
| 830 | |
| 831 | assert cast(Any, response.is_closed) is True |
| 832 | |
| 833 | @parametrize |
| 834 | async def test_path_params_delete(self, async_client: AsyncOpenAI) -> None: |
| 835 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `completion_id` but received ''"): |
| 836 | await async_client.chat.completions.with_raw_response.delete( |
| 837 | "", |
| 838 | ) |
| 839 | |