Uvicorn server shutting down unexpectedly

I'm working with FastAPI framework, served by Uvicorn server. My application should run some time consuming numerical computation at a given endpoint (/run). For this I am using 'background_task'...

How to convert async generator stream into a file-like object in Python3?

So I made a webservice (based on starlette), with an endpoint that accepts a binary body. I want to feed this binary body to fastavro. Starlette doc says, I can access the raw data as a async...

relative URL paths when serving static files with FastAPI/Starlette

I have a simple FastAPI application that serves a file test.html in app/main.py like so: @app.get('/') def index(): return FileResponse('static/test.html') The directory structure is like...

FastAPI (starlette) get client real IP

I have an API on FastAPI and i need to get the client real IP address when he request my page. I'm ty to use starlette Request. But it returns my server IP, not client remote IP. My...

Why MongoDB's python motor client is much slower than pymongo when run with starlette?

I have a web application written with python and the async framework Starlette (https://www.starlette.io/), I need to connect to MongoDB, so I installed the async pymongo driver: motor...

How to get multiple form input field as dictionary in fastapi?

I have to submit a form like this to fastapi endpoint with 14 input fields, like bellow <FORM NAME='TESTFORM' ACTION='URL' METHOD='POST'> <input type='hidden' name='CURRENCY'...

Cookies not setting on starlette TestClient, with requests sent via Python Requests

The login/logout feature on FastAPI works in the browser, but I am trying to write unit tests for it. When my app sets the cookie, I can see the response DOES have the cookie sent. When I receive...

FastAPI middleware peeking into responses

I try to write a simple middleware for FastAPI peeking into response bodies. In this example I just log the body content: app = FastAPI() @app.middleware("http") async def log_request(request,...

FastAPI, return a File response with the output of a sql query

I'm using FastAPI and currently I return a csv which I read from SQL server with pandas. (pd.read_sql()) However the csv is quite big for the browser and I want to return it with a File...

Python - multiprocessing issue with AWS Lambda and exchangelib

I have an issue when using my code deployed on an AWS Lambda function using exchangelib library. My code works very well locally. What could be the reason ? From what I understand it looks to be...

Event Handler on FastAPI

I'm investigating the best web framework for my purposes. We'll develop several microservices, and we need to dispatch events on some microservices, that could be listened on other...

Why not getting 100% coverage on FastAPI with pytest-cov using starlettes TestClient

I simply do not understand the output from pytest-cov My test module is the following from starlette.testclient import TestClient def test_ping(test_app: TestClient): response =...

FastAPI and Pydantic RecursionError Causing Exception in ASGI application

Description I've seen similar issues about self-referencing Pydantic models causing RecursionError: maximum recursion depth exceeded in comparison but as far as I can tell there are no...

Get starlette request body in the middleware context

I have such middleware class RequestContext(BaseHTTPMiddleware): async def dispatch(self, request: Request, call_next: RequestResponseEndpoint): request_id =...

How to delete the file after a `return FileResponse(file_path)`

I'm using FastAPI to receive an image, process it and then return the image as a FileResponse. But the returned file is a temporary one that need to be deleted after the endpoint return...

Apple M1: install psycopg2 package Symbol not found: _PQbackendPID

pg_config venv ❯ which pg_config /Applications/Postgres.app/Contents/Versions/latest/bin/pg_config pip3 install -r requirements.txt requirements.txt venv ❯ cat requirements.txt -i...

Ratelimit in Fastapi

How to ratelimit API endpoint request in Fastapi application ? I need to ratelimit API call 5 request per second per user and exceeding that limit blocks that particular user for 60 seconds. In...

How to disable Starlette static files caching?

I am using the Starlette framework in a Python application with the static files configured as required by the documentation: routes = [ ... Mount('/static', app=StaticFiles(directory='static'),...

RuntimeError(f"Directory '{directory}' does not exist") RuntimeError: Directory 'app/static' does not exist

Getting error when i run server.py file File "C:\Users\nawin\AppData\Local\Programs\Python\Python38\lib\site-packages\starlette\staticfiles.py", line 57, in __init__ raise...

Raising Exception from ApiRouter does not run the exception handler, returns 500 internal server error instead. FAST API

I am raising a custom exception in a Fast API using an API Router added to a Fast API. I am defining the exception class and handler and adding them both using the following code. It was working...

AttributeError: 'Blog' object has no attribute 'items' - FastAPI

I am trying to update a single record in the database by using the PUT operation in FastAPI. But for some reason, I keep getting this error. All other operations work fine except this one. The...

FastAPI - Pydantic - Value Error Raises Internal Server Error

I am using FastAPI with Pydantic. My problem - I need to raise ValueError using Pydantic from fastapi import FastAPI from pydantic import BaseModel, validator from fastapi import Depends,...

heroku application error for fastapi (python)

I followed github/stackoverflow resources, watched youtube videos, read heroku official docs but in the end, I still getting error (network erro). Can someone help me what is the problem going on...

Problem with simpleeval installation:use _2to3 invalid

We are using poetry to upgrade packages and deploy to our servers but some issue is stopping us from deploying our work continuously to our servers.The code below is the stacktrack where our code...

FSM doesn't work when I run aiogram with fastapi

I have a problem with fsm using aiogram with Fastapi. I ran the code from aiogram_fsm_example, but changed the long-polling to the Fastapi implementation. Here's the code I've got: import...

Async SqlAlchemy with FastAPI: Getting single session for all requests

I have recently migrated a REST API coded with FastApi to the new SQLAlchemy 1.4+ Async Version. My app compiles correctly and the database seems to setup just fine. The problem appears when I...

FASTAPI custom middleware getting body of request inside

Been trying to get the BODY of a request using FASTAPI middleware but it seems i can only get request.headers but not the body. I am in need of the body in order to get a key that I will use to...

Getting DLL error when running tensorflow/keras program on python gpu 3.8

I am trying to run my simple AI program but I keep getting this DLL error: ImportError: Could not find the DLL(s) 'msvcp140_1.dll'. TensorFlow requires that these DLLs be installed in a...

Add packages from requirements.txt to Docker image to minimize cold start time on EC2?

When deploying a machine learning model on EC2 from a Docker image, the cold start time is high because the instance downloads the packages and files from requirements.txt even though the...

FastAPI Middleware: Performance issues when adding prometheus instrumentation package - increases latency of REST api

We are trying to decrease the latency of our BERT model prediction service that is deployed using FastAPI. The predictions are called through the /predict endpoint. We looked into the tracing and...