[Bugfix][Frontend] Expose completion response schemas in OpenAPI (#60926)
Signed-off-by: Guan Luo <gluo@nvidia.com> Signed-off-by: sfeng33 <4florafeng@gmail.com> Co-authored-by: sfeng33 <4florafeng@gmail.com>
This commit is contained in:
@@ -12,10 +12,12 @@ from vllm.entrypoints.openai.chat_completion.protocol import (
|
||||
BatchChatCompletionRequest,
|
||||
ChatCompletionRequest,
|
||||
ChatCompletionResponse,
|
||||
ChatCompletionStreamResponse,
|
||||
)
|
||||
from vllm.entrypoints.openai.chat_completion.serving import OpenAIServingChat
|
||||
from vllm.entrypoints.serve.engine.protocol import ErrorResponse
|
||||
from vllm.entrypoints.serve.utils.api_utils import (
|
||||
json_or_sse_responses,
|
||||
load_aware_call,
|
||||
validate_json_request,
|
||||
with_cancellation,
|
||||
@@ -41,13 +43,16 @@ def batch_chat(request: Request) -> OpenAIServingChatBatch | None:
|
||||
@router.post(
|
||||
"/v1/chat/completions",
|
||||
dependencies=[Depends(validate_json_request)],
|
||||
responses={
|
||||
HTTPStatus.OK.value: {"content": {"text/event-stream": {}}},
|
||||
HTTPStatus.BAD_REQUEST.value: {"model": ErrorResponse},
|
||||
HTTPStatus.NOT_FOUND.value: {"model": ErrorResponse},
|
||||
HTTPStatus.INTERNAL_SERVER_ERROR.value: {"model": ErrorResponse},
|
||||
HTTPStatus.NOT_IMPLEMENTED.value: {"model": ErrorResponse},
|
||||
},
|
||||
**json_or_sse_responses(
|
||||
ChatCompletionResponse,
|
||||
ChatCompletionStreamResponse,
|
||||
{
|
||||
HTTPStatus.BAD_REQUEST.value: {"model": ErrorResponse},
|
||||
HTTPStatus.NOT_FOUND.value: {"model": ErrorResponse},
|
||||
HTTPStatus.INTERNAL_SERVER_ERROR.value: {"model": ErrorResponse},
|
||||
HTTPStatus.NOT_IMPLEMENTED.value: {"model": ErrorResponse},
|
||||
},
|
||||
),
|
||||
)
|
||||
@with_cancellation
|
||||
@load_aware_call
|
||||
|
||||
@@ -10,10 +10,12 @@ from fastapi.responses import JSONResponse, StreamingResponse
|
||||
from vllm.entrypoints.openai.completion.protocol import (
|
||||
CompletionRequest,
|
||||
CompletionResponse,
|
||||
CompletionStreamResponse,
|
||||
)
|
||||
from vllm.entrypoints.openai.completion.serving import OpenAIServingCompletion
|
||||
from vllm.entrypoints.serve.engine.protocol import ErrorResponse
|
||||
from vllm.entrypoints.serve.utils.api_utils import (
|
||||
json_or_sse_responses,
|
||||
load_aware_call,
|
||||
validate_json_request,
|
||||
with_cancellation,
|
||||
@@ -35,12 +37,15 @@ def completion(request: Request) -> OpenAIServingCompletion | None:
|
||||
@router.post(
|
||||
"/v1/completions",
|
||||
dependencies=[Depends(validate_json_request)],
|
||||
responses={
|
||||
HTTPStatus.OK.value: {"content": {"text/event-stream": {}}},
|
||||
HTTPStatus.BAD_REQUEST.value: {"model": ErrorResponse},
|
||||
HTTPStatus.NOT_FOUND.value: {"model": ErrorResponse},
|
||||
HTTPStatus.INTERNAL_SERVER_ERROR.value: {"model": ErrorResponse},
|
||||
},
|
||||
**json_or_sse_responses(
|
||||
CompletionResponse,
|
||||
CompletionStreamResponse,
|
||||
{
|
||||
HTTPStatus.BAD_REQUEST.value: {"model": ErrorResponse},
|
||||
HTTPStatus.NOT_FOUND.value: {"model": ErrorResponse},
|
||||
HTTPStatus.INTERNAL_SERVER_ERROR.value: {"model": ErrorResponse},
|
||||
},
|
||||
),
|
||||
)
|
||||
@with_cancellation
|
||||
@load_aware_call
|
||||
|
||||
@@ -6,13 +6,15 @@ import dataclasses
|
||||
import functools
|
||||
import os
|
||||
from argparse import Namespace
|
||||
from http import HTTPStatus
|
||||
from logging import Logger
|
||||
from string import Template
|
||||
from typing import Any
|
||||
|
||||
from fastapi import Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse, StreamingResponse
|
||||
from fastapi.responses import JSONResponse, Response, StreamingResponse
|
||||
from pydantic import BaseModel
|
||||
from starlette.background import BackgroundTask, BackgroundTasks
|
||||
|
||||
from vllm import envs
|
||||
@@ -88,11 +90,80 @@ def with_cancellation(handler_func):
|
||||
|
||||
if handler_task in done:
|
||||
return handler_task.result()
|
||||
return None
|
||||
# The client is gone; an empty response skips response_model validation.
|
||||
return Response()
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
_ERROR_REF = {"$ref": "#/components/schemas/ErrorResponse"}
|
||||
|
||||
|
||||
def _sse_event_schema(chunk_ref: dict[str, str]) -> dict[str, Any]:
|
||||
# One SSE event: `data` is a JSON chunk, a JSON error, or "[DONE]". This is
|
||||
# the envelope FastAPI emits for EventSourceResponse routes, but published
|
||||
# under `schema` because OpenAPI 3.1 has no `itemSchema`.
|
||||
return {
|
||||
"type": "object",
|
||||
"required": ["data"],
|
||||
"properties": {
|
||||
"data": {
|
||||
"type": "string",
|
||||
"anyOf": [
|
||||
{"const": "[DONE]"},
|
||||
{
|
||||
"contentMediaType": "application/json",
|
||||
"contentSchema": chunk_ref,
|
||||
},
|
||||
{
|
||||
"contentMediaType": "application/json",
|
||||
"contentSchema": _ERROR_REF,
|
||||
},
|
||||
],
|
||||
},
|
||||
"event": {"type": "string"},
|
||||
"id": {"type": "string"},
|
||||
"retry": {"type": "integer", "minimum": 0},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def json_or_sse_responses(
|
||||
unary_model: type[BaseModel],
|
||||
chunk_model: type[BaseModel],
|
||||
error_responses: dict[int, dict[str, Any]],
|
||||
) -> dict[str, Any]:
|
||||
"""Route kwargs for an endpoint answering with ``unary_model`` as JSON, or
|
||||
with SSE ``data`` events carrying ``chunk_model`` when ``stream=true``.
|
||||
|
||||
FastAPI only registers models reachable from ``response_model`` or an
|
||||
additional response's ``model``. The chunk model rides on the 200 entry,
|
||||
which also overwrites the JSON schema, so ``openapi_extra`` restores it.
|
||||
"""
|
||||
unary_ref = {"$ref": f"#/components/schemas/{unary_model.__name__}"}
|
||||
chunk_ref = {"$ref": f"#/components/schemas/{chunk_model.__name__}"}
|
||||
return {
|
||||
"response_model": unary_model,
|
||||
"responses": {
|
||||
HTTPStatus.OK.value: {
|
||||
"model": chunk_model,
|
||||
"description": (
|
||||
"JSON body when stream=false; SSE events when stream=true."
|
||||
),
|
||||
"content": {
|
||||
"text/event-stream": {"schema": _sse_event_schema(chunk_ref)}
|
||||
},
|
||||
},
|
||||
**error_responses,
|
||||
},
|
||||
"openapi_extra": {
|
||||
"responses": {
|
||||
"200": {"content": {"application/json": {"schema": unary_ref}}}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def decrement_server_load(request: Request):
|
||||
request.app.state.server_load_metrics -= 1
|
||||
|
||||
|
||||
Reference in New Issue
Block a user