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): accept BOM-prefixed CSV when building a static cohort (#82082)

posthog[bot]7c176f970b1f
diff --git a/posthog/api/cohort.py b/posthog/api/cohort.py
index 2827faf2ea10..74ef0d5fd27e 100644
--- a/posthog/api/cohort.py
+++ b/posthog/api/cohort.py
@@ -555,7 +555,8 @@ class CSVConfig:
PERSON_ID_HEADERS = ["person_id", "person-id", "Person .id"]
DISTINCT_ID_HEADERS = ["distinct_id", "distinct-id"]
EMAIL_HEADERS = ["email", "e-mail"]
- ENCODING = "utf-8"
+ # utf-8-sig strips the byte order mark that Excel and Google Sheets glue onto the first header
+ ENCODING = "utf-8-sig"
class ErrorMessages:
EMPTY_FILE = "CSV file is empty. Please upload a CSV file with at least one row of data."
@@ -1002,12 +1003,14 @@ class CohortSerializer(SearchMatchTypeSerializerMixin, serializers.ModelSerializ
result = self._find_id_column(first_row)
if result is None:
- available_headers = [h for h in first_row if h.strip()]
+ available_headers = [h.strip() for h in first_row if h.strip()]
raise ValidationError(
{
"csv": [
CSVConfig.ErrorMessages.MISSING_ID_COLUMN.format(
- columns=", ".join(available_headers) if available_headers else "none"
+ columns=", ".join(f"'{h}'" for h in available_headers)
+ if available_headers
+ else "none"
)
]
}
diff --git a/posthog/api/test/test_cohort.py b/posthog/api/test/test_cohort.py
index cd1405304b01..36bfcee08eab 100644
--- a/posthog/api/test/test_cohort.py
+++ b/posthog/api/test/test_cohort.py
@@ -929,7 +929,7 @@ Jane Smith,25
response_data = response.json()
self.assertEqual(response_data["attr"], "csv")
self.assertIn("distinct_id", response_data["detail"])
- self.assertIn("name, age", response_data["detail"])
+ self.assertIn("'name', 'age'", response_data["detail"])
self.assertEqual(patch_calculate_cohort_from_list.call_count, 0)
@parameterized.expand([("person-id",), ("person_id",), ("Person .id",)])
@@ -972,6 +972,71 @@ Jane Smith,{person2.uuid},[email protected]
self.assertIn(str(person1.uuid), person_uuids_in_cohort)
self.assertIn(str(person2.uuid), person_uuids_in_cohort)
+ @patch(
+ "posthog.tasks.calculate_cohort.calculate_cohort_from_list.delay",
+ side_effect=calculate_cohort_from_list,
+ )
+ def test_static_cohort_csv_upload_multicolumn_with_bom_prefixed_header(self, patch_calculate_cohort_from_list):
+ person1 = create_person(team=self.team, distinct_ids=["user123"])
+ person2 = create_person(team=self.team, distinct_ids=["user456"])
+
+ csv = SimpleUploadedFile(
+ "excel_export.csv",
+ b"\xef\xbb\xbf"
+ + f"""person_id,email
+{person1.uuid},[email protected]
+{person2.uuid},[email protected]
+""".encode(),
+ content_type="application/csv",
+ )
+
+ response = self.client.post(
+ f"/api/projects/{self.team.id}/cohorts/",
+ {"name": "test_bom_multicolumn", "csv": csv, "is_static": True},
+ format="multipart",
+ )
+
+ self.assertEqual(response.status_code, 201)
+ cohort = Cohort.objects.get(pk=response.json()["id"])
+
+ person_uuids_in_cohort = _cohort_member_uuids(cohort.team_id, cohort)
+ self.assertEqual(count_cohort_members(cohort.team_id, cohort.pk), 2)
+ self.assertIn(str(person1.uuid), person_uuids_in_cohort)
+ self.assertIn(str(person2.uuid), person_uuids_in_cohort)
+
+ @patch(
+ "posthog.tasks.calculate_cohort.calculate_cohort_from_list.delay",
+ side_effect=calculate_cohort_from_list,
+ )
+ def test_static_cohort_csv_upload_single_column_with_bom_prefixed_header(self, patch_calculate_cohort_from_list):
+ person1 = create_person(team=self.team, distinct_ids=["user123"])
+ person2 = create_person(team=self.team, distinct_ids=["user456"])
+
+ csv = SimpleUploadedFile(
+ "excel_single_column.csv",
+ b"\xef\xbb\xbf"
+ + f"""person_id
+{person1.uuid}
+{person2.uuid}
+""".encode(),
+ content_type="application/csv",
+ )
+
+ response = self.client.post(
+ f"/api/projects/{self.team.id}/cohorts/",
+ {"name": "test_bom_single_column", "csv": csv, "is_static": True},
+ format="multipart",
+ )
+
+ self.assertEqual(response.status_code, 201)
+ cohort = Cohort.objects.get(pk=response.json()["id"])
+
+ self.assertEqual(patch_calculate_cohort_from_list.call_args.kwargs["id_type"], "person_id")
+ person_uuids_in_cohort = _cohort_member_uuids(cohort.team_id, cohort)
+ self.assertEqual(count_cohort_members(cohort.team_id, cohort.pk), 2)
+ self.assertIn(str(person1.uuid), person_uuids_in_cohort)
+ self.assertIn(str(person2.uuid), person_uuids_in_cohort)
+
@patch(
"posthog.tasks.calculate_cohort.calculate_cohort_from_list.delay",
side_effect=calculate_cohort_from_list,
@@ -1239,7 +1304,7 @@ Jane Smith,25
self.assertIn("at least one column with a supported ID header", response_data["detail"])
self.assertIn("person_id", response_data["detail"])
self.assertIn("distinct_id", response_data["detail"])
- self.assertIn("name, age", response_data["detail"])
+ self.assertIn("'name', 'age'", response_data["detail"])
@patch("posthog.tasks.calculate_cohort.calculate_cohort_from_list.delay")
def test_static_cohort_csv_upload_empty_file_fails(self, patch_calculate_cohort_from_list):