: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 one line reference per summary item (#95802)
posthog[bot]7645a84c6685
diff --git a/products/ai_observability/backend/api/summarization.py b/products/ai_observability/backend/api/summarization.pyindex 1d687527289f..61633fa169d8 100644--- a/products/ai_observability/backend/api/summarization.py+++ b/products/ai_observability/backend/api/summarization.py@@ -522,7 +522,18 @@ class AIObservabilitySummarizationViewSet(TeamAndOrgViewSetMixin, viewsets.GenerOpenApiExample("Success Response",value={- "summary": "## Summary\n- User initiated conversation with greeting [L5-8]\n- Assistant responded with friendly message [L12-15]\n\n## Interesting Notes\n- Standard greeting pattern with no errors",+ "summary": {+ "title": "User greets the assistant",+ "flow_diagram": "User → Assistant → Response",+ "summary_bullets": [+ {"text": "User initiated conversation with a greeting", "line_refs": "L5"},+ {"text": "Assistant responded with a friendly message", "line_refs": "L12"},+ ],+ "interesting_notes": [+ {"text": "Standard greeting pattern with no errors", "line_refs": ""},+ ],+ },+ "text_repr": "L001: user: hi\nL012: assistant: hello","metadata": {"text_repr_length": 450,"model": "gpt-4.1",diff --git a/products/ai_observability/backend/summarization/llm/schema.py b/products/ai_observability/backend/summarization/llm/schema.pyindex 69e286d95373..10b713aa5813 100644--- a/products/ai_observability/backend/summarization/llm/schema.py+++ b/products/ai_observability/backend/summarization/llm/schema.py@@ -2,7 +2,27 @@Pydantic schema for structured LLM summarization outputs."""-from pydantic import BaseModel, ConfigDict, Field+import re+from typing import Annotated++from pydantic import AfterValidator, BaseModel, ConfigDict, Field++_LINE_REF_RE = re.compile(r"L(\d+)", re.IGNORECASE)+++def _keep_one_line_ref(value: str) -> str:+ """Reduce a line reference to the first `L45` in it, or to an empty string.++ The summary UI links each item to one source line, but models answer with ranges+ (`L28-L31`) and lists although the prompt asks for one reference. OpenAI strict structured+ outputs do not enforce a `pattern`, so the contract holds here, not in the schema the model+ sees.+ """+ match = _LINE_REF_RE.search(value)+ return f"L{match.group(1)}" if match else ""+++LineRef = Annotated[str, AfterValidator(_keep_one_line_ref)]class SummaryBullet(BaseModel):@@ -11,7 +31,7 @@ class SummaryBullet(BaseModel):model_config = ConfigDict(extra="forbid")text: str = Field(description="The bullet point text")- line_refs: str = Field(+ line_refs: LineRef = Field(description="Single line reference like 'L45' pointing to the most relevant line for this bullet")@@ -22,7 +42,7 @@ class InterestingNote(BaseModel):model_config = ConfigDict(extra="forbid")text: str = Field(description="The note text")- line_refs: str = Field(+ line_refs: LineRef = Field(description="Single line reference like 'L45' pointing to the most relevant line, or empty string if no specific line")diff --git a/products/ai_observability/backend/summarization/tests/test_schema.py b/products/ai_observability/backend/summarization/tests/test_schema.pyindex 2db7e1369020..90f2cb8baa8d 100644--- a/products/ai_observability/backend/summarization/tests/test_schema.py+++ b/products/ai_observability/backend/summarization/tests/test_schema.py@@ -15,9 +15,22 @@ class TestSummaryBullet:assert bullet.text == "User greeted the assistant"assert bullet.line_refs == "L5"- def test_line_refs_range(self):- bullet = SummaryBullet(text="Multi-turn conversation", line_refs="L10-15")- assert bullet.line_refs == "L10-15"+ @pytest.mark.parametrize(+ "raw,expected",+ [+ ("L45", "L45"),+ ("L007", "L007"),+ ("", ""),+ ("L28-L31", "L28"),+ ("L10-15", "L10"),+ ("L5, L9, L12", "L5"),+ ("l45", "L45"),+ ("no specific line", ""),+ ],+ )+ def test_line_refs_reduced_to_one_reference(self, raw, expected):+ assert SummaryBullet(text="Multi-turn conversation", line_refs=raw).line_refs == expected+ assert InterestingNote(text="Multi-turn conversation", line_refs=raw).line_refs == expecteddef test_missing_text_raises(self):with pytest.raises(ValidationError) as exc_info: