Skip to content

Commit 86c6f1f

Browse files
solaiusleseb
andauthored
fix: FastAPI built-in paths bypass custom routing (Docs) and update r… (meta-llama#1841)
## What does this PR do? This PR improves the server's request routing logic by ensuring built-in FastAPI paths such as `/docs`, `/redoc`, `/openapi.json`, `/favicon.ico`, and `/static` bypass the custom `TracingMiddleware`. This prevents unnecessary tracing logic for documentation and static file requests, ensuring better performance and cleaner logs. Additionally, it adds proper metadata (`title`, `description`, and `version`) to the FastAPI application initialization and updates the requirements document accordingly. [//]: # (Closes meta-llama#1822 ) --- ## Test Plan - Ran the server locally with `uvicorn` using the provided `run.yaml` config - Verified that: - FastAPI docs (`/docs`, `/redoc`) load correctly without triggering the custom tracing middleware - All other routes still go through the middleware and trace logic - Application metadata appears as expected in the OpenAPI docs To reproduce: 1. Start the server with `python server.py --template <template-name>` 2. Navigate to `/docs` and `/redoc` 3. Confirm that no extra trace headers are added for those routes 4. Confirm other API endpoints behave as expected and include `x-trace-id` in the response headers [//]: # (## Documentation) --- Froze the requirements file to include many of the other libraries that have been added in the past few releases to make install easier. --------- Co-authored-by: Sébastien Han <seb@redhat.com>
1 parent cf158f2 commit 86c6f1f

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

llama_stack/distribution/server/server.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,30 @@ class TracingMiddleware:
229229
def __init__(self, app, impls):
230230
self.app = app
231231
self.impls = impls
232+
# FastAPI built-in paths that should bypass custom routing
233+
self.fastapi_paths = ("/docs", "/redoc", "/openapi.json", "/favicon.ico", "/static")
232234

233235
async def __call__(self, scope, receive, send):
234236
if scope.get("type") == "lifespan":
235237
return await self.app(scope, receive, send)
236238

237239
path = scope.get("path", "")
240+
241+
# Check if the path is a FastAPI built-in path
242+
if path.startswith(self.fastapi_paths):
243+
# Pass through to FastAPI's built-in handlers
244+
logger.debug(f"Bypassing custom routing for FastAPI built-in path: {path}")
245+
return await self.app(scope, receive, send)
246+
238247
if not hasattr(self, "endpoint_impls"):
239248
self.endpoint_impls = initialize_endpoint_impls(self.impls)
240-
_, _, trace_path = find_matching_endpoint(scope.get("method", "GET"), path, self.endpoint_impls)
249+
250+
try:
251+
_, _, trace_path = find_matching_endpoint(scope.get("method", "GET"), path, self.endpoint_impls)
252+
except ValueError:
253+
# If no matching endpoint is found, pass through to FastAPI
254+
logger.debug(f"No matching endpoint found for path: {path}, falling back to FastAPI")
255+
return await self.app(scope, receive, send)
241256

242257
trace_context = await start_trace(trace_path, {"__location__": "server", "raw_path": path})
243258

@@ -388,7 +403,12 @@ def main(args: Optional[argparse.Namespace] = None):
388403
safe_config = redact_sensitive_fields(config.model_dump())
389404
logger.info(yaml.dump(safe_config, indent=2))
390405

391-
app = FastAPI(lifespan=lifespan)
406+
app = FastAPI(
407+
lifespan=lifespan,
408+
docs_url="/docs",
409+
redoc_url="/redoc",
410+
openapi_url="/openapi.json",
411+
)
392412
if not os.environ.get("LLAMA_STACK_DISABLE_VERSION_CHECK"):
393413
app.add_middleware(ClientVersionMiddleware)
394414

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy