: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(aio): delegate system one gateway authorization to the endpoint (#110874)
Bernat Torres5317cc451f2d
diff --git a/docs/internal/ai-observability-judge-inputs.md b/docs/internal/ai-observability-judge-inputs.mdindex 9cd79e60414b..c4812f7808b1 100644--- a/docs/internal/ai-observability-judge-inputs.md+++ b/docs/internal/ai-observability-judge-inputs.md@@ -47,7 +47,7 @@ Both use the project's UUID as its group key; the numeric project ID is a groupDeploy the ingestion and evaluation worker changes before enabling the flag.Projects configure a System One-compatible deployment and its authentication.Evaluation connections never fall back to an instance credential or gateway configuration.-PostHog's regional AI gateway endpoints additionally require an organization in `POSTHOG_INTERNAL_ORG_IDS`; customer projects cannot use those endpoints.+The configured endpoint authorizes the supplied credential, including connections to PostHog's regional AI gateway endpoints.Connection validation and every evaluation check these gates; an absent flag or failed flag lookup blocks the call.Turning the flag off stops subsequent runs, including queued work, without disabling the saved evaluation.Keep the experimental flag limited to staff projects during rollout.diff --git a/posthog/temporal/ai_observability/test_run_evaluation.py b/posthog/temporal/ai_observability/test_run_evaluation.pyindex 568690cd559b..919768e4c5e2 100644--- a/posthog/temporal/ai_observability/test_run_evaluation.py+++ b/posthog/temporal/ai_observability/test_run_evaluation.py@@ -316,8 +316,8 @@ def _mock_config_with_active_key(provider: str = "openai") -> MagicMock:{"input_tokens": 120},),(- {"api_key": "example-token", "base_url": "https://decisions.example.com/v1"},- "https://decisions.example.com/v1",+ {"api_key": "example-token", "base_url": "https://ai-gateway.us.posthog.com/v1"},+ "https://ai-gateway.us.posthog.com/v1","example-judge-v1",{},),@@ -355,13 +355,14 @@ def test_system_one_judge_emits_boolean_probability_without_reasoning("evaluation_config": {"prompt": "Is the response polite?"},}with (+ override_settings(POSTHOG_INTERNAL_ORG_IDS=[]),patch("posthog.security.url_validation.resolve_host_ips", return_value={ip_address("8.8.8.8")}),patch("posthog.temporal.ai_observability.evaluation_llm_judge.model_spec") as spec,- patch(- "posthog.temporal.ai_observability.evaluation_llm_judge.system_one_evaluations_enabled", return_value=True- ),+ patch("products.ai_observability.backend.llm.system_one.Team.objects.only") as teams,+ patch("products.ai_observability.backend.llm.system_one.get_feature_flag_or_none", return_value=True),patch("httpx.AsyncHTTPTransport.handle_async_request", return_value=response) as request,):+ teams.return_value.get.return_value = Team(id=1, organization_id=uuid.uuid4(), uuid=uuid.uuid4())spec.return_value.resolve.return_value = resolvedresult = call_llm_judge(evaluation=evaluation,@@ -371,6 +372,9 @@ def test_system_one_judge_emits_boolean_probability_without_reasoning()assert str(request.call_args.args[0].url) == f"{base_url}/systemone"+ assert request.call_args.args[0].headers.get("Authorization") == (+ f"Bearer {connection_config['api_key']}" if connection_config["api_key"] else None+ )assert json.loads(request.call_args.args[0].content)["model"] == modelassert result["verdict"] is verdictassert result["reasoning"] == ""@@ -576,7 +580,11 @@ def test_system_one_numeric_requires_a_score_range(output_config: dict[str, floa@pytest.mark.parametrize("base_url,flag",- [("https://decisions.example.com/v1", False), ("https://ai-gateway.us.posthog.com/v1", True)],+ [+ ("https://decisions.example.com/v1", False),+ ("https://ai-gateway.us.posthog.com/v1", False),+ ("https://api.typesafe.ai/v1", True),+ ],)def test_system_one_restricted_connection_does_not_send_evaluation_data(base_url: str, flag: bool) -> None:with (diff --git a/products/ai_observability/backend/api/test/test_provider_keys.py b/products/ai_observability/backend/api/test/test_provider_keys.pyindex 0a26e24089d2..2d43772a203a 100644--- a/products/ai_observability/backend/api/test/test_provider_keys.py+++ b/products/ai_observability/backend/api/test/test_provider_keys.py@@ -174,7 +174,7 @@ class TestLLMProviderKeyViewSet(APIBaseTest):self.organization_membership.save()@parameterized.expand([("create",), ("update",), ("validate",), ("prevalidate",)])- def test_system_one_customer_project_cannot_use_posthog_gateway_or_disabled_feature(self, operation: str) -> None:+ def test_system_one_connection_requires_enabled_feature(self, operation: str) -> None:gateway_url = "https://ai-gateway.us.posthog.com/v1"key = LLMProviderKey.objects.create(team=self.team,@@ -188,7 +188,7 @@ class TestLLMProviderKeyViewSet(APIBaseTest):self.settings(POSTHOG_INTERNAL_ORG_IDS=[]),patch("products.ai_observability.backend.llm.system_one.get_feature_flag_or_none",- return_value=operation != "prevalidate",+ return_value=False,),patch("httpx.AsyncHTTPTransport.handle_async_request") as request,):@@ -209,7 +209,8 @@ class TestLLMProviderKeyViewSet(APIBaseTest):response = self.client.post(f"{base_url}provider_keys/{key.id}/validate/")else:response = self.client.post(- f"{base_url}provider_key_validations/", {"provider": "system_one", "api_key": "example-token"}+ f"{base_url}provider_key_validations/",+ {"provider": "system_one", "api_key": "example-token", "base_url": gateway_url},)self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN, response.data)request.assert_not_called()diff --git a/products/ai_observability/backend/llm/system_one.py b/products/ai_observability/backend/llm/system_one.pyindex bfc0a05e1fd6..ed563125927d 100644--- a/products/ai_observability/backend/llm/system_one.py+++ b/products/ai_observability/backend/llm/system_one.py@@ -4,8 +4,6 @@ from datetime import UTC, datetimefrom email.utils import parsedate_to_datetimefrom urllib.parse import urlsplit-from django.conf import settings-import httpxfrom posthog.llm.system_one import (@@ -45,12 +43,6 @@ def system_one_evaluations_enabled(team_id: int, *, base_url: str) -> bool:if not host or host == "typesafe.ai" or host.endswith(".typesafe.ai"):return Falseteam = Team.objects.only("uuid", "organization_id").get(id=team_id)- # Customer connections use their own provider account or deployment, never PostHog's gateway.- if (- host in {"ai-gateway.us.posthog.com", "ai-gateway.eu.posthog.com"}- and str(team.organization_id) not in settings.POSTHOG_INTERNAL_ORG_IDS- ):- return Falsereturn (get_feature_flag_or_none("llm-analytics-system-one-evaluations",diff --git a/products/ai_observability/backend/llm/test/test_system_one.py b/products/ai_observability/backend/llm/test/test_system_one.pyindex 47d72d0eda4f..5d4989439c1d 100644--- a/products/ai_observability/backend/llm/test/test_system_one.py+++ b/products/ai_observability/backend/llm/test/test_system_one.py@@ -46,27 +46,27 @@ def public_endpoint_dns() -> Iterator[None]:@pytest.mark.parametrize(- "base_url,internal,flag,enabled",+ "base_url,flag,enabled",[- ("https://api.typesafe.ai/v1", False, True, False),- ("", False, True, False),- ("https://decisions.example.com/v1", False, True, True),- ("https://decisions.example.com/v1", False, False, False),- ("https://api.typesafe.ai/v1", False, None, False),- ("https://ai-gateway.us.posthog.com/v1", False, True, False),- ("https://ai-gateway.eu.posthog.com/v1", False, True, False),- ("https://AI-GATEWAY.US.POSTHOG.COM.:443/v1", False, True, False),- ("https://ai-gateway.us.posthog.com/v1", False, True, False),- ("https://ai-gateway.us.posthog.com/v1", True, False, False),- ("https://ai-gateway.us.posthog.com/v1", True, True, True),+ ("https://api.typesafe.ai/v1", True, False),+ ("", True, False),+ ("https://decisions.example.com/v1", True, True),+ ("https://decisions.example.com/v1", False, False),+ ("https://decisions.example.com/v1", None, False),+ ("https://ai-gateway.us.posthog.com/v1", True, True),+ ("https://ai-gateway.eu.posthog.com/v1", True, True),+ ("https://AI-GATEWAY.US.POSTHOG.COM.:443/v1", True, True),+ ("https://ai-gateway.us.posthog.com/v1", True, True),+ ("https://ai-gateway.us.posthog.com/v1", False, False),+ ("https://ai-gateway.us.posthog.com/v1", None, False),],)-def test_system_one_connections_require_flag_and_reserve_posthog_gateway_for_internal_projects(- base_url: str, internal: bool, flag: bool | None, enabled: bool+def test_system_one_connections_require_flag_and_supported_endpoint(+ base_url: str, flag: bool | None, enabled: bool) -> None:team = Team(id=1, organization_id=uuid4(), uuid=uuid4())with (- override_settings(POSTHOG_INTERNAL_ORG_IDS=[str(team.organization_id)] if internal else []),+ override_settings(POSTHOG_INTERNAL_ORG_IDS=[]),patch("products.ai_observability.backend.llm.system_one.Team.objects.only") as teams,patch("products.ai_observability.backend.llm.system_one.get_feature_flag_or_none",