Skip to content

Commit d1c7396

Browse files
gcalmettesmfournioux
authored andcommitted
[Bugfix]: allow extra fields in requests to openai compatible server (vllm-project#10463)
Signed-off-by: Guillaume Calmettes <gcalmettes@scaleway.com> Signed-off-by: Maxime Fournioux <55544262+mfournioux@users.noreply.github.com>
1 parent bb51496 commit d1c7396

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

tests/entrypoints/openai/test_chat.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -899,19 +899,19 @@ async def test_response_format_json_schema(client: openai.AsyncOpenAI):
899899

900900

901901
@pytest.mark.asyncio
902-
async def test_extra_fields(client: openai.AsyncOpenAI):
903-
with pytest.raises(BadRequestError) as exc_info:
904-
await client.chat.completions.create(
905-
model=MODEL_NAME,
906-
messages=[{
907-
"role": "system",
908-
"content": "You are a helpful assistant.",
909-
"extra_field": "0",
910-
}], # type: ignore
911-
temperature=0,
912-
seed=0)
913-
914-
assert "extra_forbidden" in exc_info.value.message
902+
async def test_extra_fields_allowed(client: openai.AsyncOpenAI):
903+
resp = await client.chat.completions.create(
904+
model=MODEL_NAME,
905+
messages=[{
906+
"role": "user",
907+
"content": "what is 1+1?",
908+
"extra_field": "0",
909+
}], # type: ignore
910+
temperature=0,
911+
seed=0)
912+
913+
content = resp.choices[0].message.content
914+
assert content is not None
915915

916916

917917
@pytest.mark.asyncio

vllm/entrypoints/openai/protocol.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
from typing_extensions import Annotated
1010

1111
from vllm.entrypoints.chat_utils import ChatCompletionMessageParam
12+
from vllm.logger import init_logger
1213
from vllm.pooling_params import PoolingParams
1314
from vllm.sampling_params import (BeamSearchParams, GuidedDecodingParams,
1415
RequestOutputKind, SamplingParams)
1516
from vllm.sequence import Logprob
1617
from vllm.utils import random_uuid
1718

19+
logger = init_logger(__name__)
20+
1821
# torch is mocked during docs generation,
1922
# so we have to provide the values as literals
2023
_MOCK_LONG_INFO = Namespace(min=-9223372036854775808, max=9223372036854775807)
@@ -35,8 +38,19 @@
3538

3639

3740
class OpenAIBaseModel(BaseModel):
38-
# OpenAI API does not allow extra fields
39-
model_config = ConfigDict(extra="forbid")
41+
# OpenAI API does allow extra fields
42+
model_config = ConfigDict(extra="allow")
43+
44+
@model_validator(mode="before")
45+
@classmethod
46+
def __log_extra_fields__(cls, data):
47+
if isinstance(data, dict):
48+
extra_fields = data.keys() - cls.model_fields.keys()
49+
if extra_fields:
50+
logger.warning(
51+
"The following fields were present in the request "
52+
"but ignored: %s", extra_fields)
53+
return data
4054

4155

4256
class ErrorResponse(OpenAIBaseModel):

0 commit comments

Comments
 (0)