: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): strip NUL from eval report content before storing (#122218)
Carlos Marchald90ea529052e
diff --git a/posthog/models/utils.py b/posthog/models/utils.pyindex e4da20d0ae7e..ab22010f60f3 100644--- a/posthog/models/utils.py+++ b/posthog/models/utils.py@@ -143,6 +143,26 @@ def namedtuplefetchall(cursor):return [nt_result(*row) for row in cursor.fetchall()]+def strip_null_bytes(value: Any) -> Any:+ """Recursively remove NUL (\\x00) from strings, because Postgres text/jsonb columns cannot store it.++ Anything written to a Postgres text or JSON column that originates outside Postgres (ClickHouse+ query results, LLM output, user-supplied prompts) must pass through this first. Otherwise Django+ serializes the NUL to a unicode escape that Postgres rejects, and the whole write fails with a DataError.+ """+ if isinstance(value, str):+ return value.replace("\x00", "")+ if isinstance(value, list):+ return [strip_null_bytes(v) for v in value]+ if isinstance(value, tuple):+ return tuple(strip_null_bytes(v) for v in value)+ if isinstance(value, dict):+ # Strip keys too: a Map(String, …) column can produce data-derived keys carrying NUL,+ # and Postgres rejects it in a jsonb key just as it does in a value.+ return {strip_null_bytes(k): strip_null_bytes(v) for k, v in value.items()}+ return value++def generate_random_token(nbytes: int = 32) -> str:"""Generate a securely random token.diff --git a/posthog/temporal/ai_observability/eval_reports/activities.py b/posthog/temporal/ai_observability/eval_reports/activities.pyindex ba77dcadb351..38b132a4abed 100644--- a/posthog/temporal/ai_observability/eval_reports/activities.py+++ b/posthog/temporal/ai_observability/eval_reports/activities.py@@ -20,6 +20,7 @@ from posthog.hogql import astfrom posthog.clickhouse.client.connection import Workloadfrom posthog.exceptions import ClickHouseQueryTimeOut+from posthog.models.utils import strip_null_bytesfrom posthog.sync import database_sync_to_asyncfrom posthog.temporal.ai_observability.eval_reports.constants import (COUNT_TRIGGER_CURSOR_SETTLE_LAG,@@ -900,7 +901,8 @@ async def store_report_run_activity(from products.ai_observability.backend.models.evaluation_reports import EvaluationReportRun# Mirror content.metrics into the legacy `metadata` JSONField for consumers that still read it.- content = normalize_report_content_payload(inputs.content or {})+ # The report agent can quote generation text, which can contain NUL.+ content: dict[str, Any] = strip_null_bytes(normalize_report_content_payload(inputs.content or {}))evaluation_target = resolve_evaluation_target(content.get("evaluation_target", GENERATION_TARGET))generation_status = EvalReportGenerationStatus(content["generation_status"])metrics = content.get("metrics")diff --git a/posthog/temporal/ai_observability/eval_reports/test/test_activities.py b/posthog/temporal/ai_observability/eval_reports/test/test_activities.pyindex 78b4401a451d..3ea65d1f18d9 100644--- a/posthog/temporal/ai_observability/eval_reports/test/test_activities.py+++ b/posthog/temporal/ai_observability/eval_reports/test/test_activities.py@@ -284,6 +284,41 @@ async def test_store_legacy_boolean_report_emits_normalized_generic_metrics() ->assert properties["$ai_report_referenced_trace_ids"] == ["customer/trace:42"]+async def test_store_strips_nul_from_report_content() -> None:+ report_run = MagicMock(id="run-id", report_id="report-id")+ content = {+ "title": "Quoted \x00output",+ "sections": [{"title": "Findings", "content": "The model replied \x00 twice\x00"}],+ "citations": [{"generation_id": "generation-id", "trace_id": "trace-id", "reason": "empty\x00 answer"}],+ "metrics": None,+ }+ inputs = StoreReportRunInput(+ report_id="report-id",+ team_id=1,+ evaluation_id="evaluation-id",+ content=content,+ period_start="2026-07-01T00:00:00+00:00",+ period_end="2026-07-02T00:00:00+00:00",+ )++ with (+ patch(+ "products.ai_observability.backend.models.evaluation_reports.EvaluationReportRun.objects.create",+ return_value=report_run,+ ) as create_report_run,+ patch("posthog.models.team.Team.objects.get", return_value=MagicMock()),+ patch("posthog.models.event.util.create_event") as create_event,+ ):+ await store_report_run_activity(inputs)++ stored_content = create_report_run.call_args.kwargs["content"]+ assert stored_content["title"] == "Quoted output"+ assert stored_content["sections"][0]["content"] == "The model replied twice"+ assert stored_content["citations"][0]["reason"] == "empty answer"+ assert create_event.call_args.kwargs["properties"]["$ai_report_content"] == stored_content++@pytest.mark.asyncioasync def test_store_metrics_unavailable_report_omits_placeholder_metrics() -> None:report_run = MagicMock(id="run-id", report_id="report-id")diff --git a/products/subscriptions/backend/temporal/ai_subscription/activities.py b/products/subscriptions/backend/temporal/ai_subscription/activities.pyindex b2f87833cbc0..f1ff458730b3 100644--- a/products/subscriptions/backend/temporal/ai_subscription/activities.py+++ b/products/subscriptions/backend/temporal/ai_subscription/activities.py@@ -13,6 +13,7 @@ from temporalio.exceptions import ApplicationErrorfrom posthog.dataclasses import frozenfrom posthog.models import OrganizationMembership+from posthog.models.utils import strip_null_bytesfrom posthog.ph_client import ph_scoped_capturefrom posthog.sync import database_sync_to_async@@ -36,7 +37,6 @@ from products.subscriptions.backend.temporal.delivery_common import (auto_disable_and_return,deliver_email,deliver_slack,- strip_null_bytes,)from products.subscriptions.backend.temporal.delivery_webhook import deliver_teams_webhookfrom products.subscriptions.backend.temporal.types import (diff --git a/products/subscriptions/backend/temporal/delivery_common.py b/products/subscriptions/backend/temporal/delivery_common.pyindex f17614dfe6d7..e55ea7894900 100644--- a/products/subscriptions/backend/temporal/delivery_common.py+++ b/products/subscriptions/backend/temporal/delivery_common.py@@ -34,26 +34,6 @@ LOGGER = get_logger(__name__)_MAX_ERROR_DETAIL_RESULTS = 50-def strip_null_bytes(value: Any) -> Any:- """Recursively remove NUL (\\x00) from strings — Postgres text/jsonb columns cannot store it.-- Anything written to ``SubscriptionDelivery.content_snapshot`` that originates outside a Postgres- text column (ClickHouse query results, LLM output, user-supplied prompts) must pass through this- first, or the NUL surfaces as a unicode escape that fails the whole delivery write with a DataError.- """- if isinstance(value, str):- return value.replace("\x00", "")- if isinstance(value, list):- return [strip_null_bytes(v) for v in value]- if isinstance(value, tuple):- return tuple(strip_null_bytes(v) for v in value)- if isinstance(value, dict):- # Strip keys too: a Map(String, …) column can produce data-derived keys carrying NUL,- # and Postgres rejects it in a jsonb key just as it does in a value.- return {strip_null_bytes(k): strip_null_bytes(v) for k, v in value.items()}- return value--def error_detail_results(recipient_results: list[RecipientResult]) -> list[dict[str, Any]]:details: list[dict[str, Any]] = [{diff --git a/products/subscriptions/backend/temporal/insight_snapshot.py b/products/subscriptions/backend/temporal/insight_snapshot.pyindex 3169a7ccf5f8..e757be0e5f2f 100644--- a/products/subscriptions/backend/temporal/insight_snapshot.py+++ b/products/subscriptions/backend/temporal/insight_snapshot.py@@ -24,12 +24,12 @@ from posthog.api.services.query import ExecutionModefrom posthog.caching.calculate_results import calculate_cache_key, calculate_for_query_based_insightfrom posthog.caching.insight_result import InsightResult, NothingInCacheResultfrom posthog.models import Team, User+from posthog.models.utils import strip_null_bytesfrom products.dashboards.backend.models.dashboard import Dashboardfrom products.dashboards.backend.models.dashboard_tile import DashboardTilefrom products.product_analytics.backend.facade.models import Insightfrom products.subscriptions.backend.models.subscription import Subscription-from products.subscriptions.backend.temporal.delivery_common import strip_null_bytesfrom products.subscriptions.backend.temporal.types import MISSING_QUERY_ERROR_TYPE, safe_error_messagelogger = structlog.get_logger(__name__)@@ -100,7 +100,7 @@ def _serialize_insight_result(result: InsightResult) -> dict[str, Any]:# over-trigger on literal escape text in the data, which is harmless — the walk then finds none.## The AI-report path scrubs unconditionally instead, since those payloads are small (see- # delivery_common.strip_null_bytes callers).+ # the strip_null_bytes calls in ai_subscription/activities.py).if b"\\u0000" in dumped:parsed = strip_null_bytes(parsed)return parsed