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.

fix(subscriptions): forward resolution errors to HogQL repair LLM (#62301)

Vasco de Krijger206ae102b8cd
diff --git a/products/exports/backend/temporal/subscriptions/ai_subscription/report_pipeline.py b/products/exports/backend/temporal/subscriptions/ai_subscription/report_pipeline.py
index 4a1b5b4319bc..f4d489d3139e 100644
--- a/products/exports/backend/temporal/subscriptions/ai_subscription/report_pipeline.py
+++ b/products/exports/backend/temporal/subscriptions/ai_subscription/report_pipeline.py
@@ -1,4 +1,3 @@
-import re
import uuid
import asyncio
from datetime import UTC, datetime
@@ -9,7 +8,7 @@ import structlog
from posthog.schema import AssistantHogQLQuery
-from posthog.hogql.errors import ExposedHogQLError, InternalHogQLError
+from posthog.hogql.errors import ExposedHogQLError, InternalHogQLError, ResolutionError
from posthog.exceptions_capture import capture_exception
from posthog.models import Team, User
@@ -23,6 +22,7 @@ from products.exports.backend.temporal.subscriptions.ai_subscription.prompts imp
HOGQL_FIX_PROMPT,
HOGQL_FIX_PROMPT_NAME,
SYNTHESIS_PROMPT_NAME,
+ render_prompt,
resolve_prompt,
)
from products.exports.backend.temporal.subscriptions.ai_subscription.schemas import (
@@ -255,8 +255,14 @@ async def _run_steps(
)
fixed = await _arequest_hogql_fix(
original_hogql=current_hogql,
- # don't forward internal error text to the LLM — it can echo cluster URLs/table names
- error_message=str(exc) if isinstance(exc, ExposedHogQLError) else type(exc).__name__,
+ # Forward the message for exposed errors and ResolutionError. ResolutionError messages
+ # describe query structure — usually the field/property the planner itself referenced
+ # (e.g. "Unable to resolve field 'operaton'"), which is what the fixer needs. A few raise
+ # sites wrap a nested exception, but those describe query shape, not cluster topology, so
+ # the leak risk stays low. Other internal errors (parsing/impossible-AST) stay type-only.
+ error_message=(
+ str(exc) if isinstance(exc, (ExposedHogQLError, ResolutionError)) else type(exc).__name__
+ ),
step_description=safe_description,
team=team,
user=user,
@@ -306,18 +312,12 @@ async def _arequest_hogql_fix(
posthog_properties=posthog_properties,
).with_structured_output(HogQLFix, method="json_schema", include_raw=False)
- substitutions = {
- "description": step_description,
- "error": error_message,
- "original_hogql": original_hogql,
- }
fix_prompt = await database_sync_to_async(resolve_prompt, thread_sensitive=False)(
team, HOGQL_FIX_PROMPT_NAME, HOGQL_FIX_PROMPT
)
- rendered = re.sub(
- r"\{\{\{(\w+)\}\}\}",
- lambda m: substitutions.get(m.group(1), m.group(0)),
+ rendered = render_prompt(
fix_prompt,
+ {"description": step_description, "error": error_message, "original_hogql": original_hogql},
)
try: