: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(llma): return 400 instead of 500 on invalid Hog syntax in evaluations (#61621)
posthog[bot]6f2696e357a0
diff --git a/products/ai_observability/backend/api/test/test_evaluations.py b/products/ai_observability/backend/api/test/test_evaluations.pyindex cde668acb53b..98ec5d66180f 100644--- a/products/ai_observability/backend/api/test/test_evaluations.py+++ b/products/ai_observability/backend/api/test/test_evaluations.py@@ -376,6 +376,24 @@ class TestEvaluationConfigsApi(APIBaseTest):self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)self.assertEqual(response.data["attr"], "config")+ def test_invalid_hog_source_returns_400_not_500(self):+ # Malformed Hog source passes serializer config validation (non-empty source) but fails+ # to compile in the model's save(). The compile failure must surface as a 400 validation+ # error, not an unhandled 500 — `|` is the exact character that triggered this in production.+ response = self.client.post(+ f"/api/environments/{self.team.id}/evaluations/",+ {+ "name": "Bad Hog Eval",+ "evaluation_type": "hog",+ "evaluation_config": {"source": "return |"},+ "output_type": "boolean",+ "output_config": {},+ },+ )+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ self.assertEqual(response.data["attr"], "evaluation_config")+ self.assertEqual(Evaluation.objects.count(), 0)+def test_deleted_evaluation_configs_not_returned(self):evaluation_config = Evaluation.objects.create(name="Deleted Evaluation",diff --git a/products/ai_observability/backend/models/evaluations.py b/products/ai_observability/backend/models/evaluations.pyindex 7cadc6e94732..2e04809248b5 100644--- a/products/ai_observability/backend/models/evaluations.py+++ b/products/ai_observability/backend/models/evaluations.py@@ -1,10 +1,13 @@-from django.core.exceptions import ValidationErrorfrom django.db import models, transactionfrom django.db.models.signals import post_savefrom django.dispatch import receiverimport structlog+# DRF's ValidationError (not Django's) so save() raise sites surface as 400s via the DRF exception+# handler — Django's variant falls through to the unhandled-exception path and returns a 500.+from rest_framework.exceptions import ValidationError+from posthog.models.activity_logging.model_activity import ModelActivityMixinfrom posthog.models.utils import UUIDTModeldiff --git a/products/ai_observability/backend/models/test/test_evaluations.py b/products/ai_observability/backend/models/test/test_evaluations.pyindex 5c80f4446123..062300c15c8c 100644--- a/products/ai_observability/backend/models/test/test_evaluations.py+++ b/products/ai_observability/backend/models/test/test_evaluations.py@@ -1,9 +1,8 @@from posthog.test.base import BaseTestfrom unittest.mock import patch-from django.core.exceptions import ValidationError-from parameterized import parameterized+from rest_framework.exceptions import ValidationErrorfrom products.ai_observability.backend.models.evaluations import Evaluation, EvaluationStatus, EvaluationStatusReason