pythonfastapistarlette

FastAPI raises "RecursionError: maximum recursion depth exceeded while calling a Python object"


I am trying to build an application which stores some users and their sales records. There is a register_user API which sends the user data to the db.

However, when I trigger the API in localhost:8000/docs after running python -m main:app, I get RecursionError while I dont have any recursion calls in the implementation.

The code in main.py is as follows:

from fastapi import FastAPI, Depends, Request
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import sessionmaker, Session
from sqlalchemy.ext.declarative import declarative_base
from passlib.context import CryptContext


SQLALCHEMY_DB_URL = "sqlite:///./records.db"

engine = create_engine(
    SQLALCHEMY_DB_URL, connect_args = {"check_same_thread": False}
)

SessionLocal = sessionmaker(autocommit=False, 
                            autoflush=False,
                            bind=engine)

Base = declarative_base()


class Users(Base):
    __tablename__ = 'users'

    user_id = Column(Integer, primary_key=True, index=True)
    business_name = Column(String, nullable=False)
    phone_number = Column(String, nullable=False)
    email = Column(String, nullable=False)
    hashed_password = Column(String, nullable=False)


Base.metadata.create_all(bind=engine)


app = FastAPI()

bcrypt_context = CryptContext(schemes=['bcrypt'], deprecated='auto')



def get_password_hash(password: str):
    return bcrypt_context.hash(password)

def get_db():
    try:
        db = SessionLocal()
        yield db
    finally:
        db.close()

@app.post('/register') 
async def register_user(request:Request,
                        email: str, 
                        business_name: str,
                        phone_number: str,
                        password: str,
                        password2: str,
                        db: Session = Depends(get_db)
                        ):
    try:
        validation1 = db.query(Users) \
                        .filter(Users.phone_number == phone_number) \
                        .first()
        
        validation2 = db.query(Users) \
                        .filter(Users.email == email) \
                        .first()

        if password != password2 or validation1 is not None or validation2 is not None:
            msg = "Invalid Registration Request"
            return {"request": request, 
                "msg": msg}
        
        user_model = Users()
        user_model.email = email
        user_model.phone_number = phone_number
        user_model.business_name = business_name
        user_model.hashed_password = get_password_hash(password=password)

        db.add(user_model)
        db.commit()

        msg = "User Successfully Created"

        return {"request": request, 
            "msg": msg}

    except Exception as e:

        print(f"Error occurred: {e}")
        msg = "Internal Server Error"
        return {"request": request, "msg": msg}

The code should have committed the new user to the db, however the code just fails with some errors which I am not able to decode.

The traceback is as follows. The first few lines:

INFO:     127.0.0.1:52561 - "POST /auth/register?email=email%40email.com&business_name=business&phone_number=1234567890&password=pass123&password2=pass123 HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "C:\Users\<User>\sales_register\env\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 398, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  File "C:\Users\<User>\sales_register\env\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 70, in __call__
    return await self.app(scope, receive, send)
  File "C:\Users\<User>\sales_register\env\lib\site-packages\fastapi\applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "C:\Users\<User>\sales_register\env\lib\site-packages\starlette\applications.py", line 123, in __call__
    await self.middleware_stack(scope, receive, send)
  File "C:\Users\<User>\sales_register\env\lib\site-packages\starlette\middleware\errors.py", line 186, in __call__
    raise exc
  File "C:\Users\<User>\sales_register\env\lib\site-packages\starlette\middleware\errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "C:\Users\<User>\sales_register\env\lib\site-packages\starlette\middleware\exceptions.py", line 65, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "C:\Users\<User>\sales_register\env\lib\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "C:\Users\<User>\sales_register\env\lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "C:\Users\<User>\sales_register\env\lib\site-packages\starlette\routing.py", line 756, in __call__
    await self.middleware_stack(scope, receive, send)
  File "C:\Users\<User>\sales_register\env\lib\site-packages\starlette\routing.py", line 776, in app
    await route.handle(scope, receive, send)
  File "C:\Users\<User>\sales_register\env\lib\site-packages\starlette\routing.py", line 297, in handle
    await self.app(scope, receive, send)
  File "C:\Users\<User>\sales_register\env\lib\site-packages\starlette\routing.py", line 77, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  File "C:\Users\<User>\sales_register\env\lib\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "C:\Users\<User>\sales_register\env\lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "C:\Users\<User>\sales_register\env\lib\site-packages\starlette\routing.py", line 72, in app
    response = await func(request)
  File "C:\Users\<User>\sales_register\env\lib\site-packages\fastapi\routing.py", line 296, in app
    content = await serialize_response(
  File "C:\Users\<User>\sales_register\env\lib\site-packages\fastapi\routing.py", line 180, in serialize_response
    return jsonable_encoder(response_content)
  File "C:\Users\<User>\sales_register\env\lib\site-packages\fastapi\encoders.py", line 289, in jsonable_encoder
    encoded_value = jsonable_encoder(
  File "C:\Users\<User>\sales_register\env\lib\site-packages\fastapi\encoders.py", line 333, in jsonable_encoder
    return jsonable_encoder(
  File "C:\Users\<User>\sales_register\env\lib\site-packages\fastapi\encoders.py", line 289, in jsonable_encoder
    encoded_value = jsonable_encoder(
  File "C:\Users\<User>\sales_register\env\lib\site-packages\fastapi\encoders.py", line 333, in jsonable_encoder
    return jsonable_encoder( 

The last 3 lines here are repeated numerous times. Finally closing off with

  File "C:\Users\<User>\sales_register\env\lib\site-packages\fastapi\encoders.py", line 216, in jsonable_encoder
    if isinstance(obj, BaseModel):
  File "C:\Users\<User>\sales_register\env\lib\site-packages\pydantic\_internal\_model_construction.py", line 248, in __instancecheck__
    return hasattr(instance, '__pydantic_validator__') and super().__instancecheck__(instance)
RecursionError: maximum recursion depth exceeded while calling a Python object

Your suggestions will be much helpful.

If the codebase is needed, I would be okay with sharing.


Solution

  • The issue is with your endpoint returning the Request instance in the response, that is:

    from fastapi import Request
    
    @router.post('/register') 
    async def register_user(request: Request,...
         ...
         return {"request": request, "msg": msg}
                            ^^^^^^^
    

    That is actually what causes the following error:

    RecursionError: maximum recursion depth exceeded while calling a Python object
    

    Removing that should fix the issue for you. Plus, it does not make that sense to return a Python object in the response that would look like this:

    <starlette.requests.Request object at 0x000...>