Prefect + FastAPI

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

from fastapi import FastAPI
from prefect import flow

app = FastAPI()


@app.get('/get_data/{start_date}/{end_date}/{chunk_number}')
@flow(name='9090 Endpoint')
def get_data(start_date: str, end_date: str, chunk_number: str):
    try:
        return get_data_chunks(start_date, end_date, int(chunk_number))
    except Exception as e:
        raise HTTPException(status_code=500,
                            detail=str(e))

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')
@app.get('/get_data/{start_date}/{end_date}/{chunk_number}')

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