-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: Implement FastAPI router system #4191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
This commit introduces a new FastAPI router-based system for defining API endpoints, enabling a migration path away from the legacy @webmethod decorator system. The implementation includes router infrastructure, migration of the Batches API as the first example, and updates to server, OpenAPI generation, and inspection systems to support both routing approaches. The router infrastructure consists of a router registry system that allows APIs to register FastAPI router factories, which are then automatically discovered and included in the server application. Standard error responses are centralized in router_utils to ensure consistent OpenAPI specification generation with proper $ref references to component responses. The Batches API has been migrated to demonstrate the new pattern. The protocol definition and models remain in llama_stack_api/batches, maintaining clear separation between API contracts and server implementation. The FastAPI router implementation lives in llama_stack/core/server/routers/batches, following the established pattern where API contracts are defined in llama_stack_api and server routing logic lives in llama_stack/core/server. The server now checks for registered routers before falling back to the legacy webmethod-based route discovery, ensuring backward compatibility during the migration period. The OpenAPI generator has been updated to handle both router-based and webmethod-based routes, correctly extracting metadata from FastAPI route decorators and Pydantic Field descriptions. The inspect endpoint now includes routes from both systems, with proper filtering for deprecated routes and API levels. Response descriptions are now explicitly defined in router decorators, ensuring the generated OpenAPI specification matches the previous format. Error responses use $ref references to component responses (BadRequest400, TooManyRequests429, etc.) as required by the specification. This is neat and will allow us to remove a lot of boiler plate code from our generator once the migration is done. This implementation provides a foundation for incrementally migrating other APIs to the router system while maintaining full backward compatibility with existing webmethod-based APIs. Closes: llamastack#4188 Signed-off-by: Sébastien Han <[email protected]>
|
Thanks for writing this in a way which provides for incremental migration! |
Signed-off-by: Sébastien Han <[email protected]>
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 <[email protected]>
This commit refactors the Batches protocol to use Pydantic request models for both create_batch and list_batches methods, improving consistency, readability, and maintainability. - create_batch now accepts a single CreateBatchRequest parameter instead of individual arguments. This aligns the protocol with FastAPI’s request model pattern, allowing the router to pass the request object directly without unpacking parameters. Provider implementations now access fields via request.input_file_id, request.endpoint, etc. - list_batches now accepts a single ListBatchesRequest parameter, replacing individual query parameters. The model includes after and limit fields with proper OpenAPI descriptions. FastAPI automatically parses query parameters into the model for GET requests, keeping router code clean. Provider implementations access fields via request.after and request.limit. Signed-off-by: Sébastien Han <[email protected]>
Signed-off-by: Sébastien Han <[email protected]>
Signed-off-by: Sébastien Han <[email protected]>
Signed-off-by: Sébastien Han <[email protected]>
cdoern
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initial look, comment on the router+fastapi dependencies of llama_stack_api and a question about imports of these new packages.
Signed-off-by: Sébastien Han <[email protected]>
For clarity Signed-off-by: Sébastien Han <[email protected]>
Refactored the router to accept the implementation directly instead of using the impl_getter pattern. The caller already knows which API it's building a router for.for Signed-off-by: Sébastien Han <[email protected]>
We already have an impl at this point, no need to validate this again. Signed-off-by: Sébastien Han <[email protected]>
Signed-off-by: Sébastien Han <[email protected]>
All batch models are now exported from the top level for better discoverability and IDE support. Signed-off-by: Sébastien Han <[email protected]>
cdoern
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one comment about a slightly more strict check for our API version but it is a nit. otherwise LGTM!
Replace dynamic import-based router discovery with an explicit hardcoded list of APIs that have routers. Signed-off-by: Sébastien Han <[email protected]>
Less indirection and clearer declarations. Signed-off-by: Sébastien Han <[email protected]>
addressed https://github.com/llamastack/llama-stack/pull/4191/files#r2554273774 Signed-off-by: Sébastien Han <[email protected]>
|
Not exactly sure why the stainless github action is failing, but we got alerted on it and will investigate |
…eliminate duplication Added create_query_dependency() and create_path_dependency() helpers that automatically generate FastAPI dependency functions from Pydantic models. This makes the models the single source of truth for field types, descriptions, and defaults, eliminating duplication between models.py and fastapi_routes.py. Signed-off-by: Sébastien Han <[email protected]>
Signed-off-by: Sébastien Han <[email protected]>
✱ Stainless preview buildsThis PR will update the Edit this comment to update it. It will appear in the SDK's changelogs. ✅ llama-stack-client-node studio · code · diff
|
|
I think Stainless struggles when the PR is not up to date with main. Now it's happy/ |
cdoern
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for cleaning it up and addressing reviews. seems that the tracing.py stuff was removed so that clears up my only comment really.
I really like the dependency function generation!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow, this is pretty clean, I like this improvement!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed, it is rather sweet!
Ah ok, I arrived to the same conclusion and was going to post about it. Thanks, we will need to handle the situation more gracefully |
| } | ||
|
|
||
|
|
||
| def build_router(api: "Api", impl: Any) -> APIRouter | None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: build_fastapi_router at least for now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
|
||
| from fastapi import APIRouter | ||
|
|
||
| if TYPE_CHECKING: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this? why not just have a clear dependency here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hum I don't remember, probably a leftover.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # Add new APIs here as they are migrated to the router system | ||
| from llama_stack_api.batches.fastapi_routes import create_router as create_batches_router | ||
|
|
||
| _ROUTER_FACTORIES: dict[str, Any] = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe just dict[Api, Callable[Any, ApiRouter]]?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
|
||
| from typing import Protocol, runtime_checkable | ||
|
|
||
| try: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this specialized error message here?! if we did this, we'd need to this for every dependency we need? :) I suspect your LLM needs some instructions about defensiveness.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this code was already there, I just moved it.
| @@ -0,0 +1,75 @@ | |||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not a fan of keeping this in the init.py -- why can't we move it to batches/api.py or batches/batches.py?
secondly, why do we want to split fastapi_routes.py (i.e., the annotations) from the API signature? I guess it is cleaner but now there is boilerplate and we need to keep these two things in sync!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a tradeoff, request/response types are already shared only the signatures are duplicated. We still need the protocol to be correctly exported for impls. We could use fastapi as a source of truth and generate the protocol from it, the other way around seems much more complex. However, it's more common to use the protocol as the reference since it could be used to write other implementation than fastapi. However since we only do fastapi it might make sense to consider the routes as source of truth.
However, doing this introduces a generator which might grow in complexity over time 🤷🏻 . Also, we not only need to generate the batches.py file but edit the __init__.py too in order to add the import eg from llama_stack_api.batches.batches import Batches. I'm not really keen on editing the init also... And since it's an API should rely on __all__ and not do import *.
|
|
||
| from llama_stack_api.schema_utils import json_schema_type | ||
|
|
||
| try: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same thing about imports
src/llama_stack/core/inspect.py
Outdated
| for route in router.routes: | ||
| # Extract HTTP methods from the route | ||
| # FastAPI routes have methods as a set | ||
| if hasattr(route, "methods") and route.methods: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this code block so complex and has lots of FastAPI thingies when this branch is not even taken for FastAPI routers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need this to expose the routes when getting http://localhost:8321/v1/inspect/routes, i'll simply the code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/llama_stack/core/inspect.py
Outdated
| return not route_deprecated | ||
|
|
||
| # Process router-based routes | ||
| from llama_stack.core.resolver import api_protocol_map |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the import inline?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no good reason, will move up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| router = build_router(api, impl) | ||
| if router: | ||
| app.include_router(router) | ||
| logger.debug(f"Registered router for {api} API") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: maybe say "Registered FastAPI router"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ashwinb
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Getting there...
It's FastAPI Signed-off-by: Sébastien Han <[email protected]>
Signed-off-by: Sébastien Han <[email protected]>
llamastack#4191 (comment) Signed-off-by: Sébastien Han <[email protected]>
llamastack#4191 (comment) Signed-off-by: Sébastien Han <[email protected]>
What does this PR do?
This commit introduces a new FastAPI router-based system for defining API endpoints, enabling a migration path away from the legacy @webmethod decorator system. The implementation includes router infrastructure, migration of the Batches API as the first example, and updates to server, OpenAPI generation, and inspection systems to support both routing approaches.
The router infrastructure consists of a router registry system that allows APIs to register FastAPI router factories, which are then automatically discovered and included in the server application. Standard error responses are centralized in router_utils to ensure consistent OpenAPI specification generation with proper $ref references to component responses.
The Batches API has been migrated to demonstrate the new pattern. The protocol definition and models remain in llama_stack_api/batches, maintaining clear separation between API contracts and server implementation. The FastAPI router implementation lives in
llama_stack/core/server/routers/batches, following the established pattern where API contracts are defined in llama_stack_api and server routing logic lives in
llama_stack/core/server.
The server now checks for registered routers before falling back to the legacy webmethod-based route discovery, ensuring backward compatibility during the migration period. The OpenAPI generator has been updated to handle both router-based and webmethod-based routes, correctly extracting metadata from FastAPI route decorators and Pydantic Field descriptions. The inspect endpoint now includes routes from both systems, with proper filtering for deprecated routes and API levels.
Response descriptions are now explicitly defined in router decorators, ensuring the generated OpenAPI specification matches the previous format. Error responses use $ref references to component responses (BadRequest400, TooManyRequests429, etc.) as required by the specification. This is neat and will allow us to remove a lot of boiler plate code from our generator once the migration is done.
This implementation provides a foundation for incrementally migrating other APIs to the router system while maintaining full backward compatibility with existing webmethod-based APIs.
Closes: #4188
Test Plan
CI, the server should start, same routes should be visible.
Also: