: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(signals): stabilize finding response schema (#72958)
Alessandro Pogliaghi0c2bcbe02fe1
diff --git a/products/signals/backend/report_generation/research.py b/products/signals/backend/report_generation/research.pyindex ded99aac3e9f..a5326a8c2b39 100644--- a/products/signals/backend/report_generation/research.py+++ b/products/signals/backend/report_generation/research.py@@ -147,11 +147,11 @@ class ReportResearchOutput(BaseModel):class SignalFindingUpdate(BaseModel):- """Re-research response for a signal that already has a finding: confirm it or replace it."""+ """Stable per-signal response envelope for both new research and re-research."""previous_finding_correct: bool = Field(- description="True when the previous finding is still accurate as-is. It will be kept unchanged "- "and no new finding recorded."+ description="True when the previous finding is still accurate as-is. Set false when there is no previous "+ "finding or when it needs replacing.")finding: SignalFinding | None = Field(default=None,@@ -419,10 +419,7 @@ def build_initial_research_prompt() -> str:"""Build the opening prompt for the first signal in a multi-turn research session."""signal_block = _render_signal_for_research(first_signal, 1, total_signals)- # With a previous finding the agent answers with the update wrapper, so it can confirm the- # existing finding instead of regenerating it.- finding_model = SignalFindingUpdate if previous_finding else SignalFinding- finding_schema = json.dumps(finding_model.model_json_schema(), indent=2)+ finding_schema = json.dumps(SignalFindingUpdate.model_json_schema(), indent=2)report_context = ""if title or summary:@@ -438,11 +435,12 @@ def build_initial_research_prompt(investigation_instruction = ("You will investigate **{total_signals} signal(s)** one at a time. I will send each signal in a separate ""message. For signals with previous findings, validate them lightly first and reuse them if they still "- "hold. Investigate genuinely new or stale signals thoroughly, then respond with a `SignalFinding` JSON "- "object."+ "hold. Investigate genuinely new or stale signals thoroughly, then respond with the finding response "+ "envelope described below."if previous_report_id or previous_findingelse "You will investigate **{total_signals} signal(s)** one at a time. I will send each signal in a "- "separate message. For each one, investigate it thoroughly then respond with a `SignalFinding` JSON object."+ "separate message. For each one, investigate it thoroughly then respond with the finding response envelope "+ "described below.")bk_block = f"\n{_BUSINESS_KNOWLEDGE_BLOCK}\n" if has_business_knowledge else ""@@ -464,6 +462,8 @@ def build_initial_research_prompt({signal_block}{previous_finding_context}+{"There is no previous finding for this signal. Set `previous_finding_correct` to `false` and put the new result in `finding`." if previous_finding is None else "Use the previous-finding instructions above to confirm or replace it."}+---## Output format@@ -484,8 +484,7 @@ def build_signal_investigation_prompt() -> str:"""Build a follow-up prompt for signal N (2..total)."""signal_block = _render_signal_for_research(signal, index, total)- finding_model = SignalFindingUpdate if previous_finding else SignalFinding- finding_schema = json.dumps(finding_model.model_json_schema(), indent=2)+ finding_schema = json.dumps(SignalFindingUpdate.model_json_schema(), indent=2)previous_finding_context = _render_previous_finding_context(previous_finding)return f"""## Signal {index} of {total}@@ -493,6 +492,8 @@ def build_signal_investigation_prompt({signal_block}{previous_finding_context}+{"There is no previous finding for this signal. Set `previous_finding_correct` to `false` and put the new result in `finding`." if previous_finding is None else "Use the previous-finding instructions above to confirm or replace it."}+---If this signal substantially overlaps with one you already investigated, reference your earlier finding and focus only on what's new or different — don't re-investigate the same code paths and data.@@ -700,13 +701,10 @@ async def run_multi_turn_research(resolved_report_title=resolved_report_title,resolved_report_summary=resolved_report_summary,)- first_schema: type[SignalFinding] | type[SignalFindingUpdate] = (- SignalFindingUpdate if first_previous else SignalFinding- )session, first_response = await MultiTurnSession.start(prompt=initial_prompt,context=context,- model=first_schema,+ model=SignalFindingUpdate,branch=branch,step_name="report_research",verbose=verbose,@@ -762,12 +760,9 @@ async def run_multi_turn_research(total,previous_finding=previous_finding,)- followup_schema: type[SignalFinding] | type[SignalFindingUpdate] = (- SignalFindingUpdate if previous_finding else SignalFinding- )response = await session.send_followup(followup_prompt,- followup_schema,+ SignalFindingUpdate,label=f"signal_{i}_of_{total}",)finding, is_new = _resolve_finding_response(response, previous_finding, signal.signal_id)diff --git a/products/signals/backend/test/test_research_prompt.py b/products/signals/backend/test/test_research_prompt.pyindex 1455f4c2b5fe..c87fe33a2732 100644--- a/products/signals/backend/test/test_research_prompt.py+++ b/products/signals/backend/test/test_research_prompt.py@@ -3,8 +3,10 @@ from datetime import datetimeimport pytestfrom products.signals.backend.report_generation.research import (+ SignalFinding,_render_signal_for_research,build_initial_research_prompt,+ build_signal_investigation_prompt,)from products.signals.backend.temporal.types import SignalData@@ -102,3 +104,28 @@ class TestBuildInitialResearchPrompt:signal = _make_signal({})prompt = build_initial_research_prompt(signal, 1)assert "## Previously resolved report" not in prompt++ @pytest.mark.parametrize("has_previous_finding", [False, True])+ def test_uses_stable_finding_response_envelope(self, has_previous_finding):+ signal = _make_signal({})+ previous_finding = (+ SignalFinding(+ signal_id=signal.signal_id,+ relevant_code_paths=["example.py"],+ data_queried="Queried the relevant events.",+ verified=True,+ )+ if has_previous_finding+ else None+ )++ initial_prompt = build_initial_research_prompt(signal, 2, previous_finding=previous_finding)+ followup_prompt = build_signal_investigation_prompt(signal, 2, 2, previous_finding=previous_finding)++ for prompt in (initial_prompt, followup_prompt):+ assert '"previous_finding_correct"' in prompt+ assert '"finding"' in prompt+ assert "respond with a `SignalFinding` JSON object" not in prompt+ if not has_previous_finding:+ assert "There is no previous finding for this signal" in initial_prompt+ assert "There is no previous finding for this signal" in followup_prompt