: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
feat(data-warehouse): route the custom source builder through the ai-gateway (#124793)
Jonathan Mieloo6d5bf8a3bd03
diff --git a/products/warehouse_sources/backend/presentation/views/external_data_source/source_setup.py b/products/warehouse_sources/backend/presentation/views/external_data_source/source_setup.pyindex e7bff27d61f4..48f3285dd3bf 100644--- a/products/warehouse_sources/backend/presentation/views/external_data_source/source_setup.py+++ b/products/warehouse_sources/backend/presentation/views/external_data_source/source_setup.py@@ -14,8 +14,8 @@ from django.db.models import Qfrom django.utils import timezoneimport temporalio+from anthropic import APIConnectionErrorfrom drf_spectacular.utils import extend_schema, extend_schema_field-from openai import APIConnectionErrorfrom rest_framework import serializers, statusfrom rest_framework.exceptions import ValidationErrorfrom rest_framework.request import Requestdiff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/custom/ai_builder.py b/products/warehouse_sources/backend/temporal/data_imports/sources/custom/ai_builder.pyindex f610c8de0cc0..c0586bf04db4 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/custom/ai_builder.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/custom/ai_builder.py@@ -25,9 +25,9 @@ from typing import Literalimport requestsimport structlog-from openai import OpenAI+from anthropic import Anthropic-from posthog.llm.gateway_client import Product, get_llm_client+from posthog.llm.gateway_client import Product, build_anthropic_client, team_distinct_idfrom products.warehouse_sources.backend.temporal.data_imports.sources.common.http import make_tracked_sessionfrom products.warehouse_sources.backend.temporal.data_imports.sources.custom.source import (@@ -195,8 +195,8 @@ def _collapse_untrusted(text: str) -> str:def extract_manifest_json(content: str) -> dict | None:"""Parse the model's reply into a manifest dict, tolerating fences or surrounding prose.- ``response_format={"type": "json_object"}`` isn't reliably honoured through the gateway's- Anthropic route, so the reply can arrive fenced or with leading text. Try the whole string,+ The Anthropic Messages API has no JSON mode, so the reply can arrive fenced or with leading+ text. Try the whole string,then a fenced block, then the outermost ``{…}`` span. Returns the dict, or None if nothingparses to a JSON object."""@@ -323,22 +323,19 @@ def _validate_manifest(manifest: dict, *, team_id: int) -> tuple[list[str], strreturn [schema.name for schema in schemas], None-def _call_model(*, client: OpenAI, team_id: int, system_prompt: str, user_prompt: str) -> str:+def _call_model(*, client: Anthropic, team_id: int, system_prompt: str, user_prompt: str) -> str:# Bound each call: the SDK defaults to a 600s timeout and automatic retries, so without this one# synchronous draft request could pin a web worker for many minutes. Fail fast and let the draft# loop / caller surface the error instead. (A Temporal-backed async path is the longer-term fix.)- response = client.with_options(timeout=90.0, max_retries=0).chat.completions.create(+ response = client.with_options(timeout=90.0, max_retries=0).messages.create(model=CUSTOM_SOURCE_BUILDER_MODEL,- messages=[- {"role": "system", "content": system_prompt},- {"role": "user", "content": user_prompt},- ],+ system=system_prompt,+ messages=[{"role": "user", "content": user_prompt}],# No `temperature`: it's deprecated/rejected for claude-opus-4-8 (the model fixes it).max_tokens=MAX_OUTPUT_TOKENS,- response_format={"type": "json_object"},- user=f"team-{team_id}",+ metadata={"user_id": team_distinct_id(team_id)},)- return response.choices[0].message.content or ""+ return "".join(block.text for block in response.content if block.type == "text")def draft_manifest_sync(@@ -347,7 +344,7 @@ def draft_manifest_sync(source_name: str,docs_text: str,max_attempts: int = MAX_DRAFT_ATTEMPTS,- client: OpenAI | None = None,+ client: Anthropic | None = None,reference_text: str | None = None,) -> ManifestDraftResult:"""Draft and validate a manifest, repairing against validation errors up to ``max_attempts``.@@ -356,7 +353,16 @@ def draft_manifest_sync(AI-data-processing opt-in before calling: this ships the docs text to the LLM gateway."""reference_text = reference_text if reference_text is not None else load_skill_reference()- client = client if client is not None else get_llm_client(product=CUSTOM_SOURCE_BUILDER_PRODUCT, team_id=team_id)+ client = (+ client+ if client is not None+ else build_anthropic_client(+ product=CUSTOM_SOURCE_BUILDER_PRODUCT,+ ai_product=CUSTOM_SOURCE_BUILDER_PRODUCT,+ distinct_id=team_distinct_id(team_id),+ team_id=team_id,+ )+ )system_prompt = build_system_prompt(reference_text)docs = _bounded_docs(docs_text)diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/custom/test_ai_builder.py b/products/warehouse_sources/backend/temporal/data_imports/sources/custom/test_ai_builder.pyindex 306876aa3936..317576f0ee64 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/custom/test_ai_builder.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/custom/test_ai_builder.py@@ -4,7 +4,7 @@ from typing import castfrom django.test import SimpleTestCaseimport requests-from openai import OpenAI+from anthropic import Anthropicfrom parameterized import parameterizedfrom products.warehouse_sources.backend.temporal.data_imports.sources.custom.ai_builder import (@@ -23,7 +23,7 @@ def _valid_manifest(base_url: str = "https://api.example.com") -> dict:}-class _FakeCompletions:+class _FakeMessages:def __init__(self, responses: list[str]) -> None:self._responses = responsesself.calls: list[dict] = []@@ -32,24 +32,20 @@ class _FakeCompletions:index = min(len(self.calls), len(self._responses) - 1)self.calls.append(kwargs)content = self._responses[index]- return type(- "Resp",- (),- {"choices": [type("Choice", (), {"message": type("Msg", (), {"content": content})})]},- )+ return type("Resp", (), {"content": [type("Block", (), {"type": "text", "text": content})]})class _FakeClient:def __init__(self, responses: list[str]) -> None:- self.chat = type("Chat", (), {"completions": _FakeCompletions(responses)})+ self.messages = _FakeMessages(responses)def with_options(self, **kwargs: object) -> "_FakeClient":- # Mirror the OpenAI client's per-call options override, which returns a configured client.+ # Mirror the Anthropic client's per-call options override, which returns a configured client.return self-def _client(responses: list[str]) -> OpenAI:- return cast(OpenAI, _FakeClient(responses))+def _client(responses: list[str]) -> Anthropic:+ return cast(Anthropic, _FakeClient(responses))class TestExtractManifestJson(SimpleTestCase):diff --git a/products/warehouse_sources/backend/tests/api/test_draft_custom_manifest.py b/products/warehouse_sources/backend/tests/api/test_draft_custom_manifest.pyindex f010eef21c59..f7b0004417ce 100644--- a/products/warehouse_sources/backend/tests/api/test_draft_custom_manifest.py+++ b/products/warehouse_sources/backend/tests/api/test_draft_custom_manifest.py@@ -4,7 +4,7 @@ from unittest.mock import patchfrom django.core.cache import cacheimport httpx-from openai import APIConnectionError+from anthropic import APIConnectionErrorfrom parameterized import parameterizedfrom rest_framework import status