fix json schema alias serializing when streaming #26356
Open
+98 −2
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Purpose
Fix a serialization bug in streaming responses where Pydantic field aliases (e.g.
schema
→schema_
) were not preserved during.model_dump()
calls.This caused the
"schema_"
key to appear instead of"schema"
in streamed response events for JSON schema output formats, breaking compatibility with the OpenAI SDK’sResponseFormatTextJSONSchemaConfig
parsing.Related issue: vllm-project/vllm#26288
Root Cause
ResponsesResponse.from_request(...).model_dump()
was called withoutby_alias=True
at:vllm/entrypoints/openai/serving_responses.py:1830
vllm/entrypoints/openai/serving_responses.py:1879
by_alias=True
, Pydantic outputs internal field names (e.g.schema_
) instead of their aliases (schema
), causing validation errors downstream.Fix
Add
by_alias=True
to both.model_dump()
calls so serialized responses use the correct alias names consistent with OpenAI schema expectations.and
Test Plan
Setup
vllm==0.11.0
openai==1.108.0
Reproduce the Bug (before fix)
Observe the first streamed event includes
"schema_"
instead of"schema"
.Apply the Fix
by_alias=True
in both.model_dump()
calls.Expected Behavior
"schema"
key.Test Result
✅ Before fix
"schema_"
"schema"
field✅ After fix
"schema"
Example (after fix):
Fix is effective, without this fix following errors persist
[1;36m(APIServer pid=7)�[0;0m \| response.text.format.ResponseFormatTextJSONSchemaConfig.schema [1;36m(APIServer pid=7)�[0;0m \| Field required [type=missing, input_value={'name': 'schema_ner', 's...': None, 'strict': True}, input_type=dict]
Essential Elements of an Effective PR Description Checklist
BEFORE SUBMITTING: see vLLM contributing guide