fix: adopt FastAPI directly in llama-stack-api

This commit migrates the Batches API to use FastAPI routers directly in the
API package, removing the need for custom decorator systems and manual router
registration. The API package now defines FastAPI routers using standard
FastAPI route decorators, making it self-sufficient and eliminating dependencies
on the server package.

The router implementation has been moved from llama_stack/core/server/routers/batches.py
to llama_stack_api/batches/routes.py, where it belongs alongside the protocol
and models.

Standard error responses (standard_responses) have been moved from the server
package to llama_stack_api/router_utils.py, ensuring the API package can
define complete routers without server dependencies. FastAPI has been added
as an explicit dependency to the llama-stack-api package, making it an
intentional dependency rather than an implicit one.

Router discovery is now fully automatic. The server discovers routers by
checking for routes modules in each API package and looking for a create_router
function. This eliminates the need for manual registration and makes the system
scalable - new APIs with router modules are automatically discovered and used.

The router registry has been simplified to use automatic discovery instead of
maintaining a manual registry. The build_router function (renamed from
create_router to better reflect its purpose) discovers and combines router
factories with implementations to create the final router instances.

Exposing Routers from the API is nice for the Bring Your Own API use
case too.

Signed-off-by: Sébastien Han <seb@redhat.com>
This commit is contained in:
Sébastien Han 2025-11-20 15:00:11 +01:00
parent 2fe24a6df8
commit 00e7ea6c3b
No known key found for this signature in database
10 changed files with 54 additions and 70 deletions

View file

@ -76,14 +76,8 @@ def create_llama_stack_app() -> FastAPI:
],
)
# Import batches router to trigger router registration
try:
from llama_stack.core.server.routers import batches # noqa: F401
except ImportError:
pass
# Include routers for APIs that have them registered
from llama_stack.core.server.router_registry import create_router, has_router
# Include routers for APIs that have them (automatic discovery)
from llama_stack.core.server.router_registry import build_router, has_router
def dummy_impl_getter(api: Api) -> Any:
"""Dummy implementation getter for OpenAPI generation."""
@ -95,7 +89,7 @@ def create_llama_stack_app() -> FastAPI:
protocols = api_protocol_map()
for api in protocols.keys():
if has_router(api):
router = create_router(api, dummy_impl_getter)
router = build_router(api, dummy_impl_getter)
if router:
app.include_router(router)