:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(ci): link enum-collision fix path from openapi schema errors (#59272)
Julian Bezad0e72515007
diff --git a/.agents/skills/improving-drf-endpoints/SKILL.md b/.agents/skills/improving-drf-endpoints/SKILL.mdindex 82df7475b12f..01f397ccac95 100644--- a/.agents/skills/improving-drf-endpoints/SKILL.md+++ b/.agents/skills/improving-drf-endpoints/SKILL.md@@ -51,20 +51,21 @@ Work through this list for every serializer and viewset you touch.3. **No bare `JSONField()`** — create a custom field class with `@extend_schema_field(TypedSchema)`4. **`SerializerMethodField` has `@extend_schema_field`** on its `get_*` method5. **`ChoiceField` has explicit `choices=`** with all valid values listed-6. **Read vs write serializers are separate** when input shape differs from output-7. **Every success response is backed by a serializer** — returning raw dicts or untyped lists means no generated types downstream+6. **Avoid collision-prone enum field names** — `format`, `type`, `status`, `kind`, `level`, `mode`, `state`, `platform`, `provider` clash with existing choices and fail CI under `--fail-on-warn`; pick a specific name or add an `ENUM_NAME_OVERRIDES` entry up front (see [serializer-fields.md](references/serializer-fields.md#choicefield--explicit-choices))+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 downstreamSee [serializer-fields.md](references/serializer-fields.md) for patterns and examples.### Viewset and action annotations-8. **Every custom `@action` has `@extend_schema` or `@validated_request`** — without it, drf-spectacular discovers zero parameters-9. **Plain `ViewSet` methods have schema annotations** — `ModelViewSet` with `serializer_class` is auto-discovered; plain `ViewSet` is not-10. **`@extend_schema` is on the actual method** (`get`, `post`, `create`, `list`), not on a helper or the class itself-11. **Error responses are typed** — use `OpenApiResponse(response=ErrorSerializer)`, not `OpenApiTypes.OBJECT`-12. **List endpoints declare pagination** — reset with `pagination_class=None` on custom actions that don't paginate-13. **Prefer `@validated_request`** over manual `serializer.is_valid()` + `@extend_schema` — it handles both in one decorator-14. **ViewSets outside `products/` need `@extend_schema(tags=["<product>"])`** — ViewSets in `products/<name>/backend/` are auto-tagged via module path, but ViewSets in `posthog/api/` or `ee/` are not. Without the tag, the MCP scaffold and frontend type generator can't route the endpoint to the right product+9. **Every custom `@action` has `@extend_schema` or `@validated_request`** — without it, drf-spectacular discovers zero parameters+10. **Plain `ViewSet` methods have schema annotations** — `ModelViewSet` with `serializer_class` is auto-discovered; plain `ViewSet` is not+11. **`@extend_schema` is on the actual method** (`get`, `post`, `create`, `list`), not on a helper or the class itself+12. **Error responses are typed** — use `OpenApiResponse(response=ErrorSerializer)`, not `OpenApiTypes.OBJECT`+13. **List endpoints declare pagination** — reset with `pagination_class=None` on custom actions that don't paginate+14. **Prefer `@validated_request`** over manual `serializer.is_valid()` + `@extend_schema` — it handles both in one decorator+15. **ViewSets outside `products/` need `@extend_schema(tags=["<product>"])`** — ViewSets in `products/<name>/backend/` are auto-tagged via module path, but ViewSets in `posthog/api/` or `ee/` are not. Without the tag, the MCP scaffold and frontend type generator can't route the endpoint to the right product**Streaming endpoints:** For SSE or streaming responses, use `@extend_schema(request=InputSerializer, responses={(200, "text/event-stream"): OpenApiTypes.STR})` to document the request schema even though the response can't be fully typed.diff --git a/.agents/skills/improving-drf-endpoints/references/serializer-fields.md b/.agents/skills/improving-drf-endpoints/references/serializer-fields.mdindex 6025948c8c68..56fca8934ef7 100644--- a/.agents/skills/improving-drf-endpoints/references/serializer-fields.md+++ b/.agents/skills/improving-drf-endpoints/references/serializer-fields.md@@ -129,6 +129,16 @@ status = serializers.ChoiceField()```+**Collision-prone field names.** Generic names like `format`, `type`, `status`,+`kind`, `level`, `mode`, `state`, `platform`, `provider` already exist on multiple+components with different choices. Reusing one for a new ChoiceField means+drf-spectacular auto-names your enum (`Format5eaEnum` and similar), which fails CI+under `--fail-on-warn`. Either pick a more specific field name, or add a stable+name to `ENUM_NAME_OVERRIDES` in `posthog/settings/web.py` when you introduce the+field. Run `python manage.py find_enum_collisions` after the change — it prints+a suggested override entry (pastable as-is for type-hint enum collisions; for+ChoiceField collisions you fill in the Choices/Enum class path).+## DictField — typed values```pythondiff --git a/.github/workflows/ci-backend.yml b/.github/workflows/ci-backend.ymlindex 2f08dc60531d..7fca214c38d9 100644--- a/.github/workflows/ci-backend.yml+++ b/.github/workflows/ci-backend.yml@@ -1161,7 +1161,25 @@ jobs:PR_NUMBER: ${{ github.event.pull_request.number }}GH_TOKEN: ${{ steps.openapi-app-token.outputs.token }}run: |- ./bin/hogli build:openapi+ if ! ./bin/hogli build:openapi; then+ echo ""+ echo "::error::OpenAPI schema generation failed."+ echo ""+ echo "If the failure above mentions \"enum naming encountered a non-optimally"+ echo "resolvable collision\" or \"SchemaGenerationError: Failing as requested due"+ echo "to warnings\", drf-spectacular found a problem in a serializer/viewset and"+ echo "\`--fail-on-warn\` turned it into an error."+ echo ""+ echo "Diagnose locally:"+ echo " python manage.py find_enum_collisions # prints a suggested override entry to add"+ echo " hogli build:openapi-schema # to reproduce the full warning set"+ echo ""+ echo "Where to fix:"+ echo " posthog/settings/web.py # ENUM_NAME_OVERRIDES (see comment block at the top)"+ echo " /improving-drf-endpoints # invoke this skill for the full serializer/viewset guide"+ echo ""+ exit 1+ fipnpm --filter=@posthog/mcp run scaffold-yaml -- --sync-allif git diff --exit-code; thenecho "OpenAPI types are up to date"diff --git a/posthog/management/commands/find_enum_collisions.py b/posthog/management/commands/find_enum_collisions.pyindex 2148b24de1fb..346e266561eb 100644--- a/posthog/management/commands/find_enum_collisions.py+++ b/posthog/management/commands/find_enum_collisions.py@@ -3,6 +3,16 @@Replicates the collision detection logic from drf_spectacular.hooks.postprocess_schema_enumsbut prints actionable output instead of opaque warnings.+Run this when `hogli build:openapi-schema` fails with messages like+"enum naming encountered a non-optimally resolvable collision" / "Format5eaEnum" — it+prints a suggested ENUM_NAME_OVERRIDES entry for posthog/settings/web.py+(pastable as-is for type-hint enum collisions; for ChoiceField collisions you fill in+the Choices/Enum class path).++See also:+ posthog/settings/web.py — ENUM_NAME_OVERRIDES (where the fix goes)+ /improving-drf-endpoints — skill with the full DRF/OpenAPI guide+Usage:python manage.py find_enum_collisions"""@@ -135,18 +145,19 @@ class Command(BaseCommand):self.stdout.write(f" - {comp}.{field}")self.stdout.write("")- self.stdout.write(" Suggested override for ENUM_NAME_OVERRIDES in web.py:")+ self.stdout.write(" Override entry to add to ENUM_NAME_OVERRIDES in web.py")+ self.stdout.write(" (key defaults to the current auto-resolved name; rename for a nicer schema type):")if c["has_spec_id"]:- self.stdout.write(' "YourEnumName": "your.models.module.Model.ChoicesClass",')- self.stdout.write(" # ChoiceField path — override must be a model class path")+ self.stdout.write(f' "{c["auto_name"]}": "your.models.module.Model.ChoicesClass",')+ self.stdout.write(" # ChoiceField path — fill in the actual Choices/Enum class path")else:vals = c["values"]if all(isinstance(v, int) for v in vals):formatted = [(v, v) for v in vals]- self.stdout.write(f' "YourEnumName": {formatted},')+ self.stdout.write(f' "{c["auto_name"]}": {formatted},')else:- self.stdout.write(f' "YourEnumName": {json.dumps(vals)},')- self.stdout.write(" # Type-hint path — override must be an inline value list")+ self.stdout.write(f' "{c["auto_name"]}": {json.dumps(vals)},')+ self.stdout.write(" # Type-hint path — paste as-is to silence the warning")self.stdout.write("\n ---\n")all_hashes = set()diff --git a/posthog/settings/web.py b/posthog/settings/web.pyindex 26c80aa4894a..1418b295ee59 100644--- a/posthog/settings/web.py+++ b/posthog/settings/web.py@@ -401,7 +401,28 @@ SPECTACULAR_SETTINGS = {"posthog.api.documentation.lint_spec_consistency_hook",],"ENUM_NAME_OVERRIDES": {- # Overrides fall into two categories depending on how drf-spectacular hashes them:+ # If CI is failing with "enum naming encountered a non-optimally resolvable+ # collision" / "Format5eaEnum"-style warnings from `hogli build:openapi-schema`,+ # this is the dict you need to add an entry to. The warning fails CI because+ # `--fail-on-warn` is set on the `spectacular` invocation in hogli.yaml.+ #+ # Workflow to resolve a collision:+ # 1. Run `python manage.py find_enum_collisions` — it prints the field name,+ # the auto-generated name (e.g. `Format5eaEnum`), the enum values, which+ # components share the hash, and a suggested override entry. For type-hint+ # enum collisions the suggested line is pastable as-is; for ChoiceField+ # collisions you supply the Choices/Enum class path.+ # 2. Add the suggested entry below (pick the right category — see "hash trap"+ # note below). Optionally rename the key from the auto-generated name to a+ # more semantic one to improve the generated schema type's name.+ # 3. Re-run `hogli build:openapi-schema` locally to confirm the warning is gone.+ #+ # Full guide (when to use which pattern, anti-patterns, MCP/typegen implications):+ # /improving-drf-endpoints (skill — invoke it for the walkthrough)+ #+ # Hash trap — overrides fall into two categories depending on how drf-spectacular+ # hashes them, and using the wrong format silently fails (the override is ignored+ # and the warning persists):## 1. Model class paths — used for ChoiceField-backed enums where drf-spectacular# injects x-spec-enum-id from (value, label) tuples. The override must point@@ -411,8 +432,6 @@ SPECTACULAR_SETTINGS = {# return types) where there is NO x-spec-enum-id. Postprocessing hashes these# as (value, value) tuples, so the override must also be a plain value list# (which _load_enum_name_overrides normalizes to (value, value)).- #- # Getting this wrong means the override hash doesn't match and the warning persists.# --- Model class paths (ChoiceField x-spec-enum-id hashes) ---"RestrictionLevelEnum": "products.dashboards.backend.models.dashboard.Dashboard.RestrictionLevel","OrganizationMembershipLevelEnum": "posthog.models.organization.OrganizationMembership.Level",