FastAPI public solve_dependencies API #12131
Replies: 4 comments
-
|
It would be really nice to have a place where to put your own dependency solver |
Beta Was this translation helpful? Give feedback.
-
|
@tiangolo sorry for ping, but you asked to create such discussion. Could you spend some of your attention here? |
Beta Was this translation helpful? Give feedback.
-
|
Hello! @YuriiMotov made me aware that the latest release had incompatible changes with FastStream and pointed me here. I came to read and try to understand the use case better, but I'm struggling to see what the benefit of having a FastAPI integration would be. 🤔 The FastAPI dependency system is super simple, and coupled with the HTTP request data, as you have noted. And I see you have your own internal
import fastapi
import faststream
def some_dependency() -> int:
return 1
FastAPIDep = Annotated[int, fastapi.Depends(some_dependency)]
FastStreamDep = Annotated[int, faststream.Depends(some_dependency)]
Or alternatively, you could also support: FastAPIDep = Annotated[int, fastapi.Depends(some_dependency), faststream.Depends(some_dependency)]The thing is, FastAPI dependency functions are normally defined in a way that is quite tightly coupled with FastAPI HTTP stuff, so I would imagine they wouldn't be able to just re-use the same dependency functions. And if they do, then those are probably very simple dependency functions (e.g. to get a DB session), and they can re-use the function with the new dependency as in the example above. I struggle to see the benefit of bending how FastAPI works to solve dependencies for something that is not related to FastAPI or HTTP in general, and that also has its own equivalent way to achieve the same. 🤔 Could you share some examples of why people need this? |
Beta Was this translation helpful? Give feedback.
-
|
@tiangolo Hi! Thank you for your feedback. It's really valuable and I agree with you that this solution should work. I have the same opinion as you, but not all users. About 50% of users use our integration instead of native FastAPI + FastStream applications together. The main advantage of this integration is the ability to seamlessly use FastAPI's serialization, DI and BackgroundTasks mechanisms in endpoints to consume messages from RabbitMQ/Kafka/etc. In addition, it adds an AsyncAPI documentation section for these endpoints to the main application. You can check all features in the doc - https://faststream.ag2.ai/latest/getting-started/integrations/fastapi/ Since the current FastAPI syntax encourages people to create dependencies separately using the Annotated For example, the Nest framework from NodeJS allows users to declare endpoints for any protocol - HTTP/AMQP/Kafka/MQTT/etc. - and the user loves it. I believe that this kind of integration with FastAPI can significantly enrich its ecosystem. Thus, it could be not just an "HTTP microframework", but the core of an entire ecosystem of ready-made modules where you can simply I want to apologize for being so intrusive earlier. I understand how difficult it is to maintain such a large framework. I admire your work and just cannot imagine how I could support my tool if overwhelmed by such flood of demands and requests. I do not want to be intrusive, but I am inspired by FastAPI and want its ecosystem to be richer. However, we need some coordination for this. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello there! @tiangolo as you asked in Discord, I'll try to describe my case here
I am a core maintainer of FastStream project. As one of the feature we provide - we have pretty close FastAPI integration (documentation)
In a few words: using such plugin you can register RMQ/Kafka/Redis/NATS/SQS/etc subscribers the same way with regular FastAPI HTTP endpoints
The main feature of this integration is full FastAPI serialization and Depends system support - you can use regular
fastapi.Depends/Path/Header/BackgroundTasks/etcoptions to serialize your incloming message and reuse already written FastAPI dependencies.It is a pretty simple and limited code, but we have to use FastAPI "private" methods like
get_dependent,solve_dependenciesand other.The main mechanism really simple. We register regular FastStream subscriber, but wrap the original function to FastAPI-compatible decorator before. So, it is working like this: FastStream subscriber -> FastAPI wrapper -> user' handler
As you can see, this wrapper is just a some kind of copy of original FastAPI endpoint function.
But, this function is private and too tied to HTTP logic. I think, it will be very helpful, if FastAPI has some public API for
get_dependantandrun_endpoint(incapsulatessolve_dependencies,run_endpoint_functionandserialize_responsefunctions) with stable signature. It will be helpful for me (and other future plugin developers) to reuse FastAPI serialization logic as separated functional, and for FastAPI itself to better organize code layers and responsibility as well. Also, I can help with such refactoring if this feature will be approved.Beta Was this translation helpful? Give feedback.
All reactions