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.

chore(aio): log the provider's rejection detail on eval report failures (#103477)

posthog[bot]1ea561919491
diff --git a/posthog/temporal/ai_observability/eval_reports/report_agent/graph.py b/posthog/temporal/ai_observability/eval_reports/report_agent/graph.py
index de7b91dd6d94..4aa9170f95b3 100644
--- a/posthog/temporal/ai_observability/eval_reports/report_agent/graph.py
+++ b/posthog/temporal/ai_observability/eval_reports/report_agent/graph.py
@@ -8,6 +8,7 @@ import structlog
from langchain_core.messages import HumanMessage
from langchain_core.runnables import RunnableConfig
from langgraph.prebuilt import create_react_agent
+from openai import APIStatusError
from posthog.llm.gateway_client import team_distinct_id
from posthog.temporal.ai_observability.eval_reports.output_types import get_outcome_definition
@@ -221,6 +222,23 @@ def _validate_agent_output(content: EvalReportContent, handled_ids: set[str] | N
return None
+def _upstream_error_fields(error: Exception) -> dict[str, Any]:
+ """Return the provider's own description of a rejected call, for the agent error log.
+
+ A provider 400 says exactly which field it refused, but the exception string on its own
+ does not carry it, so a rejected call is otherwise undiagnosable after the fact.
+ """
+ if not isinstance(error, APIStatusError):
+ return {}
+ return {
+ "upstream_status": error.status_code,
+ "upstream_code": error.code,
+ "upstream_param": error.param,
+ "upstream_type": error.type,
+ "upstream_message": error.message[:1000],
+ }
+
+
def run_eval_report_agent(
inputs: RunEvalReportAgentInput,
evaluation_target: str = "generation",
@@ -396,6 +414,7 @@ def run_eval_report_agent(
evaluation_id=inputs.evaluation_id,
trace_id=resolved_trace_id,
session_id=resolved_session_id,
+ **_upstream_error_fields(e),
)
return _fallback_content(
inputs.evaluation_name,
diff --git a/posthog/temporal/ai_observability/eval_reports/report_agent/test/test_graph.py b/posthog/temporal/ai_observability/eval_reports/report_agent/test/test_graph.py
index 3c9a9a3e26c8..c5d710f3c933 100644
--- a/posthog/temporal/ai_observability/eval_reports/report_agent/test/test_graph.py
+++ b/posthog/temporal/ai_observability/eval_reports/report_agent/test/test_graph.py
@@ -4,6 +4,8 @@ from unittest.mock import MagicMock, patch
from django.test import SimpleTestCase
+import httpx
+from openai import BadRequestError
from parameterized import parameterized
from posthog.exceptions import ClickHouseAtCapacity
@@ -639,3 +641,43 @@ class TestRunEvalReportAgentInstrumentation(SimpleTestCase):
trace_id="report-run-1",
session_id="report-session-1",
)
+
+ @patch.object(graph.logger, "exception")
+ @patch.object(graph, "build_langchain_callbacks", return_value=[])
+ @patch.object(graph, "create_react_agent")
+ @patch.object(graph, "build_flex_first_chat_client")
+ @patch.object(graph, "_compute_metrics")
+ def test_error_log_preserves_the_upstream_rejection_detail(
+ self, mock_metrics, _mock_build_llm, mock_create_agent, _mock_build_callbacks, mock_logger_exception
+ ) -> None:
+ mock_metrics.return_value = EvalReportMetrics()
+ request = httpx.Request("POST", "https://gateway.invalid/chat/completions")
+ mock_create_agent.return_value.invoke.side_effect = BadRequestError(
+ "Request too large",
+ response=httpx.Response(400, request=request),
+ body={"code": "context_length_exceeded", "param": "messages", "type": "invalid_request_error"},
+ )
+
+ graph.run_eval_report_agent(
+ RunEvalReportAgentInput(
+ team_id=1,
+ report_id="report-1",
+ trace_id="report-run-1",
+ session_id="report-session-1",
+ evaluation_id="eval-1",
+ evaluation_name="Relevance",
+ evaluation_description="",
+ evaluation_prompt="",
+ evaluation_type="llm_judge",
+ period_start="2026-04-08T14:00:00+00:00",
+ period_end="2026-04-08T15:00:00+00:00",
+ previous_period_start="2026-04-08T13:00:00+00:00",
+ )
+ )
+
+ logged = mock_logger_exception.call_args.kwargs
+ self.assertEqual(logged["upstream_status"], 400)
+ self.assertEqual(logged["upstream_code"], "context_length_exceeded")
+ self.assertEqual(logged["upstream_param"], "messages")
+ self.assertEqual(logged["upstream_type"], "invalid_request_error")
+ self.assertEqual(logged["upstream_message"], "Request too large")