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.
@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.
@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.
