claude-code-pack 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,436 @@
1
+ ---
2
+ name: fastapi
3
+ description: FastAPI best practices and conventions. Use when working with FastAPI APIs and Pydantic models for them. Keeps FastAPI code clean and up to date with the latest features and patterns, updated with new versions. Write new code or refactor and update old code.
4
+ ---
5
+
6
+ # FastAPI
7
+
8
+ Official FastAPI skill to write code with best practices, keeping up to date with new versions and features.
9
+
10
+ ## Use the `fastapi` CLI
11
+
12
+ Run the development server on localhost with reload:
13
+
14
+ ```bash
15
+ fastapi dev
16
+ ```
17
+
18
+
19
+ Run the production server:
20
+
21
+ ```bash
22
+ fastapi run
23
+ ```
24
+
25
+ ### Add an entrypoint in `pyproject.toml`
26
+
27
+ FastAPI CLI will read the entrypoint in `pyproject.toml` to know where the FastAPI app is declared.
28
+
29
+ ```toml
30
+ [tool.fastapi]
31
+ entrypoint = "my_app.main:app"
32
+ ```
33
+
34
+ ### Use `fastapi` with a path
35
+
36
+ When adding the entrypoint to `pyproject.toml` is not possible, or the user explicitly asks not to, or it's running an independent small app, you can pass the app file path to the `fastapi` command:
37
+
38
+ ```bash
39
+ fastapi dev my_app/main.py
40
+ ```
41
+
42
+ Prefer to set the entrypoint in `pyproject.toml` when possible.
43
+
44
+ ## Use `Annotated`
45
+
46
+ Always prefer the `Annotated` style for parameter and dependency declarations.
47
+
48
+ It keeps the function signatures working in other contexts, respects the types, allows reusability.
49
+
50
+ ### In Parameter Declarations
51
+
52
+ Use `Annotated` for parameter declarations, including `Path`, `Query`, `Header`, etc.:
53
+
54
+ ```python
55
+ from typing import Annotated
56
+
57
+ from fastapi import FastAPI, Path, Query
58
+
59
+ app = FastAPI()
60
+
61
+
62
+ @app.get("/items/{item_id}")
63
+ async def read_item(
64
+ item_id: Annotated[int, Path(ge=1, description="The item ID")],
65
+ q: Annotated[str | None, Query(max_length=50)] = None,
66
+ ):
67
+ return {"message": "Hello World"}
68
+ ```
69
+
70
+ instead of:
71
+
72
+ ```python
73
+ # DO NOT DO THIS
74
+ @app.get("/items/{item_id}")
75
+ async def read_item(
76
+ item_id: int = Path(ge=1, description="The item ID"),
77
+ q: str | None = Query(default=None, max_length=50),
78
+ ):
79
+ return {"message": "Hello World"}
80
+ ```
81
+
82
+ ### For Dependencies
83
+
84
+ Use `Annotated` for dependencies with `Depends()`.
85
+
86
+ Unless asked not to, create a new type alias for the dependency to allow re-using it.
87
+
88
+ ```python
89
+ from typing import Annotated
90
+
91
+ from fastapi import Depends, FastAPI
92
+
93
+ app = FastAPI()
94
+
95
+
96
+ def get_current_user():
97
+ return {"username": "johndoe"}
98
+
99
+
100
+ CurrentUserDep = Annotated[dict, Depends(get_current_user)]
101
+
102
+
103
+ @app.get("/items/")
104
+ async def read_item(current_user: CurrentUserDep):
105
+ return {"message": "Hello World"}
106
+ ```
107
+
108
+ instead of:
109
+
110
+ ```python
111
+ # DO NOT DO THIS
112
+ @app.get("/items/")
113
+ async def read_item(current_user: dict = Depends(get_current_user)):
114
+ return {"message": "Hello World"}
115
+ ```
116
+
117
+ ## Do not use Ellipsis for *path operations* or Pydantic models
118
+
119
+ Do not use `...` as a default value for required parameters, it's not needed and not recommended.
120
+
121
+ Do this, without Ellipsis (`...`):
122
+
123
+ ```python
124
+ from typing import Annotated
125
+
126
+ from fastapi import FastAPI, Query
127
+ from pydantic import BaseModel, Field
128
+
129
+
130
+ class Item(BaseModel):
131
+ name: str
132
+ description: str | None = None
133
+ price: float = Field(gt=0)
134
+
135
+
136
+ app = FastAPI()
137
+
138
+
139
+ @app.post("/items/")
140
+ async def create_item(item: Item, project_id: Annotated[int, Query()]): ...
141
+ ```
142
+
143
+ instead of this:
144
+
145
+ ```python
146
+ # DO NOT DO THIS
147
+ class Item(BaseModel):
148
+ name: str = ...
149
+ description: str | None = None
150
+ price: float = Field(..., gt=0)
151
+
152
+
153
+ app = FastAPI()
154
+
155
+
156
+ @app.post("/items/")
157
+ async def create_item(item: Item, project_id: Annotated[int, Query(...)]): ...
158
+ ```
159
+
160
+ ## Return Type or Response Model
161
+
162
+ When possible, include a return type. It will be used to validate, filter, document, and serialize the response.
163
+
164
+ ```python
165
+ from fastapi import FastAPI
166
+ from pydantic import BaseModel
167
+
168
+ app = FastAPI()
169
+
170
+
171
+ class Item(BaseModel):
172
+ name: str
173
+ description: str | None = None
174
+
175
+
176
+ @app.get("/items/me")
177
+ async def get_item() -> Item:
178
+ return Item(name="Plumbus", description="All-purpose home device")
179
+ ```
180
+
181
+ **Important**: Return types or response models are what filter data ensuring no sensitive information is exposed. And they are used to serialize data with Pydantic (in Rust), this is the main idea that can increase response performance.
182
+
183
+ The return type doesn't have to be a Pydantic model, it could be a different type, like a list of integers, or a dict, etc.
184
+
185
+ ### When to use `response_model` instead
186
+
187
+ If the return type is not the same as the type that you want to use to validate, filter, or serialize, use the `response_model` parameter on the decorator instead.
188
+
189
+ ```python
190
+ from typing import Any
191
+
192
+ from fastapi import FastAPI
193
+ from pydantic import BaseModel
194
+
195
+ app = FastAPI()
196
+
197
+
198
+ class Item(BaseModel):
199
+ name: str
200
+ description: str | None = None
201
+
202
+
203
+ @app.get("/items/me", response_model=Item)
204
+ async def get_item() -> Any:
205
+ return {"name": "Foo", "description": "A very nice Item"}
206
+ ```
207
+
208
+ This can be particularly useful when filtering data to expose only the public fields and avoid exposing sensitive information.
209
+
210
+ ```python
211
+ from typing import Any
212
+
213
+ from fastapi import FastAPI
214
+ from pydantic import BaseModel
215
+
216
+ app = FastAPI()
217
+
218
+
219
+ class InternalItem(BaseModel):
220
+ name: str
221
+ description: str | None = None
222
+ secret_key: str
223
+
224
+
225
+ class Item(BaseModel):
226
+ name: str
227
+ description: str | None = None
228
+
229
+
230
+ @app.get("/items/me", response_model=Item)
231
+ async def get_item() -> Any:
232
+ item = InternalItem(
233
+ name="Foo", description="A very nice Item", secret_key="supersecret"
234
+ )
235
+ return item
236
+ ```
237
+
238
+ ## Performance
239
+
240
+ Do not use `ORJSONResponse` or `UJSONResponse`, they are deprecated.
241
+
242
+ Instead, declare a return type or response model. Pydantic will handle the data serialization on the Rust side.
243
+
244
+ ## Including Routers
245
+
246
+ When declaring routers, prefer to add router level parameters like prefix, tags, etc. to the router itself, instead of in `include_router()`.
247
+
248
+ Do this:
249
+
250
+ ```python
251
+ from fastapi import APIRouter, FastAPI
252
+
253
+ app = FastAPI()
254
+
255
+ router = APIRouter(prefix="/items", tags=["items"])
256
+
257
+
258
+ @router.get("/")
259
+ async def list_items():
260
+ return []
261
+
262
+
263
+ # In main.py
264
+ app.include_router(router)
265
+ ```
266
+
267
+ instead of this:
268
+
269
+ ```python
270
+ # DO NOT DO THIS
271
+ from fastapi import APIRouter, FastAPI
272
+
273
+ app = FastAPI()
274
+
275
+ router = APIRouter()
276
+
277
+
278
+ @router.get("/")
279
+ async def list_items():
280
+ return []
281
+
282
+
283
+ # In main.py
284
+ app.include_router(router, prefix="/items", tags=["items"])
285
+ ```
286
+
287
+ There could be exceptions, but try to follow this convention.
288
+
289
+ Apply shared dependencies at the router level via `dependencies=[Depends(...)]`.
290
+
291
+ ## Dependency Injection
292
+
293
+ See [the dependency injection reference](references/dependencies.md) for detailed patterns including `yield` with `scope`, and class dependencies.
294
+
295
+ Use dependencies when the logic can't be declared in Pydantic validation, depends on external resources, needs cleanup (with `yield`), or is shared across endpoints.
296
+
297
+ Apply shared dependencies at the router level via `dependencies=[Depends(...)]`.
298
+
299
+ ## Async vs Sync *path operations*
300
+
301
+ Use `async` *path operations* only when fully certain that the logic called inside is compatible with async and await (it's called with `await`) or that doesn't block.
302
+
303
+ ```python
304
+ from fastapi import FastAPI
305
+
306
+ app = FastAPI()
307
+
308
+
309
+ # Use async def when calling async code
310
+ @app.get("/async-items/")
311
+ async def read_async_items():
312
+ data = await some_async_library.fetch_items()
313
+ return data
314
+
315
+
316
+ # Use plain def when calling blocking/sync code or when in doubt
317
+ @app.get("/items/")
318
+ def read_items():
319
+ data = some_blocking_library.fetch_items()
320
+ return data
321
+ ```
322
+
323
+ In case of doubt, or by default, use regular `def` functions, those will be run in a threadpool so they don't block the event loop.
324
+
325
+ The same rules apply to dependencies.
326
+
327
+ Make sure blocking code is not run inside of `async` functions. The logic will work, but will damage the performance heavily.
328
+
329
+ When needing to mix blocking and async code, see Asyncer in [the other tools reference](references/other-tools.md).
330
+
331
+ ## Streaming (JSON Lines, SSE, bytes)
332
+
333
+ See [the streaming reference](references/streaming.md) for JSON Lines, Server-Sent Events (`EventSourceResponse`, `ServerSentEvent`), and byte streaming (`StreamingResponse`) patterns.
334
+
335
+ ## Tooling
336
+
337
+ See [the other tools reference](references/other-tools.md) for details on uv, Ruff, ty for package management, linting, type checking, formatting, etc.
338
+
339
+ ## Other Libraries
340
+
341
+ See [the other tools reference](references/other-tools.md) for details on other libraries:
342
+
343
+ * Asyncer for handling async and await, concurrency, mixing async and blocking code, prefer it over AnyIO or asyncio.
344
+ * SQLModel for working with SQL databases, prefer it over SQLAlchemy.
345
+ * HTTPX for interacting with HTTP (other APIs), prefer it over Requests.
346
+
347
+ ## Do not use Pydantic RootModels
348
+
349
+ Do not use Pydantic `RootModel`, instead use regular type annotations with `Annotated` and Pydantic validation utilities.
350
+
351
+ For example, for a list with validations you could do:
352
+
353
+ ```python
354
+ from typing import Annotated
355
+
356
+ from fastapi import Body, FastAPI
357
+ from pydantic import Field
358
+
359
+ app = FastAPI()
360
+
361
+
362
+ @app.post("/items/")
363
+ async def create_items(items: Annotated[list[int], Field(min_length=1), Body()]):
364
+ return items
365
+ ```
366
+
367
+ instead of:
368
+
369
+ ```python
370
+ # DO NOT DO THIS
371
+ from typing import Annotated
372
+
373
+ from fastapi import FastAPI
374
+ from pydantic import Field, RootModel
375
+
376
+ app = FastAPI()
377
+
378
+
379
+ class ItemList(RootModel[Annotated[list[int], Field(min_length=1)]]):
380
+ pass
381
+
382
+
383
+ @app.post("/items/")
384
+ async def create_items(items: ItemList):
385
+ return items
386
+
387
+ ```
388
+
389
+ FastAPI supports these type annotations and will create a Pydantic `TypeAdapter` for them, so that types can work as normally and there's no need for the custom logic and types in RootModels.
390
+
391
+ ## Use one HTTP operation per function
392
+
393
+ Don't mix HTTP operations in a single function, having one function per HTTP operation helps separate concerns and organize the code.
394
+
395
+ Do this:
396
+
397
+ ```python
398
+ from fastapi import FastAPI
399
+ from pydantic import BaseModel
400
+
401
+ app = FastAPI()
402
+
403
+
404
+ class Item(BaseModel):
405
+ name: str
406
+
407
+
408
+ @app.get("/items/")
409
+ async def list_items():
410
+ return []
411
+
412
+
413
+ @app.post("/items/")
414
+ async def create_item(item: Item):
415
+ return item
416
+ ```
417
+
418
+ instead of this:
419
+
420
+ ```python
421
+ # DO NOT DO THIS
422
+ from fastapi import FastAPI, Request
423
+ from pydantic import BaseModel
424
+
425
+ app = FastAPI()
426
+
427
+
428
+ class Item(BaseModel):
429
+ name: str
430
+
431
+
432
+ @app.api_route("/items/", methods=["GET", "POST"])
433
+ async def handle_items(request: Request):
434
+ if request.method == "GET":
435
+ return []
436
+ ```
@@ -0,0 +1,142 @@
1
+ # Dependency Injection
2
+
3
+ Use dependencies when:
4
+
5
+ * They can't be declared in Pydantic validation and require additional logic
6
+ * The logic depends on external resources or could block in any other way
7
+ * Other dependencies need their results (it's a sub-dependency)
8
+ * The logic can be shared by multiple endpoints to do things like error early, authentication, etc.
9
+ * They need to handle cleanup (e.g., DB sessions, file handles), using dependencies with `yield`
10
+ * Their logic needs input data from the request, like headers, query parameters, etc.
11
+
12
+ ## Dependencies with `yield` and `scope`
13
+
14
+ When using dependencies with `yield`, they can have a `scope` that defines when the exit code is run.
15
+
16
+ Use the default scope `"request"` to run the exit code after the response is sent back.
17
+
18
+ ```python
19
+ from typing import Annotated
20
+
21
+ from fastapi import Depends, FastAPI
22
+
23
+ app = FastAPI()
24
+
25
+
26
+ def get_db():
27
+ db = DBSession()
28
+ try:
29
+ yield db
30
+ finally:
31
+ db.close()
32
+
33
+
34
+ DBDep = Annotated[DBSession, Depends(get_db)]
35
+
36
+
37
+ @app.get("/items/")
38
+ async def read_items(db: DBDep):
39
+ return db.query(Item).all()
40
+ ```
41
+
42
+ Use the scope `"function"` when they should run the exit code after the response data is generated but before the response is sent back to the client.
43
+
44
+ ```python
45
+ from typing import Annotated
46
+
47
+ from fastapi import Depends, FastAPI
48
+
49
+ app = FastAPI()
50
+
51
+
52
+ def get_username():
53
+ try:
54
+ yield "Rick"
55
+ finally:
56
+ print("Cleanup up before response is sent")
57
+
58
+ UserNameDep = Annotated[str, Depends(get_username, scope="function")]
59
+
60
+ @app.get("/users/me")
61
+ def get_user_me(username: UserNameDep):
62
+ return username
63
+ ```
64
+
65
+ ## Class Dependencies
66
+
67
+ Avoid creating class dependencies when possible.
68
+
69
+ If a class is needed, instead create a regular function dependency that returns a class instance.
70
+
71
+ Do this:
72
+
73
+ ```python
74
+ from dataclasses import dataclass
75
+ from typing import Annotated
76
+
77
+ from fastapi import Depends, FastAPI
78
+
79
+ app = FastAPI()
80
+
81
+
82
+ @dataclass
83
+ class DatabasePaginator:
84
+ offset: int = 0
85
+ limit: int = 100
86
+ q: str | None = None
87
+
88
+ def get_page(self) -> dict:
89
+ # Simulate a page of data
90
+ return {
91
+ "offset": self.offset,
92
+ "limit": self.limit,
93
+ "q": self.q,
94
+ "items": [],
95
+ }
96
+
97
+
98
+ def get_db_paginator(
99
+ offset: int = 0, limit: int = 100, q: str | None = None
100
+ ) -> DatabasePaginator:
101
+ return DatabasePaginator(offset=offset, limit=limit, q=q)
102
+
103
+
104
+ PaginatorDep = Annotated[DatabasePaginator, Depends(get_db_paginator)]
105
+
106
+
107
+ @app.get("/items/")
108
+ async def read_items(paginator: PaginatorDep):
109
+ return paginator.get_page()
110
+ ```
111
+
112
+ instead of this:
113
+
114
+ ```python
115
+ # DO NOT DO THIS
116
+ from typing import Annotated
117
+
118
+ from fastapi import Depends, FastAPI
119
+
120
+ app = FastAPI()
121
+
122
+
123
+ class DatabasePaginator:
124
+ def __init__(self, offset: int = 0, limit: int = 100, q: str | None = None):
125
+ self.offset = offset
126
+ self.limit = limit
127
+ self.q = q
128
+
129
+ def get_page(self) -> dict:
130
+ # Simulate a page of data
131
+ return {
132
+ "offset": self.offset,
133
+ "limit": self.limit,
134
+ "q": self.q,
135
+ "items": [],
136
+ }
137
+
138
+
139
+ @app.get("/items/")
140
+ async def read_items(paginator: Annotated[DatabasePaginator, Depends()]):
141
+ return paginator.get_page()
142
+ ```
@@ -0,0 +1,76 @@
1
+ # Other Tools
2
+
3
+ ## uv
4
+
5
+ If uv is available, use it to manage dependencies.
6
+
7
+ ## Ruff
8
+
9
+ If Ruff is available, use it to lint and format the code. Consider enabling the FastAPI rules.
10
+
11
+ ## ty
12
+
13
+ If ty is available, use it to check types.
14
+
15
+ ## Asyncer
16
+
17
+ When needing to run blocking code inside of async functions, or async code inside of blocking functions, suggest using Asyncer.
18
+
19
+ Prefer it over AnyIO or asyncio.
20
+
21
+ Install:
22
+
23
+ ```bash
24
+ uv add asyncer
25
+ ```
26
+
27
+ Run blocking sync code inside of async with `asyncify()`:
28
+
29
+ ```python
30
+ from asyncer import asyncify
31
+ from fastapi import FastAPI
32
+
33
+ app = FastAPI()
34
+
35
+
36
+ def do_blocking_work(name: str) -> str:
37
+ # Some blocking I/O operation
38
+ return f"Hello {name}"
39
+
40
+
41
+ @app.get("/items/")
42
+ async def read_items():
43
+ result = await asyncify(do_blocking_work)(name="World")
44
+ return {"message": result}
45
+ ```
46
+
47
+ And run async code inside of blocking sync code with `syncify()`:
48
+
49
+ ```python
50
+ from asyncer import syncify
51
+ from fastapi import FastAPI
52
+
53
+ app = FastAPI()
54
+
55
+
56
+ async def do_async_work(name: str) -> str:
57
+ return f"Hello {name}"
58
+
59
+
60
+ @app.get("/items/")
61
+ def read_items():
62
+ result = syncify(do_async_work)(name="World")
63
+ return {"message": result}
64
+ ```
65
+
66
+ ## SQLModel for SQL databases
67
+
68
+ When working with SQL databases, prefer using SQLModel as it is integrated with Pydantic and will allow declaring data validation with the same models.
69
+
70
+ Prefer it over SQLAlchemy.
71
+
72
+ ## HTTPX
73
+
74
+ Use HTTPX for handling HTTP communication (e.g. with other APIs). It support sync and async usage.
75
+
76
+ Prefer it over Requests.