@shinyaz

Strands agent() hangs inside FastAPI async def endpoints

1 min read

Tried wrapping a Strands agent with FastAPI and the request hung indefinitely when using async def.

Python (NG)
@app.post("/invocations")
async def invoke(request: InvokeRequest):
    result = agent(request.prompt)  # ← hangs here
    return {"response": result.message["content"][0]["text"]}

agent() is a blocking call — running it inside async def blocks the entire event loop. Switching to def fixes it.

Python (OK)
@app.post("/invocations")
def invoke(request: InvokeRequest):
    result = agent(request.prompt)  # ← runs in thread pool
    return {"response": result.message["content"][0]["text"]}

FastAPI automatically runs def endpoints in an external thread pool, so the main event loop is never blocked. Covered in detail in deploy series part 1.

Share this post

Shinya Tahara

Shinya Tahara

Solutions Architect @ AWS

I'm a Solutions Architect at AWS, providing technical guidance primarily to financial industry customers. I share learnings about cloud architecture and AI/ML on this site.The views and opinions expressed on this site are my own and do not represent the official positions of my employer.