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(tasks): route the custom image scan judge through the go gateway (#122267)

Brandon8d08b0e8c9f7
diff --git a/posthog/llm/gateway_client.py b/posthog/llm/gateway_client.py
index a7747f9541bc..e91f3f64d4a3 100644
--- a/posthog/llm/gateway_client.py
+++ b/posthog/llm/gateway_client.py
@@ -41,7 +41,6 @@ Product = Literal[
"subscriptions",
"signals",
"review_hog",
- "custom_image_scans",
"conversations",
"warehouse_semantic_enrichment",
"warehouse_custom_source_builder",
@@ -476,6 +475,34 @@ def build_async_openai_client(
return get_async_llm_client(product, default_headers=fallback_headers)
+def build_ai_gateway_openai_client(
+ api_key: str,
+ ai_product: str,
+ trace_id: str | None = None,
+ properties: Mapping[str, str] | None = None,
+ distinct_id: str | None = None,
+) -> OpenAI:
+ """Go ai-gateway client on the caller's own bearer (``pha_``, ``phe_``); no Python-gateway fallback."""
+ url = settings.AI_GATEWAY_URL
+ if not url:
+ raise GatewayNotConfiguredError("AI_GATEWAY_URL is not set")
+ if not urlparse(url).path.rstrip("/").endswith("/v1"):
+ raise GatewayNotConfiguredError(f"AI_GATEWAY_URL must end with /v1, got {url}")
+ if not api_key:
+ raise GatewayNotConfiguredError("api_key is required")
+ return OpenAI(
+ api_key=api_key,
+ base_url=url,
+ default_headers=ai_gateway_headers(
+ ai_product=ai_product,
+ trace_id=trace_id,
+ properties=properties,
+ distinct_id=distinct_id,
+ ),
+ http_client=httpx.Client(trust_env=False),
+ )
+
+
def build_async_anthropic_client(
product: Product,
ai_product: str | None = None,
diff --git a/posthog/llm/test_gateway_client.py b/posthog/llm/test_gateway_client.py
index b04f80e669e0..c64d113c10ab 100644
--- a/posthog/llm/test_gateway_client.py
+++ b/posthog/llm/test_gateway_client.py
@@ -19,6 +19,7 @@ from posthog.llm.gateway_client import (
GatewayNotConfiguredError,
Product,
build_ai_gateway_anthropic_client,
+ build_ai_gateway_openai_client,
build_anthropic_client,
build_async_anthropic_client,
build_async_openai_client,
@@ -75,7 +76,7 @@ class TestGetLlmClient:
mock_settings.LLM_GATEWAY_URL = "http://gateway:8080"
mock_settings.LLM_GATEWAY_API_KEY = "shared-key"
- client = get_llm_client(product="custom_image_scans", team_id=1, api_key="server-minted-token")
+ client = get_llm_client(product="django", team_id=1, api_key="server-minted-token")
assert client.api_key == "server-minted-token"
@@ -567,6 +568,44 @@ class TestBuildAIGatewayAnthropicClient:
mock_get_anthropic.assert_not_called()
+class TestBuildAIGatewayOpenAIClient:
+ @override_settings(AI_GATEWAY_URL=AI_GATEWAY_URL, AI_GATEWAY_API_KEY=AI_GATEWAY_KEY)
+ @patch("posthog.llm.gateway_client.httpx.Client")
+ @patch("posthog.llm.gateway_client.OpenAI")
+ def test_uses_the_callers_credential_instead_of_the_shared_key(self, mock_openai, mock_httpx):
+ result = build_ai_gateway_openai_client(
+ api_key="pha_user_token",
+ ai_product="custom_image_scans",
+ properties={"team_id": "42"},
+ distinct_id="team-42",
+ )
+
+ kwargs = mock_openai.call_args.kwargs
+ assert kwargs["api_key"] == "pha_user_token"
+ assert kwargs["base_url"] == AI_GATEWAY_URL
+ assert kwargs["http_client"] is mock_httpx.return_value
+ headers = kwargs["default_headers"]
+ assert headers["X-PostHog-Product"] == "custom_image_scans"
+ assert headers["X-PostHog-Distinct-Id"] == "team-42"
+ assert json.loads(headers["X-PostHog-Properties"]) == {"ai_product": "custom_image_scans", "team_id": "42"}
+ assert result is mock_openai.return_value
+
+ @pytest.mark.parametrize(
+ ("url", "api_key", "message"),
+ [
+ ("", "pha_user_token", "AI_GATEWAY_URL is not set"),
+ ("https://ai-gateway.example", "pha_user_token", "AI_GATEWAY_URL must end with /v1"),
+ (AI_GATEWAY_URL, "", "api_key is required"),
+ ],
+ )
+ @patch("posthog.llm.gateway_client.get_llm_client")
+ def test_raises_instead_of_using_the_python_gateway(self, mock_get_llm_client, url, api_key, message):
+ with override_settings(AI_GATEWAY_URL=url):
+ with pytest.raises(GatewayNotConfiguredError, match=message):
+ build_ai_gateway_openai_client(api_key=api_key, ai_product="custom_image_scans")
+ mock_get_llm_client.assert_not_called()
+
+
class TestBuildGeminiClient:
@override_settings(AI_GATEWAY_URL=AI_GATEWAY_URL, AI_GATEWAY_API_KEY=AI_GATEWAY_KEY)
@patch("posthog.llm.gateway_client.genai.Client")
diff --git a/products/tasks/backend/temporal/build_image/activities.py b/products/tasks/backend/temporal/build_image/activities.py
index 3e1692e783f2..9ab70e5b932c 100644
--- a/products/tasks/backend/temporal/build_image/activities.py
+++ b/products/tasks/backend/temporal/build_image/activities.py
@@ -11,8 +11,6 @@ from temporalio import activity
if TYPE_CHECKING:
import modal
- from posthog.llm.gateway_client import Product
-
from posthog.temporal.common.utils import asyncify
from posthog.temporal.oauth import create_oauth_access_token_for_user
@@ -29,8 +27,8 @@ from products.tasks.backend.temporal.observability import log_activity_execution
logger = logging.getLogger(__name__)
-SCAN_JUDGE_MODEL = "@cf/zai-org/glm-5.2"
-SCAN_JUDGE_PRODUCT: "Product" = "custom_image_scans"
+SCAN_JUDGE_MODEL = "zai-org/glm-5.2"
+SCAN_JUDGE_AI_PRODUCT = "custom_image_scans"
SCAN_JUDGE_SYSTEM_PROMPT = """You are a security judge reviewing a declarative sandbox image spec before it is built and published.
@@ -104,10 +102,11 @@ def _get_image(input: ImageBuildActivityInput) -> SandboxCustomImage:
return SandboxCustomImage.objects.for_team(input.team_id).get(id=input.image_id)
-def _judge_spec_safety(
- spec_yaml: str, team_id: int, repository: str = "", gateway_token: str | None = None
-) -> ScanImageSpecOutput:
- from posthog.llm.gateway_client import get_llm_client # noqa: PLC0415 — keeps the OpenAI SDK off startup paths
+def _judge_spec_safety(spec_yaml: str, team_id: int, gateway_token: str, repository: str = "") -> ScanImageSpecOutput:
+ from posthog.llm.gateway_client import ( # noqa: PLC0415 — keeps the OpenAI SDK off startup paths
+ build_ai_gateway_openai_client,
+ team_distinct_id,
+ )
repo_context = (
f"This image is linked to the GitHub repository {repository}; the spec's purpose is to prepare "
@@ -115,7 +114,12 @@ def _judge_spec_safety(
if repository
else ""
)
- client = get_llm_client(product=SCAN_JUDGE_PRODUCT, team_id=team_id, api_key=gateway_token)
+ client = build_ai_gateway_openai_client(
+ api_key=gateway_token,
+ ai_product=SCAN_JUDGE_AI_PRODUCT,
+ properties={"team_id": str(team_id)},
+ distinct_id=team_distinct_id(team_id),
+ )
response = client.chat.completions.create(
model=SCAN_JUDGE_MODEL,
messages=[
@@ -128,7 +132,7 @@ def _judge_spec_safety(
temperature=0.0,
max_completion_tokens=1024,
response_format={"type": "json_object"},
- user=f"team-{team_id}",
+ user=team_distinct_id(team_id),
)
content = response.choices[0].message.content
try:
@@ -158,9 +162,7 @@ def scan_image_spec(input: ImageBuildActivityInput) -> ScanImageSpecOutput:
scopes=["llm_gateway:read", "internal_run:read"],
include_internal_scopes=False,
)
- result = _judge_spec_safety(
- spec.to_yaml(), input.team_id, repository=image.repository, gateway_token=gateway_token
- )
+ result = _judge_spec_safety(spec.to_yaml(), input.team_id, gateway_token, repository=image.repository)
image.scan_result = {"passed": result.passed, "findings": result.findings}
if result.passed:
diff --git a/products/tasks/backend/temporal/build_image/test_activities.py b/products/tasks/backend/temporal/build_image/test_activities.py
index edcabb891a90..7cdb01579694 100644
--- a/products/tasks/backend/temporal/build_image/test_activities.py
+++ b/products/tasks/backend/temporal/build_image/test_activities.py
@@ -3,8 +3,8 @@ from unittest.mock import MagicMock, patch
from parameterized import parameterized
from products.tasks.backend.temporal.build_image.activities import (
+ SCAN_JUDGE_AI_PRODUCT,
SCAN_JUDGE_MODEL,
- SCAN_JUDGE_PRODUCT,
_judge_spec_safety,
_parse_scan_verdict,
)
@@ -25,19 +25,26 @@ class TestParseScanVerdict:
assert verdict.findings == []
-@patch("posthog.llm.gateway_client.get_llm_client")
-def test_security_scan_uses_glm_json_output(mock_get_llm_client: MagicMock) -> None:
- response = mock_get_llm_client.return_value.chat.completions.create.return_value
+@patch("posthog.llm.gateway_client.build_ai_gateway_openai_client")
+def test_security_scan_uses_glm_json_output(mock_build_client: MagicMock) -> None:
+ response = mock_build_client.return_value.chat.completions.create.return_value
response.choices = [MagicMock()]
response.choices[
0
].message.content = '{"passed":true,"findings":[{"severity":"low","detail":"Pinned development tool"}]}'
- result = _judge_spec_safety("apt_packages:\n - git", team_id=42, repository="posthog/posthog")
+ result = _judge_spec_safety(
+ "apt_packages:\n - git", team_id=42, gateway_token="pha_user_token", repository="posthog/posthog"
+ )
assert result.passed is True
assert result.findings == [{"severity": "low", "detail": "Pinned development tool"}]
- mock_get_llm_client.assert_called_once_with(product=SCAN_JUDGE_PRODUCT, team_id=42, api_key=None)
- request = mock_get_llm_client.return_value.chat.completions.create.call_args.kwargs
- assert request["model"] == SCAN_JUDGE_MODEL
+ mock_build_client.assert_called_once_with(
+ api_key="pha_user_token",
+ ai_product=SCAN_JUDGE_AI_PRODUCT,
+ properties={"team_id": "42"},
+ distinct_id="team-42",
+ )
+ request = mock_build_client.return_value.chat.completions.create.call_args.kwargs
+ assert request["model"] == SCAN_JUDGE_MODEL == "zai-org/glm-5.2"
assert request["response_format"] == {"type": "json_object"}