Logo vun Fastapi

Fastapi

Communautéit
Lynricsy
fastapi

Guides Python FastAPI services with Pydantic v2, dependency injection and async handlers. Use when writing, reviewing or upgrading FastAPI routes, dependencies or response models, an endpoint stalls under concurrent load, a response leaks or drops fields, or tests still hit real databases.

Iwwersiicht

EditeurLynricsy
RepositoryHyperSkills
Skill-Nummfastapi
Stären
26
Forks
0
Matgeliwwert Fichieren
21
LizenzMIT
Linken
  • Instruktiounen am Markdown-Format

    Eng SKILL.md-Datei, déi de Modell bei Bedarf luet, soudatt nëmmen Token ufalen, wann eng Ufro tatsächlech dozou passt.

  • Funktionéiert mat all LLM

    KI-Skills si reng Markdown, keen ubidderspezifesche Code. Dofir funktionéiert dëse Skill mat GPT, Claude, Gemini, Grok oder engem lokale Modell.

  • 21 matgeliwwert Dateien

    Skripten, Virlagen a Referenzen, déi de Modell beim Schaffe liese kann. D'Dateie kënnen nëmme gelies ginn a ginn ni ausgeféiert.

  • Open Source

    Vum Lynricsy op GitHub verëffentlecht. Liest de Quelltext, ier Dir de Skill installéiert.

Installatioun

Installéiert de KI-Skill Fastapi an TypingMind, fir e mat all LLM ze benotzen, oder füügt en an en anere KI-Agent an, deen SKILL.md liest.

1

An TypingMind installéieren

TypingMind installéiert e Skill direkt aus sengem GitHub-Dossier — dobäi gëtt SKILL.md gelies, d'Ressourcedateie gi gebëndelt an d'Resultat gëtt lokal gespäichert.

  1. Maacht d'App op a gitt op Plugins → Skills.
  2. Wielt „Vun GitHub installéieren“.
  3. Pecht d'URL vum Skill-Dossier hei drënner an a bestätegt.
    https://github.com/Lynricsy/HyperSkills/tree/main/skills/fastapi
  4. Aktivéiert de Skill an all Chat, an deem Dir e benotze wëllt.
Plugins → Skills → Skill afügen → Vu GitHub-URL, pecht dann d'URL vum Dossier an a klickt op Weider.
2

An engem anere KI-Agent installéieren

All KI-Agent, deen d'Format Agent Skills liest, kann dëse Skill benotzen — kopéiert den Dossier an de Skill-Verzeechnes vun deem KI-Agent.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/Lynricsy/HyperSkills.git /tmp/HyperSkills
mkdir -p .claude/skills
cp -r /tmp/HyperSkills/skills/fastapi .claude/skills/fastapi
Start Claude Code nom Kopéieren nei, fir datt den neie Skill erkannt gëtt.

An TypingMind benotzen

Aktivéiert Fastapi an engem Chat vun TypingMind, an de Modell iwwerhëlt de Rescht. Den Numm an d'Beschreiwung stinn am System-Prompt. Soubal eng Ufro dozou passt, luet de Modell déi komplett Instruktioune selwer — Dir musst de Skill ni manuell opruffen, an et fale keng Token un, bis en tatsächlech benotzt gëtt.

De Modell luet Fastapi automatesch, soubal eng Ufro dozou passt.

Funktionéiert mat all KI-Modell

KI-Skills si reng Markdown-Instruktiounen amplaz vun ubidderspezifeschem Code. Dofir ass Fastapi net un de Modell gebonnen, fir deen de Skill geschriwwe gouf. Installéiert en eemol an TypingMind a benotzt e mat GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama oder engem lokale Modell, deen Dir selwer bedreift — alles mat Ären eegenen API-Keys.

  • Gëtt nëmme gelueden, wann et néideg ass

    De System-Prompt enthält nëmmen den Numm an d'Beschreiwung. D'Instruktioune gi bei der éischter passender Ufro gelueden, soudatt en onbenotzte Skill näischt kascht.

  • De Modell am lafende Chat wiesselen

    Well de Skill aus Instruktiounen amplaz vu Code besteet, funktionéiert en och no engem Modellwiessel — den nächste Modell liest déi selwecht SKILL.md-Datei.

Skill-Instruktiounen

Dëst ass den Inhalt vu SKILL.md, deen de Modell luet. Liest e virun der Installatioun — e Skill besteet aus Instruktiounen, déi Äre Modell befollegt.

fastapi

Scope

Covers building and reviewing HTTP services with FastAPI: path operations and router organisation, the dependency-injection system, Pydantic v2 models as the request and response contract, error handling, the boundary between async and blocking code, lifespan-managed resources, streaming and Server-Sent Events, background work, security dependencies, the framework's native OpenTelemetry support, testing, and the FastAPI-specific parts of the toolchain.

The body is written against FastAPI 0.141, Pydantic 2.13 and Starlette 1.6; the native telemetry material against FastAPI 0.142.1, where it first shipped.

Not covered:

  • The Python language and its general toolchain — uv projects and dependency groups, ruff rule selection, pytest fixtures and isolation, packaging, Protocol/TypedDict typing, asyncio task lifetime and cancellation. Use the python skill; this skill only covers what those tools do differently inside a FastAPI project.
  • Deciding what deserves a test and driving code with tests — use the test-driven-development skill.
  • Working a reproducible local failure down to a root cause — use the debugging skill.
  • Reviewing a diff for general correctness and style — use the code-review skill; come back here for the FastAPI-specific rules a reviewer applies.
  • REST contract and OpenAPI specification design (resource modelling, URI and versioning policy, pagination strategy, the error schema itself). Use the api-design skill.
  • Database schema design and query tuning. Use the postgres skill for PostgreSQL.
  • GraphQL schema and operations. Use the graphql skill.
  • Container images, Compose and Kubernetes deployment. Use the containers skill.
  • Vendor-neutral instrumentation — span naming, attribute cardinality, sampling, semantic conventions, Collector pipelines. Use the observability skill; this skill covers only what FastAPI's own telemetry does and how it collides with other setups.
  • ORM and migration tooling. No skill in this library covers it yet; say so rather than improvising one.

Paths below are relative to this skill's directory.

Core rules

  1. Read pyproject.toml and the app factory before changing anything. The FastAPI and Pydantic versions decide which of these rules apply, and an existing project's router layout and error shape are the truth even where they differ from the defaults here.
  2. Declare parameters and dependencies with Annotated: q: Annotated[str | None, Query()], user: Annotated[User, Depends(get_current_user)]. The = Depends(...) default form puts a sentinel in the default slot, so the function can no longer be called or type-checked outside a request. ruff's FAST002 finds and rewrites them.
  3. Name reusable dependencies as type aliases (CurrentUser = Annotated[User, Depends(...)]) and use the alias everywhere. One place to change when the dependency changes.
  4. No ... as a default. A field or parameter with no default is already required, and Field(...) is noise that hides real metadata.
  5. The body of a path operation decides its keyword: fully awaited → async def; contains any blocking call → def, which FastAPI runs in the threadpool. One requests.get inside async def stalls every other request on that worker, not just its own.
  6. The threadpool has 40 slots by default, and def dependencies take slots too. If that is the real ceiling, raise anyio.to_thread.current_default_thread_limiter().total_tokens in lifespan — deliberately, and in step with the connection pool behind it.
  7. When a handler must stay async def, push the blocking call out with asyncer.asyncify (or the project's existing run_in_threadpool), not by hoping it is fast enough.
  8. Annotate the return type and let FastAPI validate, filter, document and serialise from it. Use response_model= only when the public schema differs from what the function returns, and then annotate the return -> Any; declaring both for the same type is what FAST001 flags.
  9. Filter secrets structurally. A response model that never declares password_hash cannot leak it; response_model_exclude is a list someone forgets to extend on the next endpoint.
  10. Pydantic serialises according to the declared type, not the runtime class. Returning a subclass where the base is declared silently drops the subclass's fields — no error, no warning. Use a discriminated union when both shapes are real.
  11. Router-level prefix, tags and dependencies belong on the APIRouter(...) constructor, not on include_router(). What guards a router should be readable in the router's own file.
  12. One HTTP method per function. @app.api_route(methods=["GET", "POST"]) with a branch on request.method produces one OpenAPI operation for two behaviours.
  13. Declare the success status code on the decorator: 201 for create, 202 for accepted and queued, 204 (with response_model=None) for no body. A 204 handler that returns data does not fail — Starlette drops the body and the client gets nothing.
  14. Raise ValueError from a validator, not HTTPException: FastAPI turns it into the standard 422 with the field path. And never assert in a validator — assertions vanish under python -O, so validation silently stops.
  15. Register exception handlers against Starlette's HTTPException, not FastAPI's subclass. Otherwise the 404s and 405s Starlette itself raises bypass your handler and your API answers in two different error shapes.
  16. One error contract for the whole API, applied to HTTPException, validation failures and your own domain exceptions. Define domain exceptions with no HTTP knowledge and map them to status codes in one handler module.
  17. Dependencies with yield use try/finally, and any except that catches must re-raise. Swallowing the exception still returns 500 but leaves the server with no log line at all.
  18. A yield dependency's exit code runs after the response is sent by default. Use Depends(f, scope="function") when it must close before the client sees a byte — a session held across a slow response is a connection-pool leak.
  19. Dependency results are cached for the duration of one request, so decompose into small dependencies freely; a shared sub-dependency runs once. use_cache=False is the exception that needs a comment.
  20. Long-lived resources — connection pools, HTTP clients, loaded models — are created in an asynccontextmanager lifespan and read from request.state, not built per request and not stashed on app.state. @app.on_event is deprecated and stops firing entirely once a lifespan exists.
  21. BackgroundTasks runs in the same worker process after the response, with no retry and no persistence. If losing the work would page someone, return 202 with a job id and hand it to a real queue.
  22. On FastAPI 0.142+, request traces, metrics and exception logs are recorded natively; configure them with FastAPI(telemetry={...}) and do not add opentelemetry-instrumentation-fastapi. Exporting is version-gated: 0.142 attaches OTLP exporters from the OTEL_* variables by default, 0.143+ only with FASTAPI_OTEL_AUTO_CONFIGURE=true (or auto_configure: True), so an endpoint alone exports nothing. When anything else sets up providers, run it at import time and keep automatic setup off — otherwise every span is exported twice and a provider set in lifespan is silently refused. A gRPC or non-OTLP environment aborts startup on 0.142.0–0.142.1 and only logs a warning from 0.142.2. Exception messages are exported with the logs, so keep secrets out of them (references/opentelemetry.md).
  23. Override dependencies in tests with app.dependency_overrides[dep] = fake, and clear them in teardown. Patching the module attribute does nothing: the route captured the callable when the decorator ran.
  24. TestClient(app) without a with block never runs lifespan, so startup state is missing and the failure surfaces as an unrelated AttributeError. Use with TestClient(app) as client: in a fixture.
  25. Use TestClient for tests that only send requests; switch to AsyncClient(transport=ASGITransport(app=app)) only when the test body itself awaits application code. AsyncClient(app=app) is not a thing any more.
  26. Finish with the gate: uv run ruff format, uv run ruff check (with FAST enabled), the project's type checker, uv run pytest, and one request against the running app for the path you touched. Report the commands and their output.

Workflows

add-or-change-an-endpoint

  • Locate the router that owns this resource and read its APIRouter(...) line: the prefix, tags and shared dependencies already applied are part of your route's contract.
  • Decide the models before the handler. Request model, response model, and whether they can honestly be the same type — if the response has an id or the request has a secret, they cannot (references/pydantic-models.md).
  • Write the signature: Annotated for every parameter and dependency, declared return type, status_code on the decorator, responses={...} for the error codes a client must handle.
  • Pick async def or def by looking at what the body calls, not at the neighbours (references/async-and-lifecycle.md).
  • Route failures through the app's existing error contract; add a domain exception plus one handler entry rather than a bespoke JSONResponse (references/responses-and-errors.md).
  • Add the test in the same change: one happy path asserting the status code and body, one asserting an excluded field is absent, one invalid payload asserting 422 (references/testing.md).
  • Gate — the contract is what you think it is: the new tests pass, uv run ruff check is clean with FAST enabled, and /openapi.json shows the operation with the intended status code, response schema and no field you meant to hide.

fix-a-slow-or-stalling-endpoint

  • Confirm the shape of the problem first: latency that grows with concurrency on endpoints that are not themselves slow means the event loop is blocked, not that the database is.
  • Grep the async def handlers and async def dependencies for blocking calls: requests, time.sleep, subprocess, synchronous database drivers, file IO. Each one is a stall for the whole worker.
  • For each, choose: make the handler def (whole body blocks), or keep async def and wrap the call in asyncer.asyncify / run_in_threadpool (mixed body). Say which and why.
  • Count the threadpool consumers — def handlers plus def dependencies plus explicit threadpool calls — against the 40-slot default and against the connection pool behind them.
  • Run with PYTHONASYNCIODEBUG=1 to get the loop's slow-callback warnings if the blocking frame is not obvious from reading.
  • Check what is being rebuilt per request that should be in lifespan: an HTTP client, an engine, a parsed config, a loaded model.
  • Look for work that should not be in the request at all — anything multi-second belongs in a queue with a 202 and a job id, not in BackgroundTasks.
  • Gate — the symptom is explained, not just gone: name the blocking frame or the per-request construction, and show a concurrent run (not a single request) where latency no longer scales with concurrency.

write-or-fix-tests

  • Inventory what the suite currently touches for real: identity providers, databases, queues, third-party APIs. Each one is a dependency to override, not a service to mock at the HTTP-library level.
  • Build the client in a fixture with with TestClient(app) so lifespan runs, and set the overrides before the client is created so no request can reach the real dependency.
  • Override the dependency that produces the value (get_current_user, get_db), not the security scheme and not the transport. Clear app.dependency_overrides in teardown.
  • Keep exactly one test on the real token-decoding path with a locally signed token; the rest override the user.
  • Add async tests only where the test body must await application code, and then use ASGITransport plus pytest.mark.anyio with an anyio_backend fixture.
  • Assert the HTTP surface: status code, body, absence of excluded fields, the 422 shape for one invalid payload. Details in references/testing.md.
  • Gate — offline and honest: the suite passes with no network access, the previously failing test now passes, and removing the behaviour under test makes it fail again.

migrate-legacy-fastapi-code

  • Establish the floor: which FastAPI and Pydantic versions the project pins, and whether pydantic.v1 shims are still imported anywhere.
  • Convert dependency and parameter declarations to Annotated first — uv run ruff check --select FAST002 --fix does most of it mechanically, and everything else is easier afterwards.
  • Work through the Pydantic v1 → v2 mapping in references/pydantic-models.md. Watch the one that fails silently: Optional[X] no longer implies a None default, so migrated PATCH models start rejecting requests that omit fields.
  • Replace @app.on_event with a lifespan context manager, and check that no startup hook was left behind — once lifespan exists, the leftovers never run again.
  • Drop ORJSONResponse / UJSONResponse (deprecated) and any RootModel wrappers in favour of declared return types.
  • Delete response_model= where it now duplicates the return annotation (FAST001).
  • Crossing 0.142 or 0.143: find every existing OpenTelemetry setup (FastAPIInstrumentor, provider code, Logfire or vendor SDKs, the deployed OTEL_* and FASTAPI_OTEL_AUTO_CONFIGURE variables) and settle each one against native telemetry before deploying (references/opentelemetry.md).
  • Update the tests in the same change: dependency_overrides instead of patches, ASGITransport instead of AsyncClient(app=...), with TestClient(app) for lifespan.
  • Gate — same contract, new code: /openapi.json before and after the migration differ only where you intended, and the full suite passes with FAST rules enabled.

Topic router

TopicRead whenFile
Annotated dependencies, yield lifetime and exit scope, per-request caching, router and app-level dependencies, security schemesWiring anything through Depends, or chasing a connection held too longreferences/dependencies.md
Request/response model split, Field placement, validators, aliases, from_attributes, discriminated unions, input/output schema separation, v1→v2 mappingDesigning or migrating the models at the HTTP boundaryreferences/pydantic-models.md
Return type vs response_model, status codes, HTTPException, exception handlers, error contract, OpenAPI metadataA response leaks, drops fields, or the API answers errors in more than one shapereferences/responses-and-errors.md
async def vs def, the 40-slot threadpool, run_in_threadpool, lifespan and lifespan state, background tasks vs a queue, middleware, workersLatency grows with concurrency, or a resource is built per requestreferences/async-and-lifecycle.md
SSE with EventSourceResponse, JSON Lines, byte streaming, what breaks mid-stream, WebSocketsBuilding a long-lived or incremental responsereferences/streaming.md
TestClient vs httpx.AsyncClient, httpx2, lifespan in tests, dependency_overrides, conftest patterns, what to assertWriting tests, or a test suite still reaching real servicesreferences/testing.md
Install extras, fastapi dev/run and [tool.fastapi] entrypoint, the FAST ruff rules, project layout, library defaults, client generationSetting up a project or its checksreferences/tooling.md
Native request traces, metrics and exception logs, the telemetry dict, auto_configure at startup, coexistence with FastAPIInstrumentor/Logfire/own providers, request-span attributes, secrets in exception logsAdding, upgrading or debugging telemetry on FastAPI 0.142+, or spans are missing, doubled, or startup fails after an upgradereferences/opentelemetry.md

Environment

  • Install with uv add 'fastapi[standard]': that brings the fastapi CLI, uvicorn with uvloop/httptools, and (from 0.142) the OpenTelemetry SDK with the OTLP http/protobuf exporter. Plain fastapi is the minimal install for a service that supplies its own server.

  • Declare the app once in pyproject.toml so commands need no path argument:

    toml
    [tool.fastapi]
    entrypoint = "my_app.main:app"

    Then fastapi dev (reload, localhost) and fastapi run (production) work from anywhere in the project.

  • Add httpx2 as a test dependency (uv add --group test httpx2). Starlette's TestClient (1.6–1.10) prefers it and emits a deprecation warning when it falls back to httpx v1 — the only client fastapi[standard] installs up to 0.143.3 (0.143.4+ adds httpx2 to the extras).

  • Enable the FastAPI lint group explicitly — it is off by default:

    toml
    [tool.ruff.lint]
    extend-select = ["FAST"]
  • When a command named here does not exist in the project, say so and name the command the project actually uses rather than substituting your own.

Matgeliwwert Fichieren

De Modell liest dës Dateie bei Bedarf, wärend de Skill gelueden ass. Si ginn als liesbar Dateie bereetgestallt a ginn ni ausgeféiert.

Heefeg gestallte Froen

Wat mécht de KI-Skill Fastapi?

Guides Python FastAPI services with Pydantic v2, dependency injection and async handlers. Use when writing, reviewing or upgrading FastAPI routes, dependencies or response models, an endpoint stalls under concurrent load, a response leaks or drops fields, or tests still hit real databases.

Firwat Fastapi op TypingMind benotzen?

Well Dir en eemol installéiert a mat all Modell benotze kënnt. Fastapi besteet aus einfachem Markdown amplaz vun ubidderspezifeschem Code. Dee selwechte Skill funktionéiert also mat GPT-5, Claude, Gemini, Grok oder engem lokale Modell – an Dir kënnt de Modell matzen am Chat wiesselen, ouni datt de Skill ophält ze funktionéieren. TypingMind benotzt Är eegen API-Schlësselen, sou datt Dir d'Ubidder direkt bezuelt amplaz vun engem Abonnement pro Benotzerplaz. Är Skills a Chats bleiwen an Ärem eegene Späicher.

Wéi installéieren ech Fastapi an TypingMind?

Maacht an TypingMind Plugins → Skills → Vu GitHub installéieren op a setzt https://github.com/Lynricsy/HyperSkills/tree/main/skills/fastapi an. TypingMind liest d'Datei SKILL.md, bündelt déi zougehéiereg Dateien an installéiert dat Ganzt als Skill, deen Dir pro Chat aktivéiere kënnt.

Wéi eng KI-Modeller kënnen Fastapi benotzen?

All Modell, deen Dir an TypingMind verbënnt. KI-Skills sinn einfach Markdown-Instruktiounen an net ubidderspezifesche Code. Dofir kënne GPT, Claude, Gemini, Grok a lokal Modeller dëse Skill lueden, wann eng Ufro dozou passt.

Wéi vill KI-Modeller kann ech mat Fastapi benotzen?

Esou vill Dir wëllt. Soulaang e Modell Skills ënnerstëtzt, kënnt Dir Fastapi domat benotzen – GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama a méi – alles op TypingMind mat Ären eegenen API-Schlësselen.

Ass de KI-Skill Fastapi gratis?

Jo. De Skill gëtt vum Lynricsy ënner der Lizenz MIT op GitHub publizéiert. Dir bezuelt nëmmen Ären eegene KI-Ubidder fir d'Token, déi Dir benotzt.

Wat si KI-Skills?

E KI-Skill ass e weiderverwendbaart Pak vun Instruktiounen, dat engem KI-Modell bäibréngt, wéi en eng bestëmmt Aufgab erfëllt. E follegt dem oppenen Agent Skills-Format: eng Datei SKILL.md mat engem Numm an enger Beschreiwung, plus all Scripten, Virlagen oder Referenzdateien, déi de Modell eventuell brauch. De Modell liest d'Instruktiounen eréischt, wann Är Ufro zum Skill passt. En installéierte Skill kascht also näischt, bis e benotzt gëtt.

Wéi ënnerscheede sech KI-Skills vu Plugins oder MCP-Serveren?

E Plugin oder MCP-Server gëtt engem Modell nei Tools, déi en opruffe kann – Code, deen iergendwou leeft an e Resultat zeréckgëtt. E KI-Skill gëtt dem Modell dogéint Wëssen an eng Virgoensweis: wéi en eng Aufgab ugeet, wéi eng Schrëtt en nokënnt a wéi eng gutt Ausgab ausgesäit. Skills bestinn aus einfachem Markdown, brauche kee Server, keen API-Schlëssel a keng Lafzäitëmfeld a funktionéiere mat all Modell.

All weisen

Elo Ären eegene KI-Workspace ariichten

Abonnéiert Iech op eisen Newsletter, fir iwwer nei Funktiounen an zukünfteg Gewënnspiller informéiert ze ginn 👇