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