fix: FastAPI built-in paths bypass custom routing (Docs) and update requirements document

This commit is contained in:
Peter Double 2025-03-30 11:12:35 -04:00
parent 3a2314dcef
commit de60fb571f
2 changed files with 83 additions and 15 deletions

View file

@ -229,15 +229,34 @@ class TracingMiddleware:
def __init__(self, app, impls):
self.app = app
self.impls = impls
# FastAPI built-in paths that should bypass custom routing
self.fastapi_paths = [
"/docs",
"/redoc",
"/openapi.json",
"/favicon.ico",
"/static"
]
async def __call__(self, scope, receive, send):
if scope.get("type") == "lifespan":
return await self.app(scope, receive, send)
path = scope.get("path", "")
# Check if the path is a FastAPI built-in path
if any(path.startswith(fastapi_path) for fastapi_path in self.fastapi_paths):
# Pass through to FastAPI's built-in handlers
return await self.app(scope, receive, send)
if not hasattr(self, "endpoint_impls"):
self.endpoint_impls = initialize_endpoint_impls(self.impls)
_, _, trace_path = find_matching_endpoint(scope.get("method", "GET"), path, self.endpoint_impls)
try:
_, _, trace_path = find_matching_endpoint(scope.get("method", "GET"), path, self.endpoint_impls)
except ValueError:
# If no matching endpoint is found, pass through to FastAPI
return await self.app(scope, receive, send)
trace_context = await start_trace(trace_path, {"__location__": "server", "raw_path": path})
@ -388,7 +407,15 @@ def main(args: Optional[argparse.Namespace] = None):
safe_config = redact_sensitive_fields(config.model_dump())
logger.info(yaml.dump(safe_config, indent=2))
app = FastAPI(lifespan=lifespan)
app = FastAPI(
lifespan=lifespan,
docs_url="/docs",
redoc_url="/redoc",
openapi_url="/openapi.json",
title="Llama Stack API",
description="API for Llama Stack",
version="0.1.9"
)
if not os.environ.get("LLAMA_STACK_DISABLE_VERSION_CHECK"):
app.add_middleware(ClientVersionMiddleware)
@ -499,4 +526,4 @@ def extract_path_params(route: str) -> List[str]:
if __name__ == "__main__":
main()
main()