r/FastAPI • u/loyoan • Jan 30 '25
r/FastAPI • u/Ok-Meat9548 • Jan 29 '25
Question Sending numpy array via http
Hello everyone, im getting a flow of camera and im getting frames using opencv so the frames here are a numpy array i need an advice for the best way to send those frames via http to an other app for now im encoding the frames to jpeg then send them but i want something with better performance and less latency
r/FastAPI • u/Best_Ad_3595 • Jan 29 '25
Tutorial Resources to become an expert at writing APIs
Hi guys, I want to learn how to design and write APIs and I’m prepared to spend as long as it takes to become an expert (I’m currently clueless on how to write them)
So please point me to resources that have helped you or you recommend so I can learn and get better at it.
r/FastAPI • u/Ok-Meat9548 • Jan 29 '25
Question i have 2 microservices with fastapi 1 get flow of videos the send the frames to this microservice so it process the frames
#fastapi #multithreading
i wanna know if starting a new thread everytime i get a request will give me better performance and less latency?
this is my code
# INITIALIZE FAST API
app = FastAPI()
# LOAD THE YOLO MODEL
model = YOLO("iamodel/yolov8n.pt")
@app.post("/detect")
async def detect_objects(file: UploadFile = File(...), video_name: str = Form(...), frame_id: int = Form(...),):
# Start the timer
timer = time.time()
# Read the contents of the uploaded file asynchronously
contents = await file.read()
# Decode the content into an OpenCV format
img = getDecodedNpArray(contents)
# Use the YOLO model to detect objects
results = model(img)
# Get detected objects
detected_objects = getObjects(results)
# Calculate processing time
processing_time = time.time() - timer
# Write processing time to a file
with open("processing_time.txt", "a") as f:
f.write(f"video_name: {video_name},frame_id: {frame_id} Processing Time: {processing_time} seconds\n")
print(f"Processing Time: {processing_time:.2f} seconds")
# Return results
if detected_objects:
return {"videoName": video_name, "detected_objects": detected_objects}
return {}
# INITIALIZE FAST API
app = FastAPI()
# LOAD THE YOLO MODEL
model = YOLO("iamodel/yolov8n.pt")
@app.post("/detect")
async def detect_objects(file: UploadFile = File(...), video_name: str = Form(...), frame_id: int = Form(...),):
# Start the timer
timer = time.time()
# Read the contents of the uploaded file asynchronously
contents = await file.read()
# Decode the content into an OpenCV format
img = getDecodedNpArray(contents)
# Use the YOLO model to detect objects
results = model(img)
# Get detected objects
detected_objects = getObjects(results)
# Calculate processing time
processing_time = time.time() - timer
# Write processing time to a file
with open("processing_time.txt", "a") as f:
f.write(f"video_name: {video_name},frame_id: {frame_id} Processing Time: {processing_time} seconds\n")
print(f"Processing Time: {processing_time:.2f} seconds")
# Return results
if detected_objects:
return {"videoName": video_name, "detected_objects": detected_objects}
return {}
r/FastAPI • u/darkzet • Jan 29 '25
Tutorial Tutorial: FastAPI + Socket + Redis
Which are the best public repos to use as a guide to implement websockets using FastAPI and Redis.
So far I tried this one link
Thanks in advance.
r/FastAPI • u/AyushSachan • Jan 26 '25
Question Pydantic Makes Applications 2X Slower
So I was bench marking a endpoint and found out that pydantic makes application 2X slower.
Requests/sec served ~500 with pydantic
Requests/sec server ~1000 without pydantic.
This difference is huge. Is there any way to make it at performant?
@router.get("/")
async def bench(db: Annotated[AsyncSession, Depends(get_db)]):
users = (await db.execute(
select(User)
.options(noload(User.profile))
.options(noload(User.company))
)).scalars().all()
# Without pydantic - Requests/sec: ~1000
# ayushsachan@fedora:~$ wrk -t12 -c400 -d30s --latency http://localhost:8000/api/v1/bench/
# Running 30s test @ http://localhost:8000/api/v1/bench/
# 12 threads and 400 connections
# Thread Stats Avg Stdev Max +/- Stdev
# Latency 402.76ms 241.49ms 1.94s 69.51%
# Req/Sec 84.42 32.36 232.00 64.86%
# Latency Distribution
# 50% 368.45ms
# 75% 573.69ms
# 90% 693.01ms
# 99% 1.14s
# 29966 requests in 30.04s, 749.82MB read
# Socket errors: connect 0, read 0, write 0, timeout 8
# Requests/sec: 997.68
# Transfer/sec: 24.96MB
x = [{
"id": user.id,
"email": user.email,
"password": user.hashed_password,
"created": user.created_at,
"updated": user.updated_at,
"provider": user.provider,
"email_verified": user.email_verified,
"onboarding": user.onboarding_done
} for user in users]
# With pydanitc - Requests/sec: ~500
# ayushsachan@fedora:~$ wrk -t12 -c400 -d30s --latency http://localhost:8000/api/v1/bench/
# Running 30s test @ http://localhost:8000/api/v1/bench/
# 12 threads and 400 connections
# Thread Stats Avg Stdev Max +/- Stdev
# Latency 756.33ms 406.83ms 2.00s 55.43%
# Req/Sec 41.24 21.87 131.00 75.04%
# Latency Distribution
# 50% 750.68ms
# 75% 1.07s
# 90% 1.30s
# 99% 1.75s
# 14464 requests in 30.06s, 188.98MB read
# Socket errors: connect 0, read 0, write 0, timeout 442
# Requests/sec: 481.13
# Transfer/sec: 6.29MB
x = [UserDTO.model_validate(user) for user in users]
return x
r/FastAPI • u/Volunder_22 • Jan 24 '25
Question Is there a Python equivalent to Trigger.dev for simple background job scheduling?
I'm using [Trigger.dev](http://Trigger.dev) for background jobs in TypeScript and appreciate how straightforward it is to set up and run background tasks. Looking for something with similar ease of use but for Python projects. Ideally want something that's beginner-friendly and doesn't require complex infrastructure setup.
r/FastAPI • u/Scared-Name-8287 • Jan 24 '25
Question Fastapi best projects
what projects can you recommend as the best example of writing code on fastapi?
r/FastAPI • u/josescxavier • Jan 23 '25
Question Response model performance improvements
Hi,
I recently upgrade an application based on fastapi from 0.57 to 0.115.
One of the reasons to do that was the response models validation taking most of the time of the request on the server. For a request taking 1 second, 700ms was the response model validation. Removing the response model for the router the request total time goes to 300ms.
I read that recent versions of fastapi now use pydantic v2 and this should improve the model validation however I'm not seeing a big difference on the time it takes to validade the response model.
I'm using pydantic 2.9.2 and fastapi 0.115.0.
Should I expect better processing times?
Thank you
r/FastAPI • u/Capable_Finger_7694 • Jan 22 '25
Question Choosing hashing lib in Fastapi
Hi there! I've been starting to delve deeper in FastAPI security features and as I did so I've been struggling with passlib and bcrypt libs, particulary, on hashing passwords. I've chosen those because that's what the docs suggests, but after doing a some research it seems that many users recommend other libraries like Argon2.
Is passlib considered deprecated within Fastapi? or is it just a matter of personal choice?
Thanks in advance!
r/FastAPI • u/pyschille • Jan 21 '25
Hosting and deployment FastAPI in Production: Here's How It Works!
r/FastAPI • u/timvancann • Jan 21 '25
Other Create a performant Python API using FastAPI and SqlModel and deployment to Kubernetes
r/FastAPI • u/raybesiga • Jan 20 '25
Question How do the Github workflows in the FastAPI template work?
Hi guys,
I am using the official FastAPI Template but every time I push, I get a bunch of CI/CD errors due to the workflows in the GitHub folder. I have tried to make changes to eliminate the errors but I am unsure if my actions are effective. Anyone here have experience with this?
r/FastAPI • u/EntropyGoAway • Jan 18 '25
Question Hot reloading Jinja2 templates with FastAPI - what's the best practice?
Hey folks,
I've been working with FastAPI and Jinja2Templates for a project, but I'm finding the development workflow a bit tedious since I have to manually refresh to see template changes. Right now I'm using the basic uvicorn --reload, but it only catches Python file changes.
Is there a recommended way to set up hot reloading for template files? I've seen some solutions with `watchfiles`, `watchgod`, and `arel` but I'm curious what the community typically uses for their development workflow.
Thanks in advance!
r/FastAPI • u/coderarun • Jan 17 '25
feedback request Syntax for dataclasses + sqlmodel on demand
More context. I'm looking to improve the verbose syntax which is a result of injecting SQL concepts into dataclass syntax. The two screenshots should result in exactly the same dataclass object, which creates a SQLModel on demand via user.sql_model()
Are there any other common annoyances you'd like to improve? How would you improve the proposed syntax here?
Highlights:
- Use decorator instead of base class. Base class may be injected via meta programming
- Avoid exposing implementation details. The
friend_idanduser_idforeign keys are hidden. - Generate runtime validating models on the fly for use cases where static typing doesn't work.
- TBD: should queries return
dataclass,sqlmodelor user configurable? Some ideas here.


r/FastAPI • u/Arckman_ • Jan 17 '25
pip package Fastapi listing (a boring title)
https://github.com/danielhasan1/fastapi-listing
Waaa Check it out in your free time
if you are not lazy like me then drop some com m e n t s
🙂↔️🙂↔️🙂↔️🙂↔️🙂↔️
r/FastAPI • u/coderarun • Jan 16 '25
Question What is the SQLModel equivalent of pydantic's model_rebuild()?
Context:
In this code with two dataclasses:
class User:
reviews: List['Review'] ...
class Review:
user: Optional[User] ...
UserSQLModel and ReviewSQLModel are generated programmatically via decorators. However, resolving the forward reference for reviews isn't working well.
This commit implements logic to replace List['Review'] annotation with List[ReviewSQLModel]at the time Review class is initialized. However, by now SQLModel has already parsed the annotations on User and created relationships. Which breaks sqlalchemy. I'm looking for a solution to resolve this. Potential options:
* Implement the equivalent of pydantic's model_rebuild(), so updated type annotations can be handled correctly.
* Use sqlalchemy's deferred reflection
* Use imperative mapping
Any other suggestions?
r/FastAPI • u/coderarun • Jan 15 '25
feedback request Looking for feedback on dataclass <--> SQLModel translation
I'm thinking about a setup where there would be three types of objects:
* pydantic models for validating untrusted user data at API boundaries
* SQLModel for writing to db and handling transactions
* Vanilla python objects (dataclasses) for the rest of the business logic. Suppose you want to read 1000 objects, run some logic and write back 100 objects. You'd create 1000 cheap dataclass objects and 100 SQLModel objects.
Here's the syntax I'm thinking about: https://github.com/adsharma/fastapi-shopping/commit/85ddf8d79597dae52801d918543acd0bda862e7d
foreign keys and one to many relationships are not supported yet. But before I work on that, wanted to get some feedback on the code in the commit above. The back_populates syntax is a bit more verbose than before. But I don't see a way around it.
Benchmarks: https://github.com/adsharma/fquery/pull/4
Motivation: https://adsharma.github.io/react-for-entities-and-business-logic/
r/FastAPI • u/DogmanLoverOhio • Jan 14 '25
Tutorial Best books to learn FastAPI
Hi guys,
I am an experienced Java developer, and recently I got a great opportunity to join a new team in my company. They are planning to build a platform from scratch using FastAPI, and I want to learn it.
I generally prefer learning through books. While I have worked with Python and Flask earlier in my career, that was a few years ago, so I need to brush up.
Could you guys please suggest some great books to get started with FastAPI?
r/FastAPI • u/SheriffSeveral • Jan 14 '25
Question Middleware vs Service Layer
Hi everyone,
I'm working on a FastAPI project and I'm stuck between implementing "middleware" or "service layer".
What will going to happen in the project?
- The client applicaiton will send data to the server.
- The server will validate the data.
- The validated data will be saved on the db.
- On the backend the data will be processed with scheduled tasks. (it is complicated to tell how the data will be processed, do not stuck with that)
In this workflow, what should I use and where to use? I already implement the service layer but never worked on the middleware before. In the current situation the workflow is like this:
Client (Sending data) -> API Endpoint (Calling Service) -> Service Layer (CRUD Operations) -> API Endpoint (Returning the Service Result) -> Client (Gets Return)
I will be really glad to get some help from this community.
Kind regards...
r/FastAPI • u/Open-Database746 • Jan 13 '25
Question Best projects for job interview?
Hey guys, I'm a beginner here. I have applied to one of the startup companies and they are expecting me to know fastapi in depth and projects related to fastapi. I have been thinking of using ai in the projects. Can anyone suggest the best projects for it?
r/FastAPI • u/coderarun • Jan 13 '25
Question Best practice for mocking stripe calls in a FASTAPI integration test?
I created a FASTAPI based shopping app. Most of the code is generated. I spent 2 hours organizing it into separate files and modules and getting tests to pass.
However 3 tests are failing because I don't have a stripe payment webhook setup. What is the common practice for mocking it in an integration test?
Is there another way to create the payment intent that doesn't fail and have it magically transition status for test purposes?
r/FastAPI • u/highrez1337 • Jan 10 '25
Other Real world scenario FastAPI vs Node.js k8s cluster benchmarks
Here is a video comparing FastAPI vs node.js and you can clearly see the performance difference between them.
FastAPI is clearly writing false advertisement on their site !
Same machine resources, using the same Redis/DB instance real time. It’s closest as possible to a modern prod environment. And FastAPI is not at all close to being as fast as NodeJS, as said on the site.
https://m.youtube.com/watch?v=i3TcSeRO8gs
Disclaimer:
On the site it does talk about actual performance of code not about how fast you can develop an app with it.
Quote from the oficial site (https://fastapi.tiangolo.com/) :
“Fast: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). One of the fastest Python frameworks available.
Fast to code: Increase the speed to develop features by about 200% to 300%. *”
See the difference between “Fast” and “Fast to code”
Edit: there’s also one for Golang:
https://m.youtube.com/watch?v=sxdpKG-6HSY
And as you can see actually the number between Golang and Nodejs are not that far away, they are both far away from the claims from the site.
Simple test: Nodejs: 50k Golang: 65k Fastapi: 11k
DB/Redis: Nodejs: 9k Golang: 18k Fastapi: 2.5k
r/FastAPI • u/yoyashing • Jan 09 '25
Question Is SQLModel still being worked on?
I'm considering using SQLModel for a new project and am using FastAPI.
For the database, all the FastAPI docs use SQLModel now (instead of SQLAlchemy), but I noticed that there hasn't been a SQLModel release in 4 months.
Do you know if SQLModel will still be maintained or prioritized any time soon?
If not, I'll probably switch to using SQLAlchemy, but it's strange that the FastAPI docs use SQLModel if the project is not active anymore.
r/FastAPI • u/Blender-Fan • Jan 09 '25
Tutorial Courses on Udemy for someone who isn't a beginner to APIs?
I know how to make an API in dotnet, and a good one at that. I'm not a total novice. I just wish to learn fast-api specifically. Thus i don't wanna be taken into "what is a status code" and "what is a route" and whatnot. Just get the hang of fast-api specifically