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