API Docs Generator
Audits API endpoint documentation for completeness, generates enhanced docstrings with
proper parameter descriptions and examples, documents all response codes, and produces
Pydantic model examples — bridging the gap between auto-generated OpenAPI specs and
genuinely useful API documentation.
Reference Files
| File |
Contents |
Load When |
references/fastapi-patterns.md |
FastAPI-specific documentation patterns, Path/Query/Body parameter docs |
FastAPI endpoint |
references/example-generation.md |
Creating realistic field examples, model_config patterns |
Example values needed |
references/response-codes.md |
Standard HTTP response documentation, error response schemas |
Response documentation needed |
references/openapi-enhancement.md |
OpenAPI spec enrichment, tag organization, schema documentation |
OpenAPI spec review |
Prerequisites
- Access to the API source code (route definitions, models)
- Framework identification (FastAPI, Flask, Django REST, Express)
Workflow
Phase 1: Analyze Endpoints
- Inventory endpoints — List all routes with HTTP method, path, handler function.
- Identify models — Request bodies (Pydantic models, dataclasses), response models,
query parameters, path parameters.
- Map dependencies — Authentication requirements, middleware, shared dependencies.
- Read existing docs — Current docstrings, OpenAPI metadata, inline documentation.
Phase 2: Audit Documentation
For each endpoint, check:
| Check |
What to Verify |
Common Gap |
| Endpoint description |
Handler has a docstring |
Missing or "TODO" |
| Parameter descriptions |
Each param has description= |
Path params undocumented |
| Request example |
Body model has example= or json_schema_extra |
No request example |
| Response model |
response_model= specified |
Returns raw dict |
| Error responses |
4xx/5xx documented with responses= |
Only 200 documented |
| Tags |
Endpoint assigned to a tag group |
Untagged endpoints |
Phase 3: Generate Enhancements
- Docstrings — Write clear endpoint descriptions that explain purpose, not
implementation. Include Raises section for documented errors.
- Parameter metadata — Add
description, example, ge/le/regex to
Path, Query, Body parameters.
- Model examples — Add
Field(example=...) and model_config with json_schema_extra.
- Error responses — Document every possible error status code with response schema.
- Tags — Group endpoints by resource or feature area.
Phase 4: Output
Produce a coverage report and enhanced code.
Output Format
## API Documentation Audit
### Coverage Summary
| Metric | Count | Documented | Coverage |
|--------|-------|------------|----------|
| Endpoints | {N} | {M} | {%} |
| Parameters | {N} | {M} | {%} |
| Response codes | {N} | {M} | {%} |
| Models with examples | {N} | {M} | {%} |
### Gaps Identified
| # | Endpoint | Issue | Severity |
|---|----------|-------|----------|
| 1 | `{METHOD} {path}` | {issue} | {High/Medium/Low} |
### Enhanced Code
#### `{METHOD} {path}`
```python
@router.{method}(
"{path}",
response_model={ResponseModel},
summary="{Short summary}",
responses={{
404: {{"description": "{Not found description}"}},
422: {{"description": "Validation error"}},
}},
tags=["{tag}"],
)
async def {handler}(
{param}: {type} = Path(..., description="{description}", example={example}),
) -> {ResponseModel}:
"""
{Full description of what this endpoint does.}
{Additional context about behavior, side effects, or important notes.}
Raises:
404: {Entity} not found
403: Insufficient permissions
"""
Model: {ModelName}
class {ModelName}(BaseModel):
{field}: {type} = Field(..., description="{description}", example={example})
model_config = ConfigDict(
json_schema_extra={{
"example": {{
"{field}": {example_value},
}}
}}
)
## Calibration Rules
1. **Describe behavior, not implementation.** "Retrieves the user's profile" is good.
"Calls `db.query(User).filter_by(id=id).first()`" is implementation leakage.
2. **Realistic examples.** `"alice@example.com"` not `"string"`. `42` not `0`.
Examples serve as documentation — they should look like real data.
3. **Document every error code.** If the endpoint can return 404, document it. Users
should never encounter an undocumented error response.
4. **Consistent style.** All endpoints in the same API should use the same documentation
patterns — same tag naming, same description style, same example format.
5. **Don't duplicate the type system.** If the parameter type is `int`, don't write
"An integer" as the description. Write what the integer represents: "Unique user
identifier."
## Error Handling
| Problem | Resolution |
|---------|------------|
| Non-FastAPI framework | Adapt patterns. Document the HTTP contract regardless of framework. |
| No type hints on handlers | Infer types from usage, document uncertainty, suggest adding type hints. |
| Massive API (50+ endpoints) | Prioritize undocumented and public endpoints. Batch output by resource. |
| Generated API (OpenAPI → code) | Document at the spec level, not the generated code level. |
| Authentication varies by endpoint | Document auth requirements per endpoint group. |
## When NOT to Generate
Push back if:
- The API design itself is wrong (bad URL patterns, wrong HTTP methods) — fix the API first
- The user wants SDK generation from OpenAPI — different tool
- The code is a prototype that will change significantly — document after stabilization
1---2name: api-docs-generator3description: Audits and enhances FastAPI and REST API documentation: missing descriptions, response codes, examples, docstrings, Pydantic models, OpenAPI spec. Triggers on: "generate API docs", "document this API", "OpenAPI for", "FastAPI docs", "document endpoints", "swagger docs".4---56# API Docs Generator78Audits API endpoint documentation for completeness, generates enhanced docstrings with9proper parameter descriptions and examples, documents all response codes, and produces10Pydantic model examples — bridging the gap between auto-generated OpenAPI specs and11genuinely useful API documentation.1213## Reference Files1415| File | Contents | Load When |16| ----------------------------------- | ----------------------------------------------------------------------- | ----------------------------- |17| `references/fastapi-patterns.md` | FastAPI-specific documentation patterns, Path/Query/Body parameter docs | FastAPI endpoint |18| `references/example-generation.md` | Creating realistic field examples, model_config patterns | Example values needed |19| `references/response-codes.md` | Standard HTTP response documentation, error response schemas | Response documentation needed |20| `references/openapi-enhancement.md` | OpenAPI spec enrichment, tag organization, schema documentation | OpenAPI spec review |2122## Prerequisites2324- Access to the API source code (route definitions, models)25- Framework identification (FastAPI, Flask, Django REST, Express)2627## Workflow2829### Phase 1: Analyze Endpoints30311. **Inventory endpoints** — List all routes with HTTP method, path, handler function.322. **Identify models** — Request bodies (Pydantic models, dataclasses), response models,33 query parameters, path parameters.343. **Map dependencies** — Authentication requirements, middleware, shared dependencies.354. **Read existing docs** — Current docstrings, OpenAPI metadata, inline documentation.3637### Phase 2: Audit Documentation3839For each endpoint, check:4041| Check | What to Verify | Common Gap |42| ---------------------- | ------------------------------------------------ | ------------------------ |43| Endpoint description | Handler has a docstring | Missing or "TODO" |44| Parameter descriptions | Each param has `description=` | Path params undocumented |45| Request example | Body model has `example=` or `json_schema_extra` | No request example |46| Response model | `response_model=` specified | Returns raw dict |47| Error responses | 4xx/5xx documented with `responses=` | Only 200 documented |48| Tags | Endpoint assigned to a tag group | Untagged endpoints |4950### Phase 3: Generate Enhancements51521. **Docstrings** — Write clear endpoint descriptions that explain purpose, not53 implementation. Include Raises section for documented errors.542. **Parameter metadata** — Add `description`, `example`, `ge`/`le`/`regex` to55 Path, Query, Body parameters.563. **Model examples** — Add `Field(example=...)` and `model_config` with `json_schema_extra`.574. **Error responses** — Document every possible error status code with response schema.585. **Tags** — Group endpoints by resource or feature area.5960### Phase 4: Output6162Produce a coverage report and enhanced code.6364## Output Format6566````67## API Documentation Audit6869### Coverage Summary70| Metric | Count | Documented | Coverage |71|--------|-------|------------|----------|72| Endpoints | {N} | {M} | {%} |73| Parameters | {N} | {M} | {%} |74| Response codes | {N} | {M} | {%} |75| Models with examples | {N} | {M} | {%} |7677### Gaps Identified7879| # | Endpoint | Issue | Severity |80|---|----------|-------|----------|81| 1 | `{METHOD} {path}` | {issue} | {High/Medium/Low} |8283### Enhanced Code8485#### `{METHOD} {path}`8687```python88@router.{method}(89 "{path}",90 response_model={ResponseModel},91 summary="{Short summary}",92 responses={{93 404: {{"description": "{Not found description}"}},94 422: {{"description": "Validation error"}},95 }},96 tags=["{tag}"],97)98async def {handler}(99 {param}: {type} = Path(..., description="{description}", example={example}),100) -> {ResponseModel}:101 """102 {Full description of what this endpoint does.}103104 {Additional context about behavior, side effects, or important notes.}105106 Raises:107 404: {Entity} not found108 403: Insufficient permissions109 """110````111112#### Model: `{ModelName}`113114```python115class {ModelName}(BaseModel):116 {field}: {type} = Field(..., description="{description}", example={example})117118 model_config = ConfigDict(119 json_schema_extra={{120 "example": {{121 "{field}": {example_value},122 }}123 }}124 )125```126127```text128129## Calibration Rules1301311. **Describe behavior, not implementation.** "Retrieves the user's profile" is good.132 "Calls `db.query(User).filter_by(id=id).first()`" is implementation leakage.1332. **Realistic examples.** `"alice@example.com"` not `"string"`. `42` not `0`.134 Examples serve as documentation — they should look like real data.1353. **Document every error code.** If the endpoint can return 404, document it. Users136 should never encounter an undocumented error response.1374. **Consistent style.** All endpoints in the same API should use the same documentation138 patterns — same tag naming, same description style, same example format.1395. **Don't duplicate the type system.** If the parameter type is `int`, don't write140 "An integer" as the description. Write what the integer represents: "Unique user141 identifier."142143## Error Handling144145| Problem | Resolution |146|---------|------------|147| Non-FastAPI framework | Adapt patterns. Document the HTTP contract regardless of framework. |148| No type hints on handlers | Infer types from usage, document uncertainty, suggest adding type hints. |149| Massive API (50+ endpoints) | Prioritize undocumented and public endpoints. Batch output by resource. |150| Generated API (OpenAPI → code) | Document at the spec level, not the generated code level. |151| Authentication varies by endpoint | Document auth requirements per endpoint group. |152153## When NOT to Generate154155Push back if:156- The API design itself is wrong (bad URL patterns, wrong HTTP methods) — fix the API first157- The user wants SDK generation from OpenAPI — different tool158- The code is a prototype that will change significantly — document after stabilization159```