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