Skip to content
Skillpedia
APIs & servers (backend)41k Β· #6815

improving-drf-endpoints

What it does

This skill guides the AI through reviewing and fixing Django REST Framework endpoints and serializers. It is for developers who want their API documentation and generated code to be accurate.

When to use it

  • βœ“When you are editing a Django REST Framework serializer or viewset and want its fields typed correctly.
  • βœ“When generated API types show unknown or wrong values and you need to find the cause.
  • βœ“When you are reviewing an API change and want to check its field descriptions and schema.

Skill link

https://github.com/PostHog/posthog/blob/master/.agents/skills/improving-drf-endpoints/SKILL.md

Copy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.

How to use

The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.

Ready-to-paste message

Use this skill to help me: https://github.com/PostHog/posthog/blob/master/.agents/skills/improving-drf-endpoints/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
Want your AI to find skills like this on its own?Install the free Skillpedia MCP: one line, and it searches 15,000+ skills before each task.Install in 1 minute β†’

Example of what to ask your AI

β€œHelp me review my Django API serializers so the generated docs and types show the right fields and descriptions.”

5 new skills a week. 1 minute to read.

How it works

It checks each field's type, help text and schema notes, because these details flow into the API documentation and the code generated from it. It then suggests fixes to the serializer code.

Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

Improving DRF Endpoints

Before you propose a contract test against the generated OpenAPI schema, check things already tried. Six PRs took that idea, and none merged.

Overview

Serializer fields are the source of truth for PostHog's entire type pipeline:

Django serializer β†’ drf-spectacular β†’ OpenAPI JSON β†’ Orval β†’ Zod schemas β†’ MCP tools

Every help_text, every field type, every @extend_schema annotation flows downstream. A missing help_text means an agent guessing at parameters. A bare ListField() means z.unknown() in the generated Zod schema. Getting the serializer right means every consumer β€” frontend types, MCP tools, API docs β€” gets correct types and descriptions automatically.

When to use

  • Editing or reviewing any file that defines a Serializer or ViewSet
  • Fixing OpenAPI spec warnings or generated type issues
  • Preparing an endpoint for MCP tool exposure
  • Code review of API changes

Audit checklist

Triage: check the generated output first

Before diving into Python, look at the committed generated types to see what's broken. Find the generated files for the endpoint's product:

  • Core API: frontend/src/generated/core/
  • Product APIs: products/<product>/frontend/generated/

Each has two files:

  • api.schemas.ts β€” TypeScript interfaces derived from serializers. Search for the serializer name and look for unknown types (bare ListField/JSONField), missing JSDoc descriptions (missing help_text), or overly generic Record<string, unknown> shapes.
  • api.ts β€” API client functions. Check if the endpoint's operation exists at all β€” if missing, the viewset method likely lacks @extend_schema.

This tells you exactly which fields and endpoints to prioritize.

Serializer fields

Work through this list for every serializer and viewset you touch.

  1. Every field has help_text β€” describes purpose, format, constraints, valid values
  2. No bare ListField() or DictField() β€” always specify child= with a typed serializer or field
  3. No bare JSONField() β€” create a custom field class with @extend_schema_field(TypedSchema)
  4. SerializerMethodField has @extend_schema_field on its get_* method
  5. ChoiceField has explicit choices= with all valid values listed
  6. Define choices as a class β€” models.TextChoices in a product's internal modules, LabeledStrEnum or LabeledIntEnum from posthog/enums.py in facade contract files (backend/facade/contracts.py, backend/facade/enums.py), which must not import Django. The OpenAPI component is named after the class either way (EarlyAccessFeature.Stage -> EarlyAccessFeatureStageEnum, via ChoicesEnumNameOverrides in posthog/openapi/enum_names.py), so a class-backed enum never collides on field names like format, type, status, kind. Pass X.choices to choices=, and to name a labeled enum on a SerializerMethodField use @extend_schema_field(ChoiceField(choices=X.choices)), not a return type hint. Inline choices=[...] lists have no class to read, collide with existing choices, and fail CI under --fail-on-warn; an explicit ENUM_NAME_OVERRIDES entry in posthog/settings/web.py is the fallback for choice sets no class can carry (see serializer-fields.md)
  7. Read vs write serializers are separate when input shape differs from output
  8. Every success response is backed by a serializer β€” returning raw dicts or untyped lists means no generated types downstream

See serializer-fields.md for patterns and examples.

Viewset and action annotations

  1. Every custom @action has @extend_schema or @validated_request β€” without it, drf-spectacular discovers zero parameters
  2. Plain ViewSet methods have schema annotations β€” ModelViewSet with serializer_class is auto-discovered; plain ViewSet is not
  3. @extend_schema is on the actual method (get, post, create, list), not on a helper or the class itself
  4. Error responses are typed β€” use OpenApiResponse(response=ErrorSerializer), not OpenApiTypes.OBJECT
  5. List endpoints declare pagination β€” reset with pagination_class=None on custom actions that don't paginate
  6. Prefer @validated_request over manual serializer.is_valid() + @extend_schema β€” it handles both in one decorator
  7. **ViewSets o
FAQ

Do I need to know Django to use it?

Yes. It is meant for developers who already work with Django REST Framework. It explains what to check, but you still need to understand the code.

What is the main benefit?

Clear help text and correct field types mean the generated API docs and client code describe each endpoint accurately.

Does it work with any API?

It is aimed at Django REST Framework APIs that generate their schema with drf-spectacular.

Details

Original description by the author

Use when editing, reviewing, or auditing DRF viewsets and serializers in PostHog. Triggers on files in posthog/api/, products/*/backend/api/, products/*/backend/presentation/, or any file importing rest_framework. Covers field typing, schema annotations, enum collision fixes, and OpenAPI spec quality β€” everything that flows downstream into generated TypeScript types and MCP tools.

repo: PostHog/posthog

Popularity

GitHub stars
41k
Ranking position
#6815
License
β€”
Updated
11/2/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills