Prefect + FastAPI

I try to use prefect with fast api endpoint like this:

from fastapi import FastAPI
from prefect import flow

app = FastAPI()

@flow(name='9090 Endpoint')
def get_data(start_date: str, end_date: str, chunk_number: str):
        return get_data_chunks(start_date, end_date, int(chunk_number))
    except Exception as e:
        raise HTTPException(status_code=500,

But the flow does not get created in the prefect UI when I call the endpoint, even though the endpoint runs and the prefect setup is all correct.

If I set the flow decorator over get_data_chunks() like this, it WILL trigger a flow in the prefect UI when I manually execute that method from within python, but it will not trigger a flow via the api call.

@flow(name='9090 Endpoint')
def get_data_chunks(start_date: str, end_date: str, chunk_number: int = 0, chunk_size: int = 11000):
    """ ... """

If I reverse the decorators

@flow(name='9090 Endpoint')

I get a 500 response on the api call.

Is there any way to get prefect and fast api to work together?

1 Like

Im looking for answers to the same question. Bumping for visibility