: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
fix(aio): break evaluation report list sort ties on id (#101840)
posthog[bot]dec0faf3c981
diff --git a/products/ai_observability/backend/api/evaluation_reports.py b/products/ai_observability/backend/api/evaluation_reports.pyindex 60fd8f5a930c..ce34b3a7b16e 100644--- a/products/ai_observability/backend/api/evaluation_reports.py+++ b/products/ai_observability/backend/api/evaluation_reports.py@@ -643,7 +643,7 @@ class EvaluationReportViewSet(TeamAndOrgViewSetMixin, ForbidDestroyModel, viewsegenerated_report_count=Count("runs"),last_generated_at=Max("runs__created_at"),)- .order_by("-created_at"),+ .order_by("-created_at", "id"),)if not is_service_auth(self.request):visible_evaluation_ids = self.user_access_control.filter_queryset_by_access_level(@@ -772,7 +772,7 @@ class EvaluationReportViewSet(TeamAndOrgViewSetMixin, ForbidDestroyModel, viewsedef runs(self, request: Request, **kwargs) -> Response:"""List report runs (history) for this report."""report = self.get_object()- queryset = EvaluationReportRun.objects.filter(report=report).order_by("-created_at")+ queryset = EvaluationReportRun.objects.filter(report=report).order_by("-created_at", "id")page = self.paginate_queryset(queryset)if page is not None:serializer = EvaluationReportRunSerializer(page, many=True)diff --git a/products/ai_observability/backend/api/test/test_evaluation_reports.py b/products/ai_observability/backend/api/test/test_evaluation_reports.pyindex b2badd2acf2e..14bc557a9266 100644--- a/products/ai_observability/backend/api/test/test_evaluation_reports.py+++ b/products/ai_observability/backend/api/test/test_evaluation_reports.py@@ -5,7 +5,9 @@ import time_machinefrom posthog.test.base import APIBaseTestfrom unittest.mock import MagicMock, patch+from django.db import connectionfrom django.test import SimpleTestCase+from django.test.utils import CaptureQueriesContextfrom django.utils import timezonefrom parameterized import parameterized@@ -299,6 +301,38 @@ class TestEvaluationReportApi(APIBaseTest):self.assertEqual(results_by_id[str(report_without_runs.id)]["generated_report_count"], 0)self.assertIsNone(results_by_id[str(report_without_runs.id)]["last_generated_at"])+ @parameterized.expand(+ [+ ("report_configs", EvaluationReport._meta.db_table, ""),+ ("report_runs", EvaluationReportRun._meta.db_table, "{report_id}/runs/"),+ ]+ )+ def test_paginated_list_orders_by_a_unique_tie_breaker(self, _name: str, table: str, path_suffix: str) -> None:+ # created_at is not unique, so offset pagination skips or repeats tied rows unless the+ # query also orders by id.+ report = self._create_report()+ EvaluationReportRun.objects.create(+ report=report,+ content={},+ metadata={},+ period_start=timezone.now() - dt.timedelta(hours=1),+ period_end=timezone.now(),+ )+ url = self.base_url + path_suffix.format(report_id=report.id)++ with CaptureQueriesContext(connection) as captured:+ response = self.client.get(url)++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ ordered_selects = [+ query["sql"]+ for query in captured.captured_queries+ if f'FROM "{table}"' in query["sql"] and "ORDER BY" in query["sql"]+ ]+ self.assertTrue(ordered_selects, f"no ordered select on {table} was captured")+ order_by = ordered_selects[-1].split("ORDER BY")[-1].split("LIMIT")[0]+ self.assertRegex(order_by, rf'"{table}"\."created_at" DESC.*"{table}"\."id" ASC')+def test_list_filters_by_evaluation(self) -> None:report = self._create_report()self._create_report(evaluation=self._create_boolean_evaluation())diff --git a/products/ai_observability/backend/migrations/0050_alter_evaluationreportrun_options.py b/products/ai_observability/backend/migrations/0050_alter_evaluationreportrun_options.pynew file mode 100644index 000000000000..2c38f354bb0c--- /dev/null+++ b/products/ai_observability/backend/migrations/0050_alter_evaluationreportrun_options.py@@ -0,0 +1,16 @@+# Generated by Django 5.2.17 on 2026-09-16 17:41++from django.db import migrations+++class Migration(migrations.Migration):+ dependencies = [+ ("ai_observability", "0049_llmpromptdependency"),+ ]++ operations = [+ migrations.AlterModelOptions(+ name="evaluationreportrun",+ options={"ordering": ["-created_at", "id"]},+ ),+ ]diff --git a/products/ai_observability/backend/migrations/max_migration.txt b/products/ai_observability/backend/migrations/max_migration.txtindex ad7a807018e4..f7b364cf86ad 100644--- a/products/ai_observability/backend/migrations/max_migration.txt+++ b/products/ai_observability/backend/migrations/max_migration.txt@@ -1 +1 @@-0049_llmpromptdependency+0050_alter_evaluationreportrun_optionsdiff --git a/products/ai_observability/backend/models/evaluation_reports.py b/products/ai_observability/backend/models/evaluation_reports.pyindex f6807f2c28af..def7cfb7f99b 100644--- a/products/ai_observability/backend/models/evaluation_reports.py+++ b/products/ai_observability/backend/models/evaluation_reports.py@@ -190,7 +190,7 @@ class EvaluationReportRun(UUIDTModel):class Meta:db_table = "llm_analytics_evaluationreportrun"- ordering = ["-created_at"]+ ordering = ["-created_at", "id"]indexes = [models.Index(fields=["report", "-created_at"]),]