Skip to content

: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.

fix(cohorts): apply person id list limit on static cohort create (#115741)

Patriciob1e51260345a
diff --git a/posthog/api/cohort.py b/posthog/api/cohort.py
index 28163f08d359..a98c96cd14b3 100644
--- a/posthog/api/cohort.py
+++ b/posthog/api/cohort.py
@@ -799,7 +799,12 @@ class CohortSerializer(SearchMatchTypeSerializerMixin, serializers.ModelSerializ
earliest_timestamp_func = earliest_timestamp_func
_create_in_folder = serializers.CharField(required=False, allow_blank=True, write_only=True)
_create_static_person_ids = serializers.ListField(
- required=False, child=serializers.CharField(), write_only=True, default=[]
+ required=False,
+ child=serializers.CharField(),
+ write_only=True,
+ default=[],
+ max_length=DEFAULT_COHORT_INSERT_BATCH_SIZE,
+ help_text="Person UUIDs to add to a new static cohort.",
)
# Explicit filters field with proper OpenAPI schema
diff --git a/posthog/api/test/test_cohort.py b/posthog/api/test/test_cohort.py
index fb7e9dde81ad..8bd42bce7ec6 100644
--- a/posthog/api/test/test_cohort.py
+++ b/posthog/api/test/test_cohort.py
@@ -1,6 +1,7 @@
import json
from datetime import datetime, timedelta
from typing import Any, Optional
+from uuid import uuid4
import time_machine
from posthog.test.base import (
@@ -47,7 +48,7 @@ from posthog.test.persons import create_person
from products.actions.backend.models.action import Action
from products.cohorts.backend.models.backfill import CohortBackfillKind, CohortBackfillTrigger
-from products.cohorts.backend.models.cohort import Cohort, CohortType
+from products.cohorts.backend.models.cohort import DEFAULT_COHORT_INSERT_BATCH_SIZE, Cohort, CohortType
from products.cohorts.backend.models.dependencies import cohort_backfill_pending_key, find_behavioral_cohorts
from products.cohorts.backend.models.util import count_cohort_members, list_cohort_member_ids
from products.exports.backend.api.test.test_exports import TestExportMixin
@@ -1302,6 +1303,20 @@ Jane Smith,user456,[email protected]
self.assertIn(str(person1.uuid), person_uuids_in_cohort)
self.assertIn(str(person2.uuid), person_uuids_in_cohort)
+ def test_static_cohort_with_too_many_manually_added_person_ids(self):
+ response = self.client.post(
+ f"/api/projects/{self.team.id}/cohorts/",
+ {
+ "name": "test_too_many_person_ids",
+ "_create_static_person_ids": [str(uuid4()) for _ in range(DEFAULT_COHORT_INSERT_BATCH_SIZE + 1)],
+ "is_static": True,
+ },
+ )
+
+ self.assertEqual(response.status_code, 400)
+ self.assertEqual(response.json()["attr"], "_create_static_person_ids")
+ self.assertFalse(Cohort.objects.filter(team=self.team, name="test_too_many_person_ids").exists())
+
def test_static_cohort_csv_and_manually_added(self):
"""Test CSV upload with person_id column using async task"""
person1 = create_person(team=self.team, distinct_ids=["user123"])
diff --git a/products/cohorts/frontend/generated/api.schemas.ts b/products/cohorts/frontend/generated/api.schemas.ts
index 01f6f51c6efc..3b26f21523d7 100644
--- a/products/cohorts/frontend/generated/api.schemas.ts
+++ b/products/cohorts/frontend/generated/api.schemas.ts
@@ -440,6 +440,10 @@ export interface CohortApi {
/** How this row matched the `search` query parameter: `exact` (the term is a case-insensitive substring of a searched field) or `similar` (a fuzzy trigram match, returned only when no exact match exists). Null when the list is not filtered by `search`. */
readonly search_match_type: SearchMatchTypeEnumApi | null
_create_in_folder?: string
+ /**
+ * Person UUIDs to add to a new static cohort.
+ * @maxItems 1000
+ */
_create_static_person_ids?: string[]
}
@@ -509,6 +513,10 @@ export interface PatchedCohortApi {
/** How this row matched the `search` query parameter: `exact` (the term is a case-insensitive substring of a searched field) or `similar` (a fuzzy trigram match, returned only when no exact match exists). Null when the list is not filtered by `search`. */
readonly search_match_type?: SearchMatchTypeEnumApi | null
_create_in_folder?: string
+ /**
+ * Person UUIDs to add to a new static cohort.
+ * @maxItems 1000
+ */
_create_static_person_ids?: string[]
}
diff --git a/products/cohorts/frontend/generated/api.zod.ts b/products/cohorts/frontend/generated/api.zod.ts
index c6de9d3519eb..9af270d78e9a 100644
--- a/products/cohorts/frontend/generated/api.zod.ts
+++ b/products/cohorts/frontend/generated/api.zod.ts
@@ -36,6 +36,7 @@ export const cohortsCreateBodyNameMax = 400
export const cohortsCreateBodyDescriptionMax = 1000
export const cohortsCreateBodyCreateStaticPersonIdsDefault = []
+export const cohortsCreateBodyCreateStaticPersonIdsMax = 1000
export const CohortsCreateBody = /* @__PURE__ */ zod.object({
name: zod.string().max(cohortsCreateBodyNameMax).nullish(),
@@ -70,7 +71,11 @@ export const CohortsCreateBody = /* @__PURE__ */ zod.object({
'Type of cohort based on filter complexity\n\n\* `static` - static\n\* `person_property` - person_property\n\* `behavioral` - behavioral\n\* `realtime` - realtime\n\* `analytical` - analytical'
),
_create_in_folder: zod.string().optional(),
- _create_static_person_ids: zod.array(zod.string()).default(cohortsCreateBodyCreateStaticPersonIdsDefault),
+ _create_static_person_ids: zod
+ .array(zod.string())
+ .max(cohortsCreateBodyCreateStaticPersonIdsMax)
+ .default(cohortsCreateBodyCreateStaticPersonIdsDefault)
+ .describe('Person UUIDs to add to a new static cohort.'),
})
export const cohortsUpdateBodyNameMax = 400
@@ -78,6 +83,7 @@ export const cohortsUpdateBodyNameMax = 400
export const cohortsUpdateBodyDescriptionMax = 1000
export const cohortsUpdateBodyCreateStaticPersonIdsDefault = []
+export const cohortsUpdateBodyCreateStaticPersonIdsMax = 1000
export const CohortsUpdateBody = /* @__PURE__ */ zod.object({
name: zod.string().max(cohortsUpdateBodyNameMax).nullish(),
@@ -112,7 +118,11 @@ export const CohortsUpdateBody = /* @__PURE__ */ zod.object({
'Type of cohort based on filter complexity\n\n\* `static` - static\n\* `person_property` - person_property\n\* `behavioral` - behavioral\n\* `realtime` - realtime\n\* `analytical` - analytical'
),
_create_in_folder: zod.string().optional(),
- _create_static_person_ids: zod.array(zod.string()).default(cohortsUpdateBodyCreateStaticPersonIdsDefault),
+ _create_static_person_ids: zod
+ .array(zod.string())
+ .max(cohortsUpdateBodyCreateStaticPersonIdsMax)
+ .default(cohortsUpdateBodyCreateStaticPersonIdsDefault)
+ .describe('Person UUIDs to add to a new static cohort.'),
})
export const cohortsPartialUpdateBodyNameMax = 400
@@ -120,6 +130,7 @@ export const cohortsPartialUpdateBodyNameMax = 400
export const cohortsPartialUpdateBodyDescriptionMax = 1000
export const cohortsPartialUpdateBodyCreateStaticPersonIdsDefault = []
+export const cohortsPartialUpdateBodyCreateStaticPersonIdsMax = 1000
export const CohortsPartialUpdateBody = /* @__PURE__ */ zod.object({
name: zod.string().max(cohortsPartialUpdateBodyNameMax).nullish(),
@@ -154,7 +165,11 @@ export const CohortsPartialUpdateBody = /* @__PURE__ */ zod.object({
'Type of cohort based on filter complexity\n\n\* `static` - static\n\* `person_property` - person_property\n\* `behavioral` - behavioral\n\* `realtime` - realtime\n\* `analytical` - analytical'
),
_create_in_folder: zod.string().optional(),
- _create_static_person_ids: zod.array(zod.string()).default(cohortsPartialUpdateBodyCreateStaticPersonIdsDefault),
+ _create_static_person_ids: zod
+ .array(zod.string())
+ .max(cohortsPartialUpdateBodyCreateStaticPersonIdsMax)
+ .default(cohortsPartialUpdateBodyCreateStaticPersonIdsDefault)
+ .describe('Person UUIDs to add to a new static cohort.'),
})
export const CohortsAddPersonsToStaticCohortPartialUpdateBody = /* @__PURE__ */ zod.object({
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index a6dcbed26178..af181f9a1960 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -23921,6 +23921,10 @@ export namespace Schemas {
/** How this row matched the `search` query parameter: `exact` (the term is a case-insensitive substring of a searched field) or `similar` (a fuzzy trigram match, returned only when no exact match exists). Null when the list is not filtered by `search`. */
readonly search_match_type: SearchMatchTypeEnum | null;
_create_in_folder?: string;
+ /**
+ * Person UUIDs to add to a new static cohort.
+ * @maxItems 1000
+ */
_create_static_person_ids?: string[];
}
@@ -78508,6 +78512,10 @@ export namespace Schemas {
/** How this row matched the `search` query parameter: `exact` (the term is a case-insensitive substring of a searched field) or `similar` (a fuzzy trigram match, returned only when no exact match exists). Null when the list is not filtered by `search`. */
readonly search_match_type?: SearchMatchTypeEnum | null;
_create_in_folder?: string;
+ /**
+ * Person UUIDs to add to a new static cohort.
+ * @maxItems 1000
+ */
_create_static_person_ids?: string[];
}
diff --git a/services/mcp/src/generated/cohorts/api.ts b/services/mcp/src/generated/cohorts/api.ts
index 2d3956f4862c..6889875ab9d8 100644
--- a/services/mcp/src/generated/cohorts/api.ts
+++ b/services/mcp/src/generated/cohorts/api.ts
@@ -56,6 +56,7 @@ export const cohortsCreateBodyFiltersOnePropertiesValuesItemTwoNegationDefault =
export const cohortsCreateBodyFiltersOnePropertiesValuesItemThreeNegationDefault = false
export const cohortsCreateBodyFiltersOnePropertiesValuesItemFourNegationDefault = false
export const cohortsCreateBodyCreateStaticPersonIdsDefault = []
+export const cohortsCreateBodyCreateStaticPersonIdsMax = 1000
export const CohortsCreateBody = () => zod.object({
name: zod.string().max(cohortsCreateBodyNameMax).nullish(),
@@ -182,7 +183,11 @@ export const CohortsCreateBody = () => zod.object({
'Type of cohort based on filter complexity\n\n\* `static` - static\n\* `person_property` - person_property\n\* `behavioral` - behavioral\n\* `realtime` - realtime\n\* `analytical` - analytical'
),
_create_in_folder: zod.string().optional(),
- _create_static_person_ids: zod.array(zod.string()).default(cohortsCreateBodyCreateStaticPersonIdsDefault),
+ _create_static_person_ids: zod
+ .array(zod.string())
+ .max(cohortsCreateBodyCreateStaticPersonIdsMax)
+ .default(cohortsCreateBodyCreateStaticPersonIdsDefault)
+ .describe('Person UUIDs to add to a new static cohort.'),
})
export const CohortsRetrieveParams = () => zod.object({
@@ -211,6 +216,7 @@ export const cohortsPartialUpdateBodyFiltersOnePropertiesValuesItemOneNegationDe
export const cohortsPartialUpdateBodyFiltersOnePropertiesValuesItemTwoNegationDefault = false
export const cohortsPartialUpdateBodyFiltersOnePropertiesValuesItemThreeNegationDefault = false
export const cohortsPartialUpdateBodyFiltersOnePropertiesValuesItemFourNegationDefault = false
+export const cohortsPartialUpdateBodyCreateStaticPersonIdsMax = 1000
export const CohortsPartialUpdateBody = () => zod.object({
name: zod.string().max(cohortsPartialUpdateBodyNameMax).nullish(),
@@ -344,7 +350,11 @@ export const CohortsPartialUpdateBody = () => zod.object({
'Type of cohort based on filter complexity\n\n\* `static` - static\n\* `person_property` - person_property\n\* `behavioral` - behavioral\n\* `realtime` - realtime\n\* `analytical` - analytical'
),
_create_in_folder: zod.string().optional(),
- _create_static_person_ids: zod.array(zod.string()).optional(),
+ _create_static_person_ids: zod
+ .array(zod.string())
+ .max(cohortsPartialUpdateBodyCreateStaticPersonIdsMax)
+ .optional()
+ .describe('Person UUIDs to add to a new static cohort.'),
})
export const CohortsAddPersonsToStaticCohortPartialUpdateParams = () => zod.object({