r/FastAPI • • Dec 17 '25

pip package What do you consider “table stakes” for a FastAPI service? (I tried to package mine)

Thumbnail nfrax.com
0 Upvotes

Every time I start a FastAPI service, I think I’m just going to build endpoints… then I end up rebuilding the same foundation:

  • auth (JWT/sessions + OAuth)

  • background jobs + retries

  • webhooks (signing/verification + delivery retries)

  • caching + rate limiting

  • metrics/health checks

I finally extracted my defaults into svc-infra so I can do this up front:

from svc_infra.api.fastapi.ease import easy_service_app
from svc_infra.api.fastapi.auth import add_auth_users
from svc_infra.jobs.easy import easy_jobs

app = easy_service_app(name="MyAPI")
add_auth_users(app)          # JWT, sessions, OAuth hooks, MFA, API keys
queue, scheduler = easy_jobs()  # queue + retries + scheduler

It also includes webhooks (HMAC signing + signature verification), caching helpers (Redis/memory), rate limiting, and Prometheus/OTEL wiring.

Tradeoff: it assumes “production-ish” defaults (Postgres/Redis). If you hate that, totally fair.

Repo: https://github.com/nfraxlab/svc-infra

What do you personally consider non-negotiable for a new FastAPI service? And do you keep it as a repo template or a shared internal package?


r/FastAPI • • May 11 '26

feedback request long shot - anyone have a python sentry crash sitting unresolved that i could try to reproduce for you? (free, weird ask)

1 Upvotes

im 19, i have been building this thing for a few months that takes a sentry url and tries to turn the crash into a failing pytest you can paste into your repo. the agent reads the stacktrace, picks the frame in your code, builds a test that calls the function with whatever locals sentry captured at crash time, runs it in a docker sandbox and tells you if it still reproduces on your current branch.

it works on my own django+celery demo (integrityerror races, staledataerror, fk violations). problem is i havent actually run it on anyone elses real production code yet which is kinda the entire point. or a writeup of what it thinks happened and why it gave up. either way you get something. i just need 5 min of feedback after if you can spare it.

things i already know break it: race conditions across multiple workers, anything depending on live db rows that arent in the frame, c-extension stuff. those still ship a writeup not a green test. im genuinely trying to find more edges so please dont send me only the easy ones.

no signup no install nothing. paste url in dm or here. if your org is locked down i can take the redacted event json instead

it's python only for now(django/flask/fastapi/celery/sqlalchemy whatever, anything where sentry has frame locals turned on)


r/FastAPI • • Oct 24 '23

Question Adding Google ReCaptcha to fastAPI

1 Upvotes

Hello,
I am working on a project that has a backend using FastAPI and I want to add ReCaptcha to an endpoint to avoid server abuse. I couldn't find a way to do so on google all the results were for flask. Does anyone know what should I do or if there references that would help me?


r/FastAPI • • Feb 18 '23

Question Help: how to make ML predictions of a pandas dataframe

0 Upvotes

Hi, I need help, after I saved a ML learning model as a pickle file, how can i create the FastAPI endpoint that makes predictions of a new features pandas dataframe? Not sure how to make FastAPI "read" the pandas dataframe for the prediction. Thanks


r/FastAPI • • Apr 13 '22

Question Can we use Pydantic models (Basemodel) directly inside model.predict using FastAPI, if not why?

1 Upvotes

I'm using Pydantic model (Basemodel) with FastAPI and converting the input into a dictionary, and then converting it into a Pandas dataframe to assign it into "model.predict" for Machine Learning Prediction as bellow :

from fastapi
import FastAPI
import uvicorn
from pydantic import BaseModel 
import pandas as pd 
from typing import List 

class Inputs(BaseModel):     
f1: float,     
f2: float,     
f3: str 

@app.post('/predict') 
def predict(features: List[Inputs]):    
 
output = []      
# loop the list of input features 
for data in features:          
result = {}           
# Convert data into dict and then into a DataFrame             
data = data.dict()             
df = pd.DataFrame([data])           
# get predictions             
prediction = classifier.predict(df)[0]           
# get probability             
probability = classifier.predict_proba(df).max()           
# assign to dictionary              
result["prediction"] = prediction             
result["probability"] = probability           
# append dictionary to list (many outputs)             
output.append(result)      

return output 

It works fine, I'm just not quite sure if it's optimized or the right way to do it, since I convert the input two times to get the predictions, and I'm not sure if it's gonna work fast in case having a huge number of inputs. Any improvements for this ?! If there's a way (even other than using (Pydantic models) where I can work directly and avoid going through conversions and the loop


r/FastAPI • • Sep 06 '21

Hosting and deployment Autonoma v1.0.0 - I made a service using FastAPI + React that automates your playlists!

0 Upvotes

Tl;dr - I made a free service that lets you use the native "like" Spotify feature to automatically add songs to playlists

Link: https://spotify.janhendrikewers.uk/

What is Autonoma?

Autonoma, at it's core, is a script on a loop checking if you've liked any new songs. If you have, that song gets added to a monthly playlist. It's really all quite simple.

But why?

I have used this in various forms for the past 7 or so years. I absolutely need it in my life because I'm too lazy to press 4 more buttons to accomplish the same thing. Jokes aside, it's a banging feature for when you're driving or cba unlocking your phone.

Talk me through the UX

  1. You like the song you're currently listening to

  2. You unlock your phone

  3. On android, you swipe down your notification tray and press the heart button

  4. Autonoma does the rest

Is this project open source?

Not yet. If people start using Autonoma and there's interest in helping out with the development, then I would definitely consider it. For now, if you have any suggestions/bugs/questions/etc., please make your voice heard at https://github.com/autonomauk/feedback


r/FastAPI • • Mar 23 '22

feedback request Participate in user research with the Veterans Affairs

0 Upvotes

Hello everyone! I'm Sones, an associate UX researcher at the Department of Veterans Affairs (VA). I work on the VA Lighthouse APIs platform, which gives developers secure access to VA data so that they can build services for Veterans.

We're looking to speak with devs in different industries and with various levels of API experience -- from self-taught beginners to experts -- about what makes a good API developer platform. Your insights will help us give devs the tools they need to create reliable and secure Veteran services.

If you're interested in helping us, please sign up to participate in future research sessions. We'll reach out if you're a good fit for an upcoming study, and you can opt out at any time. Sign up here. If you have any questions, feel free to drop them below in this thread or message me directly. Thanks for your time!


r/FastAPI • • Dec 27 '21

Question (HELP) 'Pydantic model' object is not subscriptable

1 Upvotes

Hey!

I'm trying to create an API where I take an array in a request body and then update a database based on the array values. I'm seeing the error in title and have no idea where it's going wrong.

This is my (part of my) code, and this is where the error happens.

@assessments.put("/qna/sort_order",
                 tags=["Assessments"])
async def update_assessments_sort_order(
        assessment_qna_sort_order: List[AssessmentQNASortOrder]):
    # loop through assessment_qna_sort_order and update sort_order
    for assessment_qna in assessment_qna_sort_order:
        sort_order = assessment_qna['sort_order']

The error occurs in the last line where I assign sort order. The AssessmentQNASortOrder pydantic model is:

class AssessmentQNASortOrder(BaseModel):
    assessment_qna_id: str
    sort_order: str

And the error I'm getting:

TypeError: 'AssessmentQNASortOrder' object is not subscriptable

I do not know what I'm doing wrong. My request body looks something like this:

[
{"sort_order": "1234", 
"user_id": 1},
{"sort_order": "12", 
"user_id": 2},
]

and printing assessment_qna_sort_order on the terminal gives me something like this, of type List

[AssessmentQNASortOrder(sort_order='1234', user_id='1'), AssessmentQNASortOrder(sort_order='12', user_id='2'), ]


<class 'list'>

Should i not be able to access the content of each index of the list using the bracket notation ['sort_order'] or ['user_id'] as I have done above?

Please help me out, thanks in advance!


r/FastAPI • • Sep 16 '25

Hosting and deployment FastAPI deployment

1 Upvotes

Hey im trying to deploy my FASTAPI application on render but im facing some issues. please let me know if you can help out and we can discuss this further. Thanks :)


r/FastAPI • • Mar 27 '26

pip package API-Shield is an application-level API control at runtime for ASGI frameworks with no redeployments

1 Upvotes

There is no complete library for managing API lifecycle at the application level in ASGI frameworks, so I built one.

api-shield gives you per-route control over maintenance windows, environment gating, deprecation, rate limiting, canary rollouts, and feature flags, all at runtime, without redeploying.

FastAPI is fully supported today and more adapters are on the roadmap.

What it covers

Per-route maintenance windows: put one endpoint in maintenance while every other route keeps serving. Schedule windows in advance and they activate/deactivate automatically.

Environment gating: routes decorated with @env_only("dev") return 404 in production (not 403, you probably don't want to advertise the route exists). Hidden from OpenAPI docs in the wrong environment too.

Deprecation headers:@deprecated injects Deprecation, Sunset, and Link RFC headers automatically. Route keeps working, clients get the signal to migrate.

Rate limiting: per-IP, per-user, per-API-key, or global shared counters. Fixed window, sliding window, moving window. Tiered limits by subscription plan. Exempt specific IPs or roles. One decorator.

Canary rollouts: @rollout(percentage=10) sends 10% of traffic to the route. Adjust the percentage at runtime without touching code.

Feature flags: built-in flag engine with an OpenFeature-compatible client. Boolean, string, integer, float, and JSON flag types. Individual targeting, attribute-based rules, percentage rollout, and kill-switch. Flags share the same dashboard and CLI as everything else.

Multiple services, one control plane: run a standalone Shield Server and connect multiple services to it via the SDK. Each service registers under its own namespace. Manage them independently or all at once from one dashboard and one CLI.

Full examples: github.com/Attakay78/api-shield/tree/main/examples

Runtime control

Everything is controllable at runtime through three surfaces with no redeploy and no restart:

  • Dashboard — mountable HTMX UI with live SSE updates. Route state, maintenance scheduling, audit log, flag management.

  • CLI — shield status, shield maintenance /api/payments --reason "DB migration", shield flags disable new-checkout, shield services and more.

  • REST API — the same admin is also a REST API for integration with deployment pipelines and runbooks.

Links

Happy to answer questions about any of the supported features.


r/FastAPI • • Mar 14 '26

pip package Open Source Credit Management Library — Plug-and-Play Credits Token Billing & Subscriptions

1 Upvotes

Hello everyone,

As LLM-based applications move from prototypes to production, managing consumption-based billing (tokens/credits) remains a fragmented challenge. I’ve developed CreditManagement, an open-source framework designed to bridge the gap between API execution and financial accountability.

GitHub Repository:https://github.com/Meenapintu/credit_management

Core Philosophy

CreditManagement is designed to be unobtrusive yet authoritative. It functions either as a library plugged directly into your Python app or as a standalone, self-hosted Credit Manager server.

High-Performance Features

  • Automated FastAPI Middleware: Implements a sophisticated "Reserve-then-Deduct" workflow. It automatically intercepts requests via header values to reserve credits before the API logic executes and finalizes the deduction post-response—preventing overages in high-latency LLM calls.

  • Agnostic Data Layer: Includes a dedicated Schema Builder. While it supports MongoDB and In-Memory data (for CI/CD and testing) out of the box, it is engineered to be extended to any database backend.

  • Bank-Level Audit Logging: For compliance-heavy environments, every Credit operation (Check, Reserve, Deduct, Refund) triggers an immutable logger entry. This provides a transparent, "bank-level" audit trail for every transaction.

  • Full Lifecycle Management: Ready-to-use API routers for subscriptions, credit checks, and balance adjustments.

The LLM Use Case

If you are building an AI wrapper or an agentic workflow, you know that token counting is only half the battle. This framework handles the state management of those tokens—ensuring that if an LLM call fails, the reserved credits are handled correctly, and if it succeeds, they are billed precisely.

Architecture & Extensibility

The framework is built for developers who prioritize clean architecture:

  1. Pluggable: Drop the middleware into FastAPI, and your billing logic is decoupled from your business logic.

  2. Scalable: The self-hosted server option allows you to centralize credit management across multiple microservices.

  3. Compliant: Built-in logging ensures you are ready for financial audits from day one.

Collaboration & Feedback

I am looking for professional feedback from the community regarding:

  • Middleware Expansion: Interest in Starlette or Django Ninja support?

  • Database Adapters: Which SQL-based drivers should be prioritized for the Schema Builder?

  • Edge Cases: Handling race conditions in high-concurrency "Reserve" operations.

Check out the repo, and if this helps your current stack, I’d appreciate your thoughts or a star!

Technologies: #Python #FastAPI #LLM #OpenSource #FinTech #BackendEngineering #MongoDB


r/FastAPI • • Dec 21 '25

pip package Open-source FastAPI full-stack template for AI/LLM apps – now with LangChain support alongside PydanticAI!

12 Upvotes

Hey r/FastAPI,

For those new here: I've developed an open-source CLI generator that creates production-ready full-stack templates centered around FastAPI for AI/LLM applications. It's all about eliminating boilerplate so you can dive straight into building scalable APIs, integrating AI agents, and deploying enterprise-grade apps – think chatbots, ML tools, or SaaS products with real-time features.

Repo: https://github.com/vstorm-co/full-stack-fastapi-nextjs-llm-template
(Install via pip install fastapi-fullstack, then generate with fastapi-fullstack new – the wizard lets you select LangChain, databases, auth, and more)

Exciting update: I've just integrated full LangChain support! Now, when generating your project, you can choose LangChain (with LangGraph for agents) or PydanticAI as your AI framework. This adds flexible chains, tools, streaming responses, conversation persistence, and LangSmith observability – all seamlessly wired into your FastAPI backend with WebSockets and async handling.

Quick overview for newcomers:

  • FastAPI Core: High-performance async APIs with Pydantic v2, versioned routes, dependency injection, middleware (CORS, CSRF, rate limiting), and a layered architecture (routes → services → repositories).

  • Databases & Tasks: Async support for PostgreSQL (SQLAlchemy + Alembic), MongoDB, or SQLite; background queues with Celery/Taskiq/ARQ.

  • Auth & Security: JWT with refresh, API keys, OAuth2 (Google) – all configurable.

  • AI/LLM Features: LangChain or PydanticAI agents with tool calling, multi-model support (OpenAI/Anthropic), WebSocket streaming, and persistence. Observability via LangSmith (for LangChain) or Logfire.

  • Frontend (Optional): Next.js 15 with React 19, Tailwind, dark mode, i18n, and a chat UI for real-time interactions.

  • 20+ Integrations: Redis, admin panels (SQLAdmin), webhooks, Sentry/Prometheus, Docker/CI/CD/Kubernetes – pick what you need to avoid bloat.

  • Django-style CLI: Auto-discovered commands for migrations, user management, seeding, and custom scripts – super handy for FastAPI workflows.

  • Why FastAPI? Leverages its speed and type-safety for everything from API endpoints to agent orchestration. 100% test coverage included.

Screenshots (updated chat UI, auth, LangSmith/Logfire dashboards), demo GIFs, architecture diagrams (Mermaid), and detailed docs are in the README. Also check out the related pydantic-deep for advanced agents.

If you're using FastAPI for AI projects, how does this align with your setups?

  • Does the LangChain integration help with your LLM workflows?

  • Any FastAPI-specific features to expand (e.g., more async integrations)?

  • Pain points it addresses (or misses) in production apps?

Feedback and contributions welcome – let's make FastAPI even stronger for AI devs! 🚀

Thanks!


r/FastAPI • • May 24 '21

Other Is FastAPI a Fad?

Thumbnail
andrewbrookins.com
6 Upvotes

r/FastAPI • • Sep 29 '21

Question "msg": "field required", "type": "value_error.missing" error

0 Upvotes

code: https://github.com/InternNova-Labs/api/blob/5077b58508f83fa11e9fc11f2938a95501331bc2/app/routers/internships.py#L34

Postman request:

{
  "id": 0,
  "position_title": "string",
  "company_name": "string",
  "description": "string",
  "part_time_or_full_time": "part_time",
  "location": "string",
  "skills": [
    "string"
  ],
  "number_of_openings": 0,
  "expires_on": "2021-09-29",
  "application_id": 0,
  "applications": [
    {
      "id": 0,
      "intern_name": "string",
      "intern_phone_number": 0,
      "intern_email": 0,
      "internship_id": 0,
      "user_id": 0,
      "applicant_information": "string"
    }
  ]
}

API response:

{
    "detail": [
        {
            "loc": [
                "body",
                "position_title"
            ],
            "msg": "field required",
            "type": "value_error.missing"
        },
        {
            "loc": [
                "body",
                "company_name"
            ],
            "msg": "field required",
            "type": "value_error.missing"
        },
        {
            "loc": [
                "body",
                "description"
            ],
            "msg": "field required",
            "type": "value_error.missing"
        },
        {
            "loc": [
                "body",
                "part_time_or_full_time"
            ],
            "msg": "field required",
            "type": "value_error.missing"
        },
        {
            "loc": [
                "body",
                "location"
            ],
            "msg": "field required",
            "type": "value_error.missing"
        },
        {
            "loc": [
                "body",
                "skills"
            ],
            "msg": "field required",
            "type": "value_error.missing"
        },
        {
            "loc": [
                "body",
                "number_of_openings"
            ],
            "msg": "field required",
            "type": "value_error.missing"
        },
        {
            "loc": [
                "body",
                "expires_on"
            ],
            "msg": "field required",
            "type": "value_error.missing"
        },
        {
            "loc": [
                "body",
                "application_id"
            ],
            "msg": "field required",
            "type": "value_error.missing"
        }
    ]
}

Sorry if this is a stupid question.. I have been pulling my hair for quite long because of it though.


r/FastAPI • • Nov 14 '21

Question Depends () FastAPI

0 Upvotes

Hi !

Beginner here ! I am using fastAPI. But I could not understand the depends() function. Can Someone explain the need for it ? and the use of it ? Thanks in advance!


r/FastAPI • • Mar 28 '24

Question How to have an endpoint with both response_model and response_class

0 Upvotes

Hi all,

Just started creating a simple app to keep track of my garden plants. Before I go down a wrong road, I would like some help. I have an endpoint to get details of a plant, but I want the same endpoint to work as an API. How do I do that. This is what I have:

...

app = FastAPI(lifespan=lifespan)
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")

...

@app.get("/plants/{plant_id}", response_class=HTMLResponse, response_model=PlantRead)
async def read_plant(
    *, session: Session = Depends(get_session), request: Request, plant_id: int
):
    if plant := session.get(Plant, plant_id):
        context = {"plant": plant}
        return templates.TemplateResponse(
            request=request, name="plant.html", context=context
        )
    raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Plant not found")

I use SQLModel/Pydantic for all the model stuff (e.g. PlantRead just has the attributes id, name, kind and created_date). When I access the endpoint in the SwaggerUI, the response is just the HTML template rendered, but I would like it to be json. How do I organize this? Or am I doing something the wrong way?


r/FastAPI • • Feb 22 '24

Question How to return nothing with status 204 using response_model?

0 Upvotes

I have a GET endpoint which uses response_model for the response. There is however a possibility that the user will call it with non-existent id. In which case I would like to return status 204 and, well, no content. I do use response.status_code = status.HTTP_204_NO_CONTENT , however what should the function actualy return? I tried return None but that resulted in following error:

fastapi.exceptions.ResponseValidationError: 1 validation errors:
  {'type': 'model_attributes_type', 'loc': ('response',), 'msg': 'Input should be a valid dictionary or object to extract fields from', 'input': None, 'url': 'https://errors.pydantic.dev/2.6/v/model_attributes_type'}

r/FastAPI • • May 02 '24

Question Using $ in URL - FastAPI 0.54

0 Upvotes

Hi,

I would like to have url like: /Example$trending.

FASTAPI EXAMPLE:

from fastapi import FastAPI

app = FastAPI()


@app.get("/Example$trending")
async def root():
    return {"message": "Hello World"}


#uvicorn test_api:app --reload

If I test this on new FastAPI version everything works. But if I use version 0.54.1 it doesn't work. Any workaround for version 0.54.1 to still include URLs including dollar sign


r/FastAPI • • Dec 04 '20

Hosting and deployment How do I deploy a FastAPI application on cPanel?

0 Upvotes

I'm trying to deploy FastAPI application on cPanel, but I am clueless to start Uvicorn. Below is my main.py file code.

from fastapi import FastAPI

app = FastAPI()

@app.get('/')
async def root():
    return {"message": "Hello World"}

@app.get('/update')
async def update():
	return {"Update": "This seem working!"}

And this is my passenger_wsgi.py file code:

import imp
import os
import sys

sys.path.insert(0, os.path.dirname(__file__))

wsgi = imp.load_source('wsgi', 'main.py')
application = wsgi.app

r/FastAPI • • May 31 '22

Question Someone knows if testdriven.io deliver some type of certificate?

0 Upvotes

Someone knows if testdriven.io generate some type of certification by completion of their courses? I've searching in their web site so I can't found information about that.


r/FastAPI • • Jun 07 '23

Question Convert a LangChain Based Python Notebook or Chatbot Into a REST Web Service for use in Cross Platform Apps

0 Upvotes

Most Data Science and Generative AI Chat Bot activity is being done in Python, and most sample codes and projects are delivered as Notebooks.

Much of the Enterprise App Stack is, well, not in Python. We are a .NET and Java shop new to Python.

We wish to integrate Python code into our existing .NET/C# apps, particularly in the area of Generative AI and LLMs

Although most LLMs such as Open AI natively provide REST APIs, in many cases it would be useful to simply tap into working code written in Python particularly when leveraging Langchain or rapid prototyping of various LLM Models, Data ingestion techniques, and Vector Databases

The general interface to these Generative AI chatbot tools is a text query/response format with some prompts and parameters. A user asks a question. The app provides an answer. This lends itself to exposing the query/response chatbot functionality as a web service which could be leveraged by other apps.

With that background, what is the quickest way to convert Python code, lets say presented already as a Notebook, into a REST web service?

For example Streamlit etc can be used to convert Python code to a UI web app and some tools exist for rapidly converting the the notebook into a simpler web app - is there something robust that exists for web services?

An approach or tool that that would take Python code and set up the basic scaffolding to wrap it with FastAPI to deliver a web service?

Performance goals are modest at 10 hits/second. Authentication support is desirable.

(Alternative Approaches Considered: Our stack in .NET. We did consider using IronPython or Python.net but they do not appear to support the latest version of Python in which most of the Data Science work is being done. Some are not being actively developed. And the Web service approach is cleaner overall

We are aware that Amazon Lambda can also expose Python code as a web service, but hoping for a simpler solution at least during the initial phases of experimenting with various LLMs and chains)


r/FastAPI • • Apr 06 '26

Question Gathering sources more than a month out

Thumbnail reddit.com
0 Upvotes

r/FastAPI • • May 28 '26

Question I built something that cuts down API costs dramatically--- can someone give me feedback?

Thumbnail reddit.com
0 Upvotes