This is an issue when calling uvicorn.run() with the reload, debug, or workers parameters while passing the application object directly instead of as an import string.

Why is this an issue?

Uvicorn is an ASGI server that can run your FastAPI application. When you use certain features like auto-reload (for development) or multiple workers (for production), Uvicorn needs to start new Python processes.

When Uvicorn starts a new process, it needs to import your application fresh in that process. This is where the problem occurs: Python cannot serialize and pass application objects between processes. Instead, Uvicorn needs an import string (like "main:app") that tells it how to import the application in each new process.

If you pass the application object directly, Uvicorn will:

This means your development workflow breaks (no auto-reload when you change code) or your production deployment doesn’t scale (no worker processes).

The import string format is "module:variable", where:

For example, if your application is defined as app = FastAPI() in main.py, the import string would be "main:app".

Note: The reload and workers parameters are mutually exclusive. You should use reload=True for development (single process with auto-reload) or workers=N for production (multiple processes without auto-reload), but never both together.

What is the potential impact?

When the application object is passed directly instead of as an import string:

How to fix it

Replace the direct application object reference with an import string in the format "module:variable". The module path should match your file structure, and the variable should be the name of your FastAPI instance.

Code examples

Noncompliant code example

import uvicorn
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello World"}

def main():
    uvicorn.run(app, host="0.0.0.0", port=8000, reload=True)  # Noncompliant

Compliant solution

import uvicorn
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello World"}

def main():
    uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)

Resources

Documentation