: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): keep transient llm judge errors out of error tracking (#99884)
posthog[bot]6e7aa3135ca2
diff --git a/posthog/temporal/ai_observability/evaluation_llm_judge.py b/posthog/temporal/ai_observability/evaluation_llm_judge.pyindex 15be9f320179..6b34453ef95f 100644--- a/posthog/temporal/ai_observability/evaluation_llm_judge.py+++ b/posthog/temporal/ai_observability/evaluation_llm_judge.py@@ -31,6 +31,7 @@ from posthog.temporal.ai_observability.metrics import (increment_user_errors,)from posthog.temporal.ai_observability.model_resolution import model_spec+from posthog.temporal.common.errors import NonReportableErrorfrom posthog.temporal.common.utils import close_db_connectionsfrom products.ai_observability.backend.llm import DEFAULT_MODEL_BY_PROVIDER, Client, CompletionRequest@@ -61,6 +62,22 @@ LLM_JUDGE_RETRY_POLICY = RetryPolicy()+class TransientJudgeError(NonReportableError):+ """A transient transport failure that reached the judge, wrapped to keep it out of error tracking.++ A connection reset interrupts the judge at whatever line it reached, so each occurrence+ fingerprints differently and error tracking files a new issue for it. The Temporal retry policy+ already covers it. This class is a plain exception, not an `ApplicationError`, so the activity+ failure stays retryable.++ The marker class is what keeps it quiet. The worker interceptor wraps the activity from+ outside its decorators and reports every exception it does not recognise, so opting the+ activity out of automatic capture is not enough on its own. `NonReportableError` is one of the+ types the interceptor re-raises untouched. A worker drain needs no marker, because the+ interceptor skips cancellations already.+ """++class BooleanEvalResult(BaseModel):"""Structured output for boolean evaluation results"""@@ -205,7 +222,9 @@ def _build_context_window_skip_result(@temporalio.activity.defn@close_db_connections+# capture_exceptions=False: the worker interceptor reports judge failures, and its capture carries+# the team and evaluation ids. A capture inside the activity wins the SDK's dedupe and loses them.[email protected](capture_exceptions=False)def execute_llm_judge_activity(inputs: ExecuteLLMJudgeInputs) -> EvaluationActivityResult:"""Execute LLM judge to evaluate the target event.@@ -413,17 +432,17 @@ def call_llm_judge(increment_errors("context_window_exceeded", provider=provider)return _build_context_window_skip_result(allows_na, is_byok=is_byok, key_id=key_id)- except ProviderConnectionError:+ except ProviderConnectionError as e:# Transient transport failure (connection reset, read timeout). Retrying usually succeeds,- # so track it as a metric and re-raise for the retry policy — without the logger.exception- # that would clutter error tracking with a non-actionable issue.+ # so track it as a metric and re-raise for the retry policy. `TransientJudgeError` keeps it+ # out of error tracking, where a per-occurrence fingerprint files a new issue every time.increment_errors("connection_error", provider=provider)- raise+ raise TransientJudgeError(str(e)) from eexcept temporalio.exceptions.CancelledError:- # A worker drain or a workflow cancel interrupts the judge at whatever line it reached, so- # the fingerprint differs per cancellation. Logging it would file a new error tracking issue- # every time, so track it as a metric and re-raise for the retry policy instead.+ # A worker drain or a workflow cancel is not a judge failure, so track it as a metric and+ # re-raise for the retry policy. The worker interceptor skips cancellations, so it stays+ # out of error tracking.increment_errors("cancelled", provider=provider)raisediff --git a/posthog/temporal/ai_observability/run_session_evaluation.py b/posthog/temporal/ai_observability/run_session_evaluation.pyindex 85cdf69d7836..5d012d9b2b7f 100644--- a/posthog/temporal/ai_observability/run_session_evaluation.py+++ b/posthog/temporal/ai_observability/run_session_evaluation.py@@ -424,7 +424,9 @@ class ExecuteSessionEvaluationInputs:@temporalio.activity.defn@close_db_connections+# capture_exceptions=False: the worker interceptor reports judge failures, and its capture carries+# the team and evaluation ids. A capture inside the activity wins the SDK's dedupe and loses them.[email protected](capture_exceptions=False)def execute_session_llm_judge_activity(inputs: ExecuteSessionEvaluationInputs) -> EvaluationActivityResult:"""Fetch the whole session and run the LLM judge over its transcript.diff --git a/posthog/temporal/ai_observability/run_trace_evaluation.py b/posthog/temporal/ai_observability/run_trace_evaluation.pyindex 62d479b886e1..cf9acd2ce6c0 100644--- a/posthog/temporal/ai_observability/run_trace_evaluation.py+++ b/posthog/temporal/ai_observability/run_trace_evaluation.py@@ -553,7 +553,9 @@ def build_trace_hog_globals(trace: LLMTrace, trace_id: str, *, bytecode: list[An@temporalio.activity.defn@close_db_connections+# capture_exceptions=False: the worker interceptor reports judge failures, and its capture carries+# the team and evaluation ids. A capture inside the activity wins the SDK's dedupe and loses them.[email protected](capture_exceptions=False)def execute_trace_llm_judge_activity(inputs: ExecuteTraceEvaluationInputs) -> EvaluationActivityResult:"""Fetch the whole trace and run the LLM judge over its transcript.diff --git a/posthog/temporal/ai_observability/test_run_evaluation.py b/posthog/temporal/ai_observability/test_run_evaluation.pyindex 675dc737fcf0..7b10a5f48fbb 100644--- a/posthog/temporal/ai_observability/test_run_evaluation.py+++ b/posthog/temporal/ai_observability/test_run_evaluation.py@@ -6,6 +6,7 @@ from typing import Anyimport pytestfrom unittest.mock import AsyncMock, MagicMock, patch+import posthoganalyticsfrom asgiref.sync import sync_to_asyncfrom parameterized import parameterizedfrom temporalio import activity@@ -24,6 +25,7 @@ from products.ai_observability.backend.llm.errors import (ContextWindowExceededError,ModelNotFoundError,ModelPermissionError,+ ProviderConnectionError,QuotaExceededError,RateLimitError,StructuredOutputParseError,@@ -40,7 +42,7 @@ from .evaluation_errors import (status_reason_detail_for_terminal_user_error,terminal_user_error_result_from_application_error,)-from .evaluation_llm_judge import JUDGE_EVENT_MAX_CHARS, _execute_llm_judge_activity+from .evaluation_llm_judge import JUDGE_EVENT_MAX_CHARS, TransientJudgeError, _execute_llm_judge_activityfrom .evaluation_workflow_activities import LocalEvaluationOutcome, backfill_verdict_timestampfrom .run_evaluation import (BooleanEvalResult,@@ -1748,6 +1750,51 @@ class TestRunEvaluationWorkflow:mock_increment_errors.assert_called_once_with("cancelled", provider="openai")mock_logger.exception.assert_not_called()+ @pytest.mark.parametrize(+ "raised_exception, expected_raised",+ [+ pytest.param(ProviderConnectionError("connection reset"), TransientJudgeError, id="connection_error"),+ pytest.param(CancelledError("Cancelled"), CancelledError, id="cancellation"),+ pytest.param(RuntimeError("boom"), RuntimeError, id="unhandled_error"),+ ],+ )+ @pytest.mark.django_db(transaction=True)+ def test_execute_llm_judge_activity_leaves_error_capture_to_the_interceptor(+ self,+ raised_exception: Exception,+ expected_raised: type[Exception],+ setup_data,+ active_key_config,+ ):+ evaluation_obj = setup_data["evaluation"]+ team = setup_data["team"]++ evaluation = {+ "id": str(evaluation_obj.id),+ "name": "Test Evaluation",+ "evaluation_type": "llm_judge",+ "evaluation_config": {"prompt": "Is this accurate?"},+ "output_type": "boolean",+ "output_config": {},+ "team_id": team.id,+ }+ event_data = create_mock_event_data(team.id)++ with (+ patch("posthog.temporal.ai_observability.evaluation_llm_judge.Client") as mock_client_class,+ patch.object(posthoganalytics, "capture_exception") as mock_capture_exception,+ patch.object(posthoganalytics, "default_client", None),+ patch.object(posthoganalytics, "enable_exception_autocapture", True),+ ):+ mock_client = MagicMock()+ mock_client_class.return_value = mock_client+ mock_client.complete.side_effect = raised_exception++ with pytest.raises(expected_raised):+ execute_llm_judge_activity(ExecuteLLMJudgeInputs(evaluation=evaluation, event_data=event_data))++ mock_capture_exception.assert_not_called()+@pytest.mark.django_db(transaction=True)def test_execute_llm_judge_activity_terminal_team_requires_provider_key(self, setup_data):team = setup_data["team"]