Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json

import openai
import openai.types.responses as openai_responses_types
import pytest
from pydantic import BaseModel

Expand Down Expand Up @@ -50,6 +51,62 @@ async def test_structured_output(client: openai.AsyncOpenAI):
assert participants[1] == "Bob"


@pytest.mark.asyncio
async def test_structured_output_streaming(client: openai.AsyncOpenAI):
"""Test streaming with json_schema format.

Regression test: ResponseCreatedEvent previously failed validation when
json_schema format was used because .model_dump() serialized the response
with Python attribute names (schema_) instead of JSON aliases (schema).
"""
stream = await client.responses.create(
input=[
{"role": "system", "content": "Extract the event information."},
{
"role": "user",
"content": "Alice and Bob are going to a science fair on Friday.",
},
],
text={
"format": {
"type": "json_schema",
"name": "calendar_event",
"schema": {
"type": "object",
"properties": {
"event_name": {"type": "string"},
"date": {"type": "string"},
"participants": {
"type": "array",
"items": {"type": "string"},
},
},
"required": ["event_name", "date", "participants"],
"additionalProperties": False,
},
"description": "A calendar event.",
"strict": True,
}
},
stream=True,
)
events = [event async for event in stream]
assert isinstance(events[0], openai_responses_types.ResponseCreatedEvent)
assert any(
isinstance(event, openai_responses_types.ResponseTextDeltaEvent)
for event in events
)
assert isinstance(events[-1], openai_responses_types.ResponseCompletedEvent)

# Verify the completed response has valid structured output.
completed_event = events[-1]
output_text = completed_event.response.output[-1].content[0].text
event = json.loads(output_text)
assert "event_name" in event
assert "date" in event
assert "participants" in event


@pytest.mark.asyncio
async def test_structured_output_with_parse(client: openai.AsyncOpenAI):
class CalendarEvent(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion vllm/entrypoints/openai/responses/serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -2490,7 +2490,7 @@ def _increment_sequence_number_and_return(
output=[],
status="in_progress",
usage=None,
).model_dump()
)
yield _increment_sequence_number_and_return(
ResponseCreatedEvent(
type="response.created",
Expand Down