# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import warnings from argparse import Namespace from fastapi import FastAPI from vllm.config import ModelConfig from vllm.entrypoints.serve.exception_handling.register import init_exception_handler from vllm.entrypoints.serve.middleware.register import init_entrypoints_middleware from vllm.plugins.endpoint_plugins.interface import attach_endpoint_plugins from vllm.tasks import FALLBACK_SUPPORTED_TASKS, SupportedTask from .api_server.routers import register_api_routers from .utils.server_utils import lifespan def build_app( args: Namespace, supported_tasks: tuple["SupportedTask", ...] | None = None, model_config: ModelConfig | None = None, ) -> FastAPI: if supported_tasks is None: warnings.warn( "The 'supported_tasks' parameter was not provided to " "build_app and will be required in a future version. " "Defaulting to ('generate',).", DeprecationWarning, stacklevel=2, ) supported_tasks = FALLBACK_SUPPORTED_TASKS if args.disable_fastapi_docs: app = FastAPI( openapi_url=None, docs_url=None, redoc_url=None, lifespan=lifespan ) else: app = FastAPI(docs_url=None, redoc_url=None, lifespan=lifespan) app.state.args = args app.root_path = args.root_path register_api_routers(args, app, supported_tasks, model_config) # Endpoint plugin hooks run after core routers so plugins can inspect them. attach_endpoint_plugins(app, supported_tasks) init_exception_handler(app) init_entrypoints_middleware(args, app, supported_tasks) return app