: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(growth): route ai enrichment labelling through the ai-gateway (#123407)
Jonathan Mieloo6309612ab2ec
diff --git a/products/growth/backend/api/ai_enrichment.py b/products/growth/backend/api/ai_enrichment.pyindex 92801db8eff9..4ac2e6e995cf 100644--- a/products/growth/backend/api/ai_enrichment.py+++ b/products/growth/backend/api/ai_enrichment.py@@ -35,7 +35,7 @@ from posthog.api.utils import ErrorResponseSerializerfrom posthog.exceptions import Conflictfrom posthog.exceptions_capture import capture_exceptionfrom posthog.helpers.impersonation import is_impersonated-from posthog.llm.gateway_client import get_llm_client+from posthog.llm.gateway_client import build_openai_clientfrom posthog.models import Userfrom posthog.permissions import IsStaffUserfrom posthog.renderers import SafeJSONRenderer@@ -58,6 +58,7 @@ from products.growth.backend.enrichment.lab import (stream_run_classifications,)from products.growth.backend.enrichment.labels import (+ AI_ENRICHMENT_FEATURE,UNKNOWN,recent_latest_fetches_qs,signup_domain_for_organization,@@ -237,7 +238,11 @@ class AIEnrichmentViewSet(viewsets.ViewSet):# stop_after_attempt(3)); the SDK's own internal retries underneath would multiply that# budget several-fold per fetch and actively worsen a 429 the tenacity layer is already# backing off from.- client = get_llm_client(product="growth").with_options(max_retries=0)+ client = build_openai_client(+ product="growth",+ ai_product="growth",+ properties={"ai_feature": AI_ENRICHMENT_FEATURE, "surface": "staff_run"},+ ).with_options(max_retries=0)logger.info("growth_ai_enrichment_run",diff --git a/products/growth/backend/api/test_ai_enrichment.py b/products/growth/backend/api/test_ai_enrichment.pyindex 99bc4d84a2b4..f673468850d5 100644--- a/products/growth/backend/api/test_ai_enrichment.py+++ b/products/growth/backend/api/test_ai_enrichment.py@@ -40,7 +40,7 @@ def _mock_llm_client(verdict: bool = True, confidence: float = 0.9, reasoning: str = "builds ai software", verdict_key: str = "is_ai") -> MagicMock:client = MagicMock()- # The run action chains .with_options(max_retries=0) onto get_llm_client(...); without this,+ # The run action chains .with_options(max_retries=0) onto build_openai_client(...); without this,# that call returns a fresh, unconfigured child mock and every assertion below silently# checks the wrong object (see enrichment_label_batch.py for the same pattern).client.with_options.return_value = client@@ -188,7 +188,7 @@ class TestAIEnrichmentAPI(APIBaseTest):fake_client = MagicMock()fake_client.models.list.return_value = [MagicMock(id="gpt-5.2"), MagicMock(id="claude-fable-5")]- with patch("products.growth.backend.enrichment.lab.get_llm_client", return_value=fake_client):+ with patch("products.growth.backend.enrichment.lab.build_openai_client", return_value=fake_client):response = self.client.get("/api/growth_ai_enrichment/models/")self.assertEqual(response.status_code, status.HTTP_200_OK)@@ -196,7 +196,9 @@ class TestAIEnrichmentAPI(APIBaseTest):self.assertEqual(ids, {"gpt-5.2", "claude-fable-5"})def test_models_returns_empty_rather_than_a_stale_list_when_the_gateway_is_down(self):- with patch("products.growth.backend.enrichment.lab.get_llm_client", side_effect=RuntimeError("gateway down")):+ with patch(+ "products.growth.backend.enrichment.lab.build_openai_client", side_effect=RuntimeError("gateway down")+ ):response = self.client.get("/api/growth_ai_enrichment/models/")# No curated mirror: a hand-maintained fallback goes stale silently.@@ -422,7 +424,7 @@ class TestAIEnrichmentAPI(APIBaseTest):client = _mock_llm_client()with (- patch("products.growth.backend.api.ai_enrichment.get_llm_client", return_value=client),+ patch("products.growth.backend.api.ai_enrichment.build_openai_client", return_value=client),patch("products.growth.backend.enrichment.lab.ai_processing_approved", return_value=False),):response = self.client.post(@@ -513,7 +515,7 @@ class TestAIEnrichmentAPI(APIBaseTest):)client = _mock_llm_client()- with patch("products.growth.backend.api.ai_enrichment.get_llm_client", return_value=client) as get_client:+ with patch("products.growth.backend.api.ai_enrichment.build_openai_client", return_value=client) as get_client:response = self.client.post("/api/growth_ai_enrichment/run/",{@@ -528,7 +530,11 @@ class TestAIEnrichmentAPI(APIBaseTest):)_drain_ndjson(response.streaming_content) # type: ignore[attr-defined]- get_client.assert_called_once_with(product="growth")+ get_client.assert_called_once_with(+ product="growth",+ ai_product="growth",+ properties={"ai_feature": "ai_enrichment", "surface": "staff_run"},+ )client.with_options.assert_called_once_with(max_retries=0)def test_run_is_throttled_for_session_auth(self):@@ -547,7 +553,7 @@ class TestAIEnrichmentAPI(APIBaseTest):"sample": 1,}- with patch("products.growth.backend.api.ai_enrichment.get_llm_client", return_value=_mock_llm_client()):+ with patch("products.growth.backend.api.ai_enrichment.build_openai_client", return_value=_mock_llm_client()):statuses = []for _ in range(11):response = self.client.post("/api/growth_ai_enrichment/run/", payload, format="json")@@ -591,7 +597,7 @@ class TestAIEnrichmentRunClassification(NonAtomicAPIBaseTest):payload={"name": "Acme", "contactEmail": "[email protected]"},)- with patch("products.growth.backend.api.ai_enrichment.get_llm_client", return_value=_mock_llm_client()):+ with patch("products.growth.backend.api.ai_enrichment.build_openai_client", return_value=_mock_llm_client()):response = self.client.post("/api/growth_ai_enrichment/run/",{@@ -633,7 +639,7 @@ class TestAIEnrichmentRunClassification(NonAtomicAPIBaseTest):payload={"companyFound": False, "name": "Acme"},)- with patch("products.growth.backend.api.ai_enrichment.get_llm_client", return_value=_mock_llm_client()):+ with patch("products.growth.backend.api.ai_enrichment.build_openai_client", return_value=_mock_llm_client()):response = self.client.post("/api/growth_ai_enrichment/run/",{@@ -658,7 +664,7 @@ class TestAIEnrichmentRunClassification(NonAtomicAPIBaseTest):organization=self.organization, provider="harmonic", payload={"companyFound": False})- with patch("products.growth.backend.api.ai_enrichment.get_llm_client", return_value=_mock_llm_client()):+ with patch("products.growth.backend.api.ai_enrichment.build_openai_client", return_value=_mock_llm_client()):response = self.client.post("/api/growth_ai_enrichment/run/",{@@ -692,7 +698,7 @@ class TestAIEnrichmentRunClassification(NonAtomicAPIBaseTest):)client = _mock_llm_client()- with patch("products.growth.backend.api.ai_enrichment.get_llm_client", return_value=client):+ with patch("products.growth.backend.api.ai_enrichment.build_openai_client", return_value=client):response = self.client.post("/api/growth_ai_enrichment/run/",{@@ -729,7 +735,7 @@ class TestAIEnrichmentRunClassification(NonAtomicAPIBaseTest):# classify_fetch_for_run runs in a thread-pool worker that only executes (and only then# calls capture_exception) once something actually iterates streaming_content.with (- patch("products.growth.backend.api.ai_enrichment.get_llm_client", return_value=failing_client),+ patch("products.growth.backend.api.ai_enrichment.build_openai_client", return_value=failing_client),patch("products.growth.backend.enrichment.lab.capture_exception") as mock_capture,patch("tenacity.nap.time.sleep"),):@@ -765,7 +771,7 @@ class TestAIEnrichmentRunClassification(NonAtomicAPIBaseTest):client = _mock_llm_client()with (- patch("products.growth.backend.api.ai_enrichment.get_llm_client", return_value=client),+ patch("products.growth.backend.api.ai_enrichment.build_openai_client", return_value=client),patch("products.growth.backend.enrichment.lab.classify_payload", side_effect=TransientToolError("boom")),):response = self.client.post(diff --git a/products/growth/backend/enrichment/lab.py b/products/growth/backend/enrichment/lab.pyindex 02e9d790f287..ba4a3aa8d24e 100644--- a/products/growth/backend/enrichment/lab.py+++ b/products/growth/backend/enrichment/lab.py@@ -18,7 +18,7 @@ import structlogfrom openai import OpenAIfrom posthog.exceptions_capture import capture_exception-from posthog.llm.gateway_client import get_llm_client+from posthog.llm.gateway_client import build_openai_clientfrom products.growth.backend.enrichment.labels import (TransientToolError,@@ -64,7 +64,7 @@ def list_gateway_models() -> list[str]:if _model_list_cache["models"] is not None and now < _model_list_cache["expires_at"]:return list(_model_list_cache["models"])try:- client = get_llm_client(product="growth")+ client = build_openai_client(product="growth", ai_product="growth")models = sorted({model.id for model in client.models.list()})if not models:raise ValueError("gateway returned no models")diff --git a/products/growth/backend/enrichment/labels.py b/products/growth/backend/enrichment/labels.pyindex 3be4fc56cb40..fd31266ba8f7 100644--- a/products/growth/backend/enrichment/labels.py+++ b/products/growth/backend/enrichment/labels.py@@ -1,25 +1,32 @@"""Classifier runner for versioned AI enrichment labels.Pure functions only: no persistence, no client construction. Callers build the OpenAI-client via `get_llm_client(product="growth")` and pass it in — this module just turns-an archived Harmonic payload plus a prompt config into a stamped verdict.+client via `build_openai_client(product="growth", ai_product="growth", properties=...)` and pass it in —+this module just turns an archived Harmonic payload plus a prompt config into a stamped verdict."""import reimport jsonimport math+import weakref+import threadingfrom collections.abc import Callablefrom datetime import timedeltafrom typing import Any, Literal, TypeIs, castfrom urllib.parse import urlsplit, urlunsplit+from django.conf import settingsfrom django.db.models import QuerySetfrom django.utils import timezone+import httpx+import anthropic+from anthropic.types import Messagefrom openai import APIConnectionError, InternalServerError, OpenAI, RateLimitErrorfrom openai.types.chat import ChatCompletion, ChatCompletionMessageParamfrom tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential, wait_random+from posthog.llm.gateway_client import anthropic_gateway_base_urlfrom posthog.llm.semantic_enrichment import extract_json_objectfrom posthog.models.organization import Organization, OrganizationMembership@@ -55,6 +62,8 @@ MAX_INPUT_DEPTH = 6# not a reservation, so the headroom is free.MAX_OUTPUT_TOKENS = 4000+AI_ENRICHMENT_FEATURE = "ai_enrichment"+# Bounds on the prompt config's own authored content (prompt_text, output_fields) - not the# archived payload data above. Defined here, not in ai_enrichment_serializers.py, so any other# consumer of EnrichmentPromptConfig content (e.g. the admin form) can share the same numbers@@ -459,6 +468,28 @@ def _run_tool_call(return run_tool(call.function.name, arguments, pacer=pacer)+_RETRYABLE_ERRORS = (+ APIConnectionError,+ RateLimitError,+ InternalServerError,+ anthropic.APIConnectionError,+ anthropic.RateLimitError,+ anthropic.InternalServerError,+)++_ANTHROPIC_TOOLS = [+ {+ "name": tool["function"]["name"],+ "description": tool["function"]["description"],+ "input_schema": tool["function"]["parameters"],+ }+ for tool in TOOLS+]++_anthropic_clients: "weakref.WeakKeyDictionary[OpenAI, anthropic.Anthropic]" = weakref.WeakKeyDictionary()+_anthropic_clients_lock = threading.Lock()++@retry(# Allowlist, not a blacklist: only failures that can plausibly succeed on a retry earn one.# APIConnectionError covers its APITimeoutError subclass too. Everything else — most@@ -467,16 +498,130 @@ def _run_tool_call(# three attempts from stacking with the SDK's own retries on a hard failure like a bad# gateway credential; see max_retries=0 at every client construction site for the other half# of that fix.- retry=retry_if_exception_type((APIConnectionError, RateLimitError, InternalServerError)),+ retry=retry_if_exception_type(_RETRYABLE_ERRORS),stop=stop_after_attempt(3),# Jitter so a worker pool that all hit a 429 together doesn't retry in lockstep.wait=wait_exponential(multiplier=1, min=2, max=30) + wait_random(0, 2),reraise=True,)def _complete(client: OpenAI, request: dict[str, Any]) -> ChatCompletion:+ if _routes_through_anthropic_messages(client, request["model"]):+ return _complete_with_anthropic_messages(client, request)return client.chat.completions.create(**request)+def _routes_through_anthropic_messages(client: OpenAI, model: str) -> bool:+ if not model.startswith("claude-"):+ return False+ if not settings.AI_GATEWAY_URL:+ return False+ return str(client.base_url).rstrip("/") == settings.AI_GATEWAY_URL.rstrip("/")+++def _anthropic_client_for(client: OpenAI) -> anthropic.Anthropic:+ with _anthropic_clients_lock:+ anthropic_client = _anthropic_clients.get(client)+ if anthropic_client is None:+ anthropic_client = anthropic.Anthropic(+ api_key=client.api_key,+ base_url=anthropic_gateway_base_url(str(client.base_url)),+ default_headers={+ name: value+ for name, value in client.default_headers.items()+ if name.lower().startswith("x-posthog-") and isinstance(value, str)+ },+ max_retries=client.max_retries,+ http_client=httpx.Client(trust_env=False),+ )+ _anthropic_clients[client] = anthropic_client+ return anthropic_client+++def _complete_with_anthropic_messages(client: OpenAI, request: dict[str, Any]) -> ChatCompletion:+ message = _anthropic_client_for(client).messages.create(**_anthropic_request(request))+ return _chat_completion_from_message(message)+++def _anthropic_request(request: dict[str, Any]) -> dict[str, Any]:+ system, *history = request["messages"]+ assert system["role"] == "system"+ assert history+ messages: list[dict[str, Any]] = []+ for m in history:+ if m["role"] == "tool":+ result = {"type": "tool_result", "tool_use_id": m["tool_call_id"], "content": m["content"]}+ if messages[-1]["role"] == "user":+ messages[-1]["content"].append(result)+ else:+ messages.append({"role": "user", "content": [result]})+ continue+ if m["role"] == "assistant" and m.get("tool_calls"):+ blocks: list[dict[str, Any]] = [{"type": "text", "text": m["content"]}] if m.get("content") else []+ blocks.extend(+ {+ "type": "tool_use",+ "id": call["id"],+ "name": call["function"]["name"],+ "input": json.loads(call["function"]["arguments"]),+ }+ for call in m["tool_calls"]+ )+ messages.append({"role": "assistant", "content": blocks})+ continue+ messages.append({"role": m["role"], "content": m["content"]})+ anthropic_request: dict[str, Any] = {+ "model": request["model"],+ "system": system["content"],+ "messages": messages,+ "max_tokens": request["max_completion_tokens"],+ "timeout": request["timeout"],+ }+ if "tools" in request:+ assert request["tools"] is TOOLS+ anthropic_request["tools"] = _ANTHROPIC_TOOLS+ elif any(m["role"] == "tool" for m in history):+ anthropic_request["tools"] = _ANTHROPIC_TOOLS+ anthropic_request["tool_choice"] = {"type": "none"}+ return anthropic_request+++_ANTHROPIC_FINISH_REASONS = {+ "end_turn": "stop",+ "stop_sequence": "stop",+ "max_tokens": "length",+ "tool_use": "tool_calls",+}+++def _chat_completion_from_message(message: Message) -> ChatCompletion:+ text = "".join(block.text for block in message.content if block.type == "text")+ tool_calls = [+ {"id": block.id, "type": "function", "function": {"name": block.name, "arguments": json.dumps(block.input)}}+ for block in message.content+ if block.type == "tool_use"+ ]+ return ChatCompletion.model_validate(+ {+ "id": message.id,+ "object": "chat.completion",+ "created": int(timezone.now().timestamp()),+ "model": message.model,+ "choices": [+ {+ "index": 0,+ "finish_reason": _ANTHROPIC_FINISH_REASONS.get(message.stop_reason or "", "stop"),+ "message": {"role": "assistant", "content": text or None, "tool_calls": tool_calls or None},+ }+ ],+ "usage": {+ "prompt_tokens": message.usage.input_tokens,+ "completion_tokens": message.usage.output_tokens,+ "total_tokens": message.usage.input_tokens + message.usage.output_tokens,+ },+ }+ )++def _call_and_parse(config: EnrichmentPromptConfig,messages: list[dict[str, Any]],diff --git a/products/growth/backend/management/commands/enrichment_label_batch.py b/products/growth/backend/management/commands/enrichment_label_batch.pyindex 723c87f90fb0..f7574efcd245 100644--- a/products/growth/backend/management/commands/enrichment_label_batch.py+++ b/products/growth/backend/management/commands/enrichment_label_batch.py@@ -24,7 +24,7 @@ from django.db.models import Qimport structlogfrom posthog.exceptions_capture import capture_exception-from posthog.llm.gateway_client import get_llm_client+from posthog.llm.gateway_client import build_openai_clientfrom posthog.ph_client import ph_scoped_capturefrom posthog.utils import get_instance_region@@ -32,6 +32,7 @@ from products.growth.backend.enrichment import gatesfrom products.growth.backend.enrichment.fit_recomputation import apply_enrichment_result, label_needs_applicationfrom products.growth.backend.enrichment.icp_lists import load_active_listsfrom products.growth.backend.enrichment.labels import (+ AI_ENRICHMENT_FEATURE,PromptConfigError,TransientToolError,ai_processing_approved,@@ -198,7 +199,11 @@ class Command(BaseCommand):# tenacity in labels.py already owns retries (stop_after_attempt(3)); the SDK's own# internal retries underneath would multiply that budget nine-fold per fetch and actively# worsen a 429 the tenacity layer is already backing off from.- client = get_llm_client(product="growth").with_options(max_retries=0)+ client = build_openai_client(+ product="growth",+ ai_product="growth",+ properties={"ai_feature": AI_ENRICHMENT_FEATURE, "surface": "batch"},+ ).with_options(max_retries=0)pacer = FirecrawlPacer()counts: dict[str, int] = {diff --git a/products/growth/backend/management/commands/enrichment_label_dry_run.py b/products/growth/backend/management/commands/enrichment_label_dry_run.pyindex 3a0e0693d5e5..8bc09e0e9d53 100644--- a/products/growth/backend/management/commands/enrichment_label_dry_run.py+++ b/products/growth/backend/management/commands/enrichment_label_dry_run.py@@ -9,9 +9,10 @@ from typing import Anyfrom django.core.management.base import BaseCommand, CommandError, CommandParser-from posthog.llm.gateway_client import get_llm_client+from posthog.llm.gateway_client import build_openai_clientfrom products.growth.backend.enrichment.labels import (+ AI_ENRICHMENT_FEATURE,UNKNOWN,PromptConfigError,TransientToolError,@@ -94,7 +95,11 @@ class Command(BaseCommand):# tenacity in labels.py already owns retries; the SDK's own internal retries underneath# would multiply that budget nine-fold per row.- client = get_llm_client(product="growth").with_options(max_retries=0)+ client = build_openai_client(+ product="growth",+ ai_product="growth",+ properties={"ai_feature": AI_ENRICHMENT_FEATURE, "surface": "dry_run"},+ ).with_options(max_retries=0)# A custom output schema's pass/fail key differs from `label` - see# verdict_field_key's docstring.verdict_key = verdict_field_key(config)diff --git a/products/growth/backend/tests/test_ai_pilled.py b/products/growth/backend/tests/test_ai_pilled.pyindex 62510613d542..3988a274be71 100644--- a/products/growth/backend/tests/test_ai_pilled.py+++ b/products/growth/backend/tests/test_ai_pilled.py@@ -294,7 +294,7 @@ class TestEnrichmentScoreApplication(BaseTest):return_value=OrganizationBridgeInputs(),),patch(- "products.growth.backend.management.commands.enrichment_label_batch.get_llm_client",+ "products.growth.backend.management.commands.enrichment_label_batch.build_openai_client",return_value=gateway,),):@@ -357,7 +357,7 @@ class TestEnrichmentScoreApplication(BaseTest):return_value=OrganizationBridgeInputs(),),patch(- "products.growth.backend.management.commands.enrichment_label_batch.get_llm_client",+ "products.growth.backend.management.commands.enrichment_label_batch.build_openai_client",return_value=gateway,),patch(@@ -499,7 +499,7 @@ class TestEnrichmentScoreApplication(BaseTest):return_value=OrganizationBridgeInputs(),),patch(- "products.growth.backend.management.commands.enrichment_label_batch.get_llm_client",+ "products.growth.backend.management.commands.enrichment_label_batch.build_openai_client",return_value=gateway,),):@@ -547,7 +547,7 @@ class TestEnrichmentScoreApplication(BaseTest):return_value=OrganizationBridgeInputs(),),patch(- "products.growth.backend.management.commands.enrichment_label_batch.get_llm_client",+ "products.growth.backend.management.commands.enrichment_label_batch.build_openai_client",return_value=gateway,),patch("products.growth.backend.management.commands.enrichment_label_batch._ID_BATCH_SIZE", 1),@@ -599,7 +599,7 @@ class TestEnrichmentScoreApplication(BaseTest):return_value=OrganizationBridgeInputs(),),patch(- "products.growth.backend.management.commands.enrichment_label_batch.get_llm_client",+ "products.growth.backend.management.commands.enrichment_label_batch.build_openai_client",return_value=gateway,),patch("products.growth.backend.management.commands.enrichment_label_batch._ID_BATCH_SIZE", 1),diff --git a/products/growth/backend/tests/test_enrichment_label_commands.py b/products/growth/backend/tests/test_enrichment_label_commands.pyindex 4e26b3aba6b9..c96197dc50bd 100644--- a/products/growth/backend/tests/test_enrichment_label_commands.py+++ b/products/growth/backend/tests/test_enrichment_label_commands.py@@ -56,7 +56,7 @@ def _bad_response() -> MagicMock:def _mock_llm_client() -> MagicMock:client = MagicMock()- # Command call sites chain .with_options(max_retries=0) onto get_llm_client(...); without+ # Command call sites chain .with_options(max_retries=0) onto build_openai_client(...); without# this self-reference, that call returns an unconfigured child mock and every call made# through it is invisible to assertions on `client`.client.with_options.return_value = client@@ -96,14 +96,14 @@ class _BatchCommandTestCase(BaseTest):class TestGatewayRetryBudget(_BatchCommandTestCase):def test_batch_disables_sdk_internal_retries(self):- # get_llm_client builds an OpenAI() client without max_retries, so the SDK's own default+ # build_openai_client builds an OpenAI() client without max_retries, so the SDK's own default# of 2 stacks under tenacity's stop_after_attempt(3): up to 9 HTTP requests per fetch# instead of 3. The command must opt out of the SDK's layer so tenacity is the only one.self._config()self._fetch()client = _mock_llm_client()- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=1)client.with_options.assert_called_once_with(max_retries=0)@@ -113,7 +113,7 @@ class TestGatewayRetryBudget(_BatchCommandTestCase):self._fetch()client = _mock_llm_client()- with patch(f"{_DRY_RUN_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_DRY_RUN_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_dry_run", label="test_label", sample=1)client.with_options.assert_called_once_with(max_retries=0)@@ -128,7 +128,7 @@ class TestCircuitBreaker(_BatchCommandTestCase):client.chat.completions.create.return_value = _bad_response()with (- patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client),+ patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client),patch(f"{_BATCH_COMMAND_MODULE}.capture_exception"),):with self.assertRaises(CommandError) as ctx:@@ -152,7 +152,7 @@ class TestCircuitBreaker(_BatchCommandTestCase):]with (- patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client),+ patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client),patch(f"{_BATCH_COMMAND_MODULE}.capture_exception"),):# max_failures=2 would trip on two failures in a row; these two never land back to@@ -180,7 +180,7 @@ class TestKeysetPagination(_BatchCommandTestCase):options["limit"] = limitwith (- patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client),+ patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client),# Forces the 5 orgs across 3 keyset pages instead of 1, so a cursor off-by-one would# duplicate or skip a row instead of silently passing on a single-page test.patch(f"{_BATCH_COMMAND_MODULE}._ID_BATCH_SIZE", 2),@@ -216,7 +216,7 @@ class TestKeysetPagination(_BatchCommandTestCase):out = StringIO()with (- patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client),+ patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client),# Forces the 3 already-done orgs and the 1 fresh org across separate keyset pages.patch(f"{_BATCH_COMMAND_MODULE}._ID_BATCH_SIZE", 2),):@@ -243,7 +243,7 @@ class TestKeysetPagination(_BatchCommandTestCase):client = _mock_llm_client()with (- patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client),+ patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client),patch(f"{_BATCH_COMMAND_MODULE}._ID_BATCH_SIZE", 2),):call_command("enrichment_label_batch", label="test_label", workers=1, **options)@@ -282,7 +282,7 @@ class TestAdvisoryLock(_BatchCommandTestCase):assert holder_ready.wait(timeout=5)assert holder_state["acquired"] is True- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):with self.assertRaises(CommandError) as ctx:call_command("enrichment_label_batch", label="test_label", workers=1)assert "already holds the lock" in str(ctx.exception)@@ -304,7 +304,7 @@ class TestUnknownAccounting(_BatchCommandTestCase):client = _mock_llm_client()out = StringIO()- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=1, stdout=out)assert "unknown 1" in out.getvalue()@@ -336,7 +336,7 @@ class TestExitCodeAndSummary(_BatchCommandTestCase):options["min_success_rate"] = min_success_ratewith (- patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client),+ patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client),patch(f"{_BATCH_COMMAND_MODULE}.capture_exception"),):if expect_raise:@@ -359,7 +359,7 @@ class TestExitCodeAndSummary(_BatchCommandTestCase):out = StringIO()with (- patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client),+ patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client),patch(f"{_BATCH_COMMAND_MODULE}.capture_exception"),):with self.assertRaises(CommandError):@@ -379,7 +379,7 @@ class TestExitCodeAndSummary(_BatchCommandTestCase):]out = StringIO()- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=1, stdout=out)assert "prompt_tokens 350" in out.getvalue()@@ -401,7 +401,7 @@ class TestExpectedVersionGuard(_BatchCommandTestCase):self._fetch()client = _mock_llm_client()- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):with self.assertRaises(CommandError):call_command("enrichment_label_batch", label="test_label", workers=1, expected_version="v1")@@ -413,7 +413,7 @@ class TestExpectedVersionGuard(_BatchCommandTestCase):self._fetch()client = _mock_llm_client()- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=1, expected_version="v2")assert EnrichmentLabelResult.objects.count() == 1@@ -423,7 +423,7 @@ class TestExpectedVersionGuard(_BatchCommandTestCase):self._fetch()client = _mock_llm_client()- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=1)assert EnrichmentLabelResult.objects.count() == 1@@ -444,7 +444,7 @@ class TestFirecrawlKeyGuard(_BatchCommandTestCase):with (override_settings(CLOUD_DEPLOYMENT=cloud_deployment, FIRECRAWL_API_KEY=firecrawl_key),- patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client),+ patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client),):if aborts:with self.assertRaisesMessage(CommandError, "FIRECRAWL_API_KEY"):@@ -477,7 +477,7 @@ class TestWorkerConnectionErrors(NonAtomicBaseTest):client = _mock_llm_client()with (- patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client),+ patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client),patch(f"{_BATCH_COMMAND_MODULE}.close_old_connections", side_effect=RuntimeError("connection reset")),patch(f"{_BATCH_COMMAND_MODULE}.capture_exception") as capture_mock,):@@ -511,7 +511,7 @@ class TestDryRunFixes(BaseTest):client = _mock_llm_client()out = StringIO()- with patch(f"{_DRY_RUN_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_DRY_RUN_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_dry_run", label="test_label", sample=2, stdout=out)printed = out.getvalue()@@ -525,7 +525,7 @@ class TestDryRunFixes(BaseTest):)client = _mock_llm_client()- with patch(f"{_DRY_RUN_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_DRY_RUN_COMMAND_MODULE}.build_openai_client", return_value=client):with self.assertRaises(CommandError):call_command("enrichment_label_dry_run", label="test_label", sample=1)@@ -551,7 +551,7 @@ class TestDryRunToolDeferral(BaseTest):out = StringIO()with (- patch(f"{_DRY_RUN_COMMAND_MODULE}.get_llm_client", return_value=client),+ patch(f"{_DRY_RUN_COMMAND_MODULE}.build_openai_client", return_value=client),patch(f"{_DRY_RUN_COMMAND_MODULE}.classify_payload", side_effect=TransientToolError("boom")),):call_command("enrichment_label_dry_run", label="test_label", sample=1, stdout=out)@@ -580,7 +580,7 @@ class TestToolCallAccounting(_BatchCommandTestCase):}with (- patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client),+ patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client),patch(f"{_BATCH_COMMAND_MODULE}.classify_payload", side_effect=_classify),):call_command("enrichment_label_batch", label="test_label", workers=1, stdout=out)@@ -596,7 +596,7 @@ class TestToolDeferral(_BatchCommandTestCase):out = StringIO()with (- patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client),+ patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client),patch(f"{_BATCH_COMMAND_MODULE}.classify_payload", side_effect=TransientToolError("boom")),self.assertRaises(CommandError),):@@ -625,7 +625,7 @@ class TestToolDeferral(_BatchCommandTestCase):out = StringIO()with (- patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client),+ patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client),patch(f"{_TOOLS_MODULE}.get_outbound_rate_limiter", return_value=limiter),patch(f"{_TOOLS_MODULE}.time.sleep"),patch(@@ -646,7 +646,7 @@ class TestToolDeferral(_BatchCommandTestCase):out = StringIO()with (- patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client),+ patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client),patch(f"{_BATCH_COMMAND_MODULE}.classify_payload", side_effect=TransientToolError("boom")) as classify_mock,self.assertRaises(CommandError) as ctx,):@@ -673,7 +673,7 @@ class TestToolDeferral(_BatchCommandTestCase):return {"is_ai": True, "confidence": 0.9, "reasoning": "x", "inputs": {"signup_domain": None, "fields": {}}}with (- patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client),+ patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client),patch(f"{_BATCH_COMMAND_MODULE}.classify_payload", side_effect=_classify),self.assertRaises(CommandError) as ctx,):@@ -695,7 +695,7 @@ class TestBatchRunReportEvent(_BatchCommandTestCase):scoped_capture.__enter__.return_value = capturewith (- patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client),+ patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client),patch(f"{_BATCH_COMMAND_MODULE}.get_instance_region", return_value="EU"),patch(f"{_BATCH_COMMAND_MODULE}.ph_scoped_capture", return_value=scoped_capture) as scoped_capture_factory,):@@ -724,7 +724,7 @@ class TestBatchRunReportEvent(_BatchCommandTestCase):out = StringIO()with (- patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client),+ patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client),patch(f"{_BATCH_COMMAND_MODULE}.get_instance_region", return_value="US"),patch(f"{_BATCH_COMMAND_MODULE}.ph_scoped_capture", side_effect=RuntimeError("boom")),patch(f"{_BATCH_COMMAND_MODULE}.capture_exception") as capture_mock,@@ -741,7 +741,7 @@ class TestBatchRunReportEvent(_BatchCommandTestCase):client = _mock_llm_client()with (- patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client),+ patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client),patch(f"{_BATCH_COMMAND_MODULE}.get_instance_region", return_value=None),patch(f"{_BATCH_COMMAND_MODULE}.ph_scoped_capture") as scoped_capture_factory,):@@ -758,7 +758,7 @@ class TestBatchRunReportEvent(_BatchCommandTestCase):scoped_capture.__enter__.return_value = MagicMock()with (- patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client),+ patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client),patch(f"{_BATCH_COMMAND_MODULE}.capture_exception"),patch(f"{_BATCH_COMMAND_MODULE}.get_instance_region", return_value="US"),patch(f"{_BATCH_COMMAND_MODULE}.ph_scoped_capture", return_value=scoped_capture) as scoped_capture_factory,@@ -777,7 +777,7 @@ class TestAiProcessingConsent(_BatchCommandTestCase):self._fetch()client = _mock_llm_client()- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=1)client.chat.completions.create.assert_not_called()@@ -792,11 +792,11 @@ class TestAiProcessingConsent(_BatchCommandTestCase):self._fetch()client = _mock_llm_client()- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=1)self.organization.is_ai_data_processing_approved = Trueself.organization.save(update_fields=["is_ai_data_processing_approved"])- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=1)assert EnrichmentLabelResult.objects.count() == 1@@ -817,7 +817,7 @@ class TestAiProcessingConsent(_BatchCommandTestCase):client.chat.completions.create.side_effect = _revoke_after_first- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=1)assert client.chat.completions.create.call_count == 1@@ -830,7 +830,7 @@ class TestAiProcessingConsent(_BatchCommandTestCase):self._fetch()client = _mock_llm_client()- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=1)client.chat.completions.create.assert_not_called()@@ -843,7 +843,7 @@ class TestAiProcessingConsent(_BatchCommandTestCase):client = _mock_llm_client()out = StringIO()- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=1, stdout=out)assert "skipped_no_ai_consent 1" in out.getvalue()@@ -866,7 +866,7 @@ class TestAiProcessingConsent(_BatchCommandTestCase):client = _mock_llm_client()out = StringIO()- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=1, limit=1, stdout=out)assert EnrichmentLabelResult.objects.filter(organization=approved_org).exists()@@ -882,7 +882,7 @@ class TestAiProcessingConsent(_BatchCommandTestCase):client = _mock_llm_client()out = StringIO()- with patch(f"{_DRY_RUN_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_DRY_RUN_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_dry_run", label="test_label", sample=1, stdout=out)output = out.getvalue()diff --git a/products/growth/backend/tests/test_enrichment_labels.py b/products/growth/backend/tests/test_enrichment_labels.pyindex fd46ddd4d565..e33d001f84c8 100644--- a/products/growth/backend/tests/test_enrichment_labels.py+++ b/products/growth/backend/tests/test_enrichment_labels.py@@ -9,9 +9,10 @@ from unittest.mock import MagicMock, patchfrom django.core.management import call_commandfrom django.core.management.base import CommandError-from django.test import SimpleTestCase+from django.test import SimpleTestCase, override_settingsimport openai+from anthropic.types import Message, TextBlock, ToolUseBlock, Usagefrom openai import OpenAIfrom parameterized import parameterized@@ -43,7 +44,7 @@ def _mock_llm_client(verdict: bool = True, confidence: float = 0.9, reasoning: str = "builds ai software", verdict_key: str = "is_ai") -> MagicMock:client = MagicMock()- # The command call sites chain .with_options(max_retries=0) onto get_llm_client(...); without+ # The command call sites chain .with_options(max_retries=0) onto build_openai_client(...); without# this, that call returns a fresh, unconfigured child mock and every assertion below silently# checks the wrong object.client.with_options.return_value = client@@ -403,6 +404,104 @@ class TestClassifyPayloadToolLoop(SimpleTestCase):assert len(client.calls) == 3+ @override_settings(AI_GATEWAY_URL="https://gateway.test/v1")+ def test_claude_on_the_ai_gateway_runs_the_tool_loop_through_anthropic_messages(self):+ config = self._config()+ config.model = "claude-haiku-4-5"+ client = OpenAI(+ api_key="phs_test", base_url="https://gateway.test/v1", default_headers={"X-PostHog-Product": "growth"}+ )+ usage = Usage(input_tokens=10, output_tokens=5)+ replies = [+ Message(+ id="msg_1",+ type="message",+ role="assistant",+ model="claude-haiku-4-5",+ stop_reason="tool_use",+ content=[ToolUseBlock(type="tool_use", id="toolu_1", name="web_search", input={"query": "Acme AI"})],+ usage=usage,+ ),+ Message(+ id="msg_2",+ type="message",+ role="assistant",+ model="claude-haiku-4-5",+ stop_reason="end_turn",+ content=[TextBlock(type="text", text=json.dumps({"is_ai": True, "evidence_url": ""}))],+ usage=usage,+ ),+ ]+ found = FirecrawlSearch(+ query="Acme AI", results=(FirecrawlSearchResult(url="https://techcrunch.com/acme", title="funding"),)+ )++ with (+ patch(f"{_LABELS_MODULE}.anthropic.Anthropic") as anthropic_client,+ patch(f"{_TOOLS_MODULE}.search", return_value=found),+ ):+ create = anthropic_client.return_value.messages.create+ create.side_effect = replies+ result = classify_payload(config, {"name": "Acme"}, "example.com", client)++ assert result["is_ai"] is True+ assert result["meta"]["prompt_tokens"] == 20+ assert anthropic_client.call_args.kwargs["base_url"] == "https://gateway.test"+ assert anthropic_client.call_args.kwargs["default_headers"] == {"X-PostHog-Product": "growth"}+ first_turn, second_turn = (call.kwargs for call in create.call_args_list)+ assert first_turn["system"].startswith("judge it.")+ assert [tool["name"] for tool in first_turn["tools"]] == ["web_search", "fetch_page"]+ assert second_turn["messages"][1]["content"][0]["type"] == "tool_use"+ tool_result = second_turn["messages"][2]["content"][0]+ assert tool_result["tool_use_id"] == "toolu_1"+ assert "techcrunch.com/acme" in tool_result["content"]++ @override_settings(AI_GATEWAY_URL="https://gateway.test/v1")+ def test_claude_final_turn_after_the_tool_budget_keeps_tools_but_disables_them(self):+ config = self._config()+ config.model = "claude-haiku-4-5"+ client = OpenAI(api_key="phs_test", base_url="https://gateway.test/v1")+ usage = Usage(input_tokens=10, output_tokens=5)+ tool_uses = [+ ToolUseBlock(type="tool_use", id=f"toolu_{i}", name="web_search", input={"query": f"q{i}"})+ for i in range(5)+ ]+ replies = [+ Message(+ id="msg_1",+ type="message",+ role="assistant",+ model="claude-haiku-4-5",+ stop_reason="tool_use",+ content=tool_uses,+ usage=usage,+ ),+ Message(+ id="msg_2",+ type="message",+ role="assistant",+ model="claude-haiku-4-5",+ stop_reason="end_turn",+ content=[TextBlock(type="text", text=json.dumps({"is_ai": True, "evidence_url": ""}))],+ usage=usage,+ ),+ ]+ found = FirecrawlSearch(query="q", results=(FirecrawlSearchResult(url="https://x.example"),))++ with (+ patch(f"{_LABELS_MODULE}.anthropic.Anthropic") as anthropic_client,+ patch(f"{_TOOLS_MODULE}.search", return_value=found),+ ):+ create = anthropic_client.return_value.messages.create+ create.side_effect = replies+ result = classify_payload(config, {"name": "Acme"}, "example.com", client)++ assert result["is_ai"] is True+ final_turn = create.call_args_list[1].kwargs+ assert [tool["name"] for tool in final_turn["tools"]] == ["web_search", "fetch_page"]+ assert final_turn["tool_choice"] == {"type": "none"}+ assert len(final_turn["messages"][-1]["content"]) == 5+class TestClassifyPayloadToolEvidenceUrl(SimpleTestCase):def _config(self) -> EnrichmentPromptConfig:@@ -776,7 +875,7 @@ class TestEnrichmentLabelBatch(BaseTest):fetch = self._fetch()client = _mock_llm_client()- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=1)result = EnrichmentLabelResult.objects.get(organization=self.organization, label_name="test_label")@@ -799,7 +898,7 @@ class TestEnrichmentLabelBatch(BaseTest):response.usage.completion_tokens = 40client.chat.completions.create.return_value = response- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=1)output = EnrichmentLabelResult.objects.get(label_name="test_label").output@@ -819,7 +918,7 @@ class TestEnrichmentLabelBatch(BaseTest):self._fetch()client = _mock_llm_client()- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=1)result = EnrichmentLabelResult.objects.get(label_name="test_label")@@ -831,7 +930,7 @@ class TestEnrichmentLabelBatch(BaseTest):self._fetch(payload={})client = _mock_llm_client()- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=1)result = EnrichmentLabelResult.objects.get(label_name="test_label")@@ -845,7 +944,7 @@ class TestEnrichmentLabelBatch(BaseTest):self._fetch()client = _mock_llm_client()- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=1)call_command("enrichment_label_batch", label="test_label", workers=1)@@ -857,7 +956,7 @@ class TestEnrichmentLabelBatch(BaseTest):first_fetch = self._fetch()client = _mock_llm_client()- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=1)second_fetch = self._fetch(payload={"name": "Acme v2"})@@ -865,7 +964,7 @@ class TestEnrichmentLabelBatch(BaseTest):fetched_at=first_fetch.fetched_at + dt.timedelta(minutes=5))- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=1)rows = EnrichmentLabelResult.objects.filter(@@ -890,7 +989,7 @@ class TestEnrichmentLabelBatch(BaseTest):return original_classify(*args, **kwargs)with (- patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client),+ patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client),patch(f"{_BATCH_COMMAND_MODULE}.classify_payload", side_effect=rename_then_classify),):call_command("enrichment_label_batch", label="test_label", workers=1)@@ -916,7 +1015,7 @@ class TestEnrichmentLabelBatch(BaseTest):client.chat.completions.create.return_value = responsewith (- patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client),+ patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client),patch(f"{_BATCH_COMMAND_MODULE}.capture_exception") as capture_mock,patch("tenacity.nap.time.sleep"),):@@ -950,7 +1049,7 @@ class TestEnrichmentLabelBatch(BaseTest):client = MagicMock()out = StringIO()- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=1, stdout=out)assert "unknown 1" in out.getvalue()@@ -963,14 +1062,14 @@ class TestEnrichmentLabelBatch(BaseTest):self._fetch()client = _mock_llm_client()- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=1)v1.is_active = Falsev1.save()self._config(version="ai-pilled-clay-v2", prompt_text="a different prompt entirely. Email: {email}")- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=1)assert EnrichmentLabelResult.objects.filter(prompt_version="ai-pilled-clay-v1").count() == 1@@ -1016,7 +1115,7 @@ class TestEnrichmentLabelDryRun(BaseTest):)client = _mock_llm_client()- with patch(f"{_DRY_RUN_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_DRY_RUN_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_dry_run", label="test_label")assert EnrichmentLabelResult.objects.count() == 0@@ -1068,7 +1167,7 @@ class TestEnrichmentLabelDryRun(BaseTest):client.chat.completions.create.return_value = responseout = StringIO()- with patch(f"{_DRY_RUN_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_DRY_RUN_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_dry_run",label="ai_native_teams",@@ -1113,13 +1212,13 @@ class TestEnrichmentLabelBatchConcurrency(NonAtomicBaseTest):)client = _mock_llm_client()- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=2)assert client.chat.completions.create.call_count == 4assert EnrichmentLabelResult.objects.count() == 4- with patch(f"{_BATCH_COMMAND_MODULE}.get_llm_client", return_value=client):+ with patch(f"{_BATCH_COMMAND_MODULE}.build_openai_client", return_value=client):call_command("enrichment_label_batch", label="test_label", workers=2)assert client.chat.completions.create.call_count == 4