r/FastAPI • • Dec 30 '25

Question FastAPI + Pydantic V2: Is anyone else using it to build AI microservices?

Hey r/FastAPI community!

I’ve been diving deep into FastAPI lately, especially with Pydantic V2 and its shiny new features (like computed fields and strict validation). With the AI/LLM boom happening right now, I’ve started building async microservices for AI pipelines things like prompt chaining, RAG systems, and real-time inference endpoints.What I’ve noticed: FastAPI’s native async support + Pydantic V2’s performance feels perfect for handling streaming responses from models likeOpenAI, Llama, etc. Dependency injection makes it super clean to manage API keys, model clients, and context caching. But… I’m curious how others are structuring their projects.

Questions for you all:

  1. Are you using FastAPI for AI/ML services? If yes, what does your stack look like?
  2. Any cool tips for integrating with message queues e.g., Celery, RabbitMQ, Kafka for async task handling?
  3. What’s your take on scaling WebSockets in FastAPI for real-time AI responses?
52 Upvotes

32 comments sorted by

View all comments

6

u/Challseus Dec 30 '25 edited Dec 30 '25
  1. My Stack

    • FastAPI
    • Pydantic V2
    • Langchain/Pydantic-AI
    • SQLite/Postgres via SQLModel
    • Redis
    • Typer/Click/Rich for my CLI and interactive chat sessions
    • AI Service Layer (works via API and CLI):

      • llm catalog ingestion (using a combined set of data from openrouter and litellm)
      • llm usage calculations
      • streaming and non-streaming chat completions
      • conversation persistence
      • local RAG via chromadb
      • local embedding models using sentence transformer's... I'll butcher the model name, it's the average one that can run on most laptops
      • APScheduler to make sure all the jobs are running and keeping all data up to date, such as LLM catalog data for prices
  2. At my current scale, normal async chat endpoints are fine. However, I still make use of arq and/or TaskIQ to handle async/background tasks, nothing fancy. I haven't needed anything as heavy as celery, or even brokers like RabbitMQ and Kafka... If needed, I just dump off these tasks from the API -> Redis queue, and return a task_id that the client can keep pinging until the job is done. Not a huge fan of this solution, but works for now.
  3. Haven't used WebSockets yet, but it's next on my list! Currently, if I need to stream tokens back to the client, I'm implementing SSE's. Actually, it's been a good solution I've not had problems with, but I still do want to check out WebSockets.

Also, I have my own built in ones, but I've been watching some prefect videos lately, I may move to that build out some pipelines.

But yeah, that's about it.

1

u/arbiter_rise Mar 04 '26

May I ask what observability stack you’re using?

1

u/Challseus Mar 04 '26

I use logfire for traces, my own custom built control plane for everything else: https://github.com/lbedner/aegis-stack/blob/main/docs/images/overseer-demo.gif