openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/api_resources/chat/test_completions.py
515lines · 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.types.chat import ( |
| 14 | ChatCompletion, |
| 15 | ) |
| 16 | |
| 17 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 18 | |
| 19 | |
| 20 | class TestCompletions: |
| 21 | parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 22 | |
| 23 | @parametrize |
| 24 | def test_method_create_overload_1(self, client: OpenAI) -> None: |
| 25 | completion = client.chat.completions.create( |
| 26 | messages=[ |
| 27 | { |
| 28 | "content": "string", |
| 29 | "role": "developer", |
| 30 | } |
| 31 | ], |
| 32 | model="gpt-4o", |
| 33 | ) |
| 34 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 35 | |
| 36 | @parametrize |
| 37 | def test_method_create_with_all_params_overload_1(self, client: OpenAI) -> None: |
| 38 | completion = client.chat.completions.create( |
| 39 | messages=[ |
| 40 | { |
| 41 | "content": "string", |
| 42 | "role": "developer", |
| 43 | "name": "name", |
| 44 | } |
| 45 | ], |
| 46 | model="gpt-4o", |
| 47 | audio={ |
| 48 | "format": "wav", |
| 49 | "voice": "alloy", |
| 50 | }, |
| 51 | frequency_penalty=-2, |
| 52 | function_call="none", |
| 53 | functions=[ |
| 54 | { |
| 55 | "name": "name", |
| 56 | "description": "description", |
| 57 | "parameters": {"foo": "bar"}, |
| 58 | } |
| 59 | ], |
| 60 | logit_bias={"foo": 0}, |
| 61 | logprobs=True, |
| 62 | max_completion_tokens=0, |
| 63 | max_tokens=0, |
| 64 | metadata={"foo": "string"}, |
| 65 | modalities=["text"], |
| 66 | n=1, |
| 67 | parallel_tool_calls=True, |
| 68 | prediction={ |
| 69 | "content": "string", |
| 70 | "type": "content", |
| 71 | }, |
| 72 | presence_penalty=-2, |
| 73 | reasoning_effort="low", |
| 74 | response_format={"type": "text"}, |
| 75 | seed=0, |
| 76 | service_tier="auto", |
| 77 | stop="string", |
| 78 | store=True, |
| 79 | stream=False, |
| 80 | stream_options={"include_usage": True}, |
| 81 | temperature=1, |
| 82 | tool_choice="none", |
| 83 | tools=[ |
| 84 | { |
| 85 | "function": { |
| 86 | "name": "name", |
| 87 | "description": "description", |
| 88 | "parameters": {"foo": "bar"}, |
| 89 | "strict": True, |
| 90 | }, |
| 91 | "type": "function", |
| 92 | } |
| 93 | ], |
| 94 | top_logprobs=0, |
| 95 | top_p=1, |
| 96 | user="user-1234", |
| 97 | ) |
| 98 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 99 | |
| 100 | @parametrize |
| 101 | def test_raw_response_create_overload_1(self, client: OpenAI) -> None: |
| 102 | response = client.chat.completions.with_raw_response.create( |
| 103 | messages=[ |
| 104 | { |
| 105 | "content": "string", |
| 106 | "role": "developer", |
| 107 | } |
| 108 | ], |
| 109 | model="gpt-4o", |
| 110 | ) |
| 111 | |
| 112 | assert response.is_closed is True |
| 113 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 114 | completion = response.parse() |
| 115 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 116 | |
| 117 | @parametrize |
| 118 | def test_streaming_response_create_overload_1(self, client: OpenAI) -> None: |
| 119 | with client.chat.completions.with_streaming_response.create( |
| 120 | messages=[ |
| 121 | { |
| 122 | "content": "string", |
| 123 | "role": "developer", |
| 124 | } |
| 125 | ], |
| 126 | model="gpt-4o", |
| 127 | ) as response: |
| 128 | assert not response.is_closed |
| 129 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 130 | |
| 131 | completion = response.parse() |
| 132 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 133 | |
| 134 | assert cast(Any, response.is_closed) is True |
| 135 | |
| 136 | @parametrize |
| 137 | def test_method_create_overload_2(self, client: OpenAI) -> None: |
| 138 | completion_stream = client.chat.completions.create( |
| 139 | messages=[ |
| 140 | { |
| 141 | "content": "string", |
| 142 | "role": "developer", |
| 143 | } |
| 144 | ], |
| 145 | model="gpt-4o", |
| 146 | stream=True, |
| 147 | ) |
| 148 | completion_stream.response.close() |
| 149 | |
| 150 | @parametrize |
| 151 | def test_method_create_with_all_params_overload_2(self, client: OpenAI) -> None: |
| 152 | completion_stream = client.chat.completions.create( |
| 153 | messages=[ |
| 154 | { |
| 155 | "content": "string", |
| 156 | "role": "developer", |
| 157 | "name": "name", |
| 158 | } |
| 159 | ], |
| 160 | model="gpt-4o", |
| 161 | stream=True, |
| 162 | audio={ |
| 163 | "format": "wav", |
| 164 | "voice": "alloy", |
| 165 | }, |
| 166 | frequency_penalty=-2, |
| 167 | function_call="none", |
| 168 | functions=[ |
| 169 | { |
| 170 | "name": "name", |
| 171 | "description": "description", |
| 172 | "parameters": {"foo": "bar"}, |
| 173 | } |
| 174 | ], |
| 175 | logit_bias={"foo": 0}, |
| 176 | logprobs=True, |
| 177 | max_completion_tokens=0, |
| 178 | max_tokens=0, |
| 179 | metadata={"foo": "string"}, |
| 180 | modalities=["text"], |
| 181 | n=1, |
| 182 | parallel_tool_calls=True, |
| 183 | prediction={ |
| 184 | "content": "string", |
| 185 | "type": "content", |
| 186 | }, |
| 187 | presence_penalty=-2, |
| 188 | reasoning_effort="low", |
| 189 | response_format={"type": "text"}, |
| 190 | seed=0, |
| 191 | service_tier="auto", |
| 192 | stop="string", |
| 193 | store=True, |
| 194 | stream_options={"include_usage": True}, |
| 195 | temperature=1, |
| 196 | tool_choice="none", |
| 197 | tools=[ |
| 198 | { |
| 199 | "function": { |
| 200 | "name": "name", |
| 201 | "description": "description", |
| 202 | "parameters": {"foo": "bar"}, |
| 203 | "strict": True, |
| 204 | }, |
| 205 | "type": "function", |
| 206 | } |
| 207 | ], |
| 208 | top_logprobs=0, |
| 209 | top_p=1, |
| 210 | user="user-1234", |
| 211 | ) |
| 212 | completion_stream.response.close() |
| 213 | |
| 214 | @parametrize |
| 215 | def test_raw_response_create_overload_2(self, client: OpenAI) -> None: |
| 216 | response = client.chat.completions.with_raw_response.create( |
| 217 | messages=[ |
| 218 | { |
| 219 | "content": "string", |
| 220 | "role": "developer", |
| 221 | } |
| 222 | ], |
| 223 | model="gpt-4o", |
| 224 | stream=True, |
| 225 | ) |
| 226 | |
| 227 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 228 | stream = response.parse() |
| 229 | stream.close() |
| 230 | |
| 231 | @parametrize |
| 232 | def test_streaming_response_create_overload_2(self, client: OpenAI) -> None: |
| 233 | with client.chat.completions.with_streaming_response.create( |
| 234 | messages=[ |
| 235 | { |
| 236 | "content": "string", |
| 237 | "role": "developer", |
| 238 | } |
| 239 | ], |
| 240 | model="gpt-4o", |
| 241 | stream=True, |
| 242 | ) as response: |
| 243 | assert not response.is_closed |
| 244 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 245 | |
| 246 | stream = response.parse() |
| 247 | stream.close() |
| 248 | |
| 249 | assert cast(Any, response.is_closed) is True |
| 250 | |
| 251 | @parametrize |
| 252 | def test_method_create_disallows_pydantic(self, client: OpenAI) -> None: |
| 253 | class MyModel(pydantic.BaseModel): |
| 254 | a: str |
| 255 | |
| 256 | with pytest.raises(TypeError, match=r"You tried to pass a `BaseModel` class"): |
| 257 | client.chat.completions.create( |
| 258 | messages=[ |
| 259 | { |
| 260 | "content": "string", |
| 261 | "role": "system", |
| 262 | } |
| 263 | ], |
| 264 | model="gpt-4o", |
| 265 | response_format=cast(Any, MyModel), |
| 266 | ) |
| 267 | |
| 268 | |
| 269 | class TestAsyncCompletions: |
| 270 | parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 271 | |
| 272 | @parametrize |
| 273 | async def test_method_create_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 274 | completion = await async_client.chat.completions.create( |
| 275 | messages=[ |
| 276 | { |
| 277 | "content": "string", |
| 278 | "role": "developer", |
| 279 | } |
| 280 | ], |
| 281 | model="gpt-4o", |
| 282 | ) |
| 283 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 284 | |
| 285 | @parametrize |
| 286 | async def test_method_create_with_all_params_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 287 | completion = await async_client.chat.completions.create( |
| 288 | messages=[ |
| 289 | { |
| 290 | "content": "string", |
| 291 | "role": "developer", |
| 292 | "name": "name", |
| 293 | } |
| 294 | ], |
| 295 | model="gpt-4o", |
| 296 | audio={ |
| 297 | "format": "wav", |
| 298 | "voice": "alloy", |
| 299 | }, |
| 300 | frequency_penalty=-2, |
| 301 | function_call="none", |
| 302 | functions=[ |
| 303 | { |
| 304 | "name": "name", |
| 305 | "description": "description", |
| 306 | "parameters": {"foo": "bar"}, |
| 307 | } |
| 308 | ], |
| 309 | logit_bias={"foo": 0}, |
| 310 | logprobs=True, |
| 311 | max_completion_tokens=0, |
| 312 | max_tokens=0, |
| 313 | metadata={"foo": "string"}, |
| 314 | modalities=["text"], |
| 315 | n=1, |
| 316 | parallel_tool_calls=True, |
| 317 | prediction={ |
| 318 | "content": "string", |
| 319 | "type": "content", |
| 320 | }, |
| 321 | presence_penalty=-2, |
| 322 | reasoning_effort="low", |
| 323 | response_format={"type": "text"}, |
| 324 | seed=0, |
| 325 | service_tier="auto", |
| 326 | stop="string", |
| 327 | store=True, |
| 328 | stream=False, |
| 329 | stream_options={"include_usage": True}, |
| 330 | temperature=1, |
| 331 | tool_choice="none", |
| 332 | tools=[ |
| 333 | { |
| 334 | "function": { |
| 335 | "name": "name", |
| 336 | "description": "description", |
| 337 | "parameters": {"foo": "bar"}, |
| 338 | "strict": True, |
| 339 | }, |
| 340 | "type": "function", |
| 341 | } |
| 342 | ], |
| 343 | top_logprobs=0, |
| 344 | top_p=1, |
| 345 | user="user-1234", |
| 346 | ) |
| 347 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 348 | |
| 349 | @parametrize |
| 350 | async def test_raw_response_create_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 351 | response = await async_client.chat.completions.with_raw_response.create( |
| 352 | messages=[ |
| 353 | { |
| 354 | "content": "string", |
| 355 | "role": "developer", |
| 356 | } |
| 357 | ], |
| 358 | model="gpt-4o", |
| 359 | ) |
| 360 | |
| 361 | assert response.is_closed is True |
| 362 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 363 | completion = response.parse() |
| 364 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 365 | |
| 366 | @parametrize |
| 367 | async def test_streaming_response_create_overload_1(self, async_client: AsyncOpenAI) -> None: |
| 368 | async with async_client.chat.completions.with_streaming_response.create( |
| 369 | messages=[ |
| 370 | { |
| 371 | "content": "string", |
| 372 | "role": "developer", |
| 373 | } |
| 374 | ], |
| 375 | model="gpt-4o", |
| 376 | ) as response: |
| 377 | assert not response.is_closed |
| 378 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 379 | |
| 380 | completion = await response.parse() |
| 381 | assert_matches_type(ChatCompletion, completion, path=["response"]) |
| 382 | |
| 383 | assert cast(Any, response.is_closed) is True |
| 384 | |
| 385 | @parametrize |
| 386 | async def test_method_create_overload_2(self, async_client: AsyncOpenAI) -> None: |
| 387 | completion_stream = await async_client.chat.completions.create( |
| 388 | messages=[ |
| 389 | { |
| 390 | "content": "string", |
| 391 | "role": "developer", |
| 392 | } |
| 393 | ], |
| 394 | model="gpt-4o", |
| 395 | stream=True, |
| 396 | ) |
| 397 | await completion_stream.response.aclose() |
| 398 | |
| 399 | @parametrize |
| 400 | async def test_method_create_with_all_params_overload_2(self, async_client: AsyncOpenAI) -> None: |
| 401 | completion_stream = await async_client.chat.completions.create( |
| 402 | messages=[ |
| 403 | { |
| 404 | "content": "string", |
| 405 | "role": "developer", |
| 406 | "name": "name", |
| 407 | } |
| 408 | ], |
| 409 | model="gpt-4o", |
| 410 | stream=True, |
| 411 | audio={ |
| 412 | "format": "wav", |
| 413 | "voice": "alloy", |
| 414 | }, |
| 415 | frequency_penalty=-2, |
| 416 | function_call="none", |
| 417 | functions=[ |
| 418 | { |
| 419 | "name": "name", |
| 420 | "description": "description", |
| 421 | "parameters": {"foo": "bar"}, |
| 422 | } |
| 423 | ], |
| 424 | logit_bias={"foo": 0}, |
| 425 | logprobs=True, |
| 426 | max_completion_tokens=0, |
| 427 | max_tokens=0, |
| 428 | metadata={"foo": "string"}, |
| 429 | modalities=["text"], |
| 430 | n=1, |
| 431 | parallel_tool_calls=True, |
| 432 | prediction={ |
| 433 | "content": "string", |
| 434 | "type": "content", |
| 435 | }, |
| 436 | presence_penalty=-2, |
| 437 | reasoning_effort="low", |
| 438 | response_format={"type": "text"}, |
| 439 | seed=0, |
| 440 | service_tier="auto", |
| 441 | stop="string", |
| 442 | store=True, |
| 443 | stream_options={"include_usage": True}, |
| 444 | temperature=1, |
| 445 | tool_choice="none", |
| 446 | tools=[ |
| 447 | { |
| 448 | "function": { |
| 449 | "name": "name", |
| 450 | "description": "description", |
| 451 | "parameters": {"foo": "bar"}, |
| 452 | "strict": True, |
| 453 | }, |
| 454 | "type": "function", |
| 455 | } |
| 456 | ], |
| 457 | top_logprobs=0, |
| 458 | top_p=1, |
| 459 | user="user-1234", |
| 460 | ) |
| 461 | await completion_stream.response.aclose() |
| 462 | |
| 463 | @parametrize |
| 464 | async def test_raw_response_create_overload_2(self, async_client: AsyncOpenAI) -> None: |
| 465 | response = await async_client.chat.completions.with_raw_response.create( |
| 466 | messages=[ |
| 467 | { |
| 468 | "content": "string", |
| 469 | "role": "developer", |
| 470 | } |
| 471 | ], |
| 472 | model="gpt-4o", |
| 473 | stream=True, |
| 474 | ) |
| 475 | |
| 476 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 477 | stream = response.parse() |
| 478 | await stream.close() |
| 479 | |
| 480 | @parametrize |
| 481 | async def test_streaming_response_create_overload_2(self, async_client: AsyncOpenAI) -> None: |
| 482 | async with async_client.chat.completions.with_streaming_response.create( |
| 483 | messages=[ |
| 484 | { |
| 485 | "content": "string", |
| 486 | "role": "developer", |
| 487 | } |
| 488 | ], |
| 489 | model="gpt-4o", |
| 490 | stream=True, |
| 491 | ) as response: |
| 492 | assert not response.is_closed |
| 493 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 494 | |
| 495 | stream = await response.parse() |
| 496 | await stream.close() |
| 497 | |
| 498 | assert cast(Any, response.is_closed) is True |
| 499 | |
| 500 | @parametrize |
| 501 | async def test_method_create_disallows_pydantic(self, async_client: AsyncOpenAI) -> None: |
| 502 | class MyModel(pydantic.BaseModel): |
| 503 | a: str |
| 504 | |
| 505 | with pytest.raises(TypeError, match=r"You tried to pass a `BaseModel` class"): |
| 506 | await async_client.chat.completions.create( |
| 507 | messages=[ |
| 508 | { |
| 509 | "content": "string", |
| 510 | "role": "system", |
| 511 | } |
| 512 | ], |
| 513 | model="gpt-4o", |
| 514 | response_format=cast(Any, MyModel), |
| 515 | ) |
| 516 | |