: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): route default client ai events at all stage (#69400)
Carlos Marchale317dfe7a886
diff --git a/posthog/apps.py b/posthog/apps.pyindex 7a1ce2792c57..a080decbd310 100644--- a/posthog/apps.py+++ b/posthog/apps.py@@ -9,6 +9,7 @@ from asgiref.sync import async_to_syncfrom posthoganalytics.client import Clientfrom posthog.git import get_git_branch, get_git_commit_short+from posthog.ph_client import enable_dedicated_ai_endpoint_for_default_clientfrom posthog.utils import (_build_flag_provider,get_available_timezones_with_offsets,@@ -126,6 +127,10 @@ class PostHogConfig(AppConfig):if not posthoganalytics.disabled and posthoganalytics.feature_flag_definitions() is None:posthoganalytics.load_feature_flags()+ # The feature_flag_definitions() call above constructs the default client, so+ # the dedicated-AI-endpoint flag can only be applied from this point on.+ enable_dedicated_ai_endpoint_for_default_client()+from posthog.async_migrations.setup import setup_async_migrationsif settings.SKIP_ASYNC_MIGRATIONS_SETUP:diff --git a/posthog/ph_client.py b/posthog/ph_client.pyindex 239b5876fd5c..b491f52fce57 100644--- a/posthog/ph_client.py+++ b/posthog/ph_client.py@@ -31,6 +31,27 @@ def _use_dedicated_ai_endpoint(caller_stage: DedicatedAIEndpointRollout) -> boolreturn _DEDICATED_AI_ENDPOINT_STAGES.index(rollout) >= _DEDICATED_AI_ENDPOINT_STAGES.index(caller_stage)+def enable_dedicated_ai_endpoint_for_default_client() -> None:+ """Route the module-level default client's `$ai_*` events to the dedicated AI+ endpoint at the `all` rollout stage.++ Deliberate workaround: the SDK's lazy `setup()` doesn't accept+ `_dedicated_ai_endpoint`, and we want to finish testing the endpoint on our own+ traffic before rethinking the flag as a public option threaded through the+ SDK's normal construction paths. Mutating the constructed client is safe: it+ and its consumers read the flag per batch, and the SDK's post-fork consumer+ rebuild copies it from the old consumers.+ """+ if not _use_dedicated_ai_endpoint(DedicatedAIEndpointRollout.ALL):+ return+ client = posthoganalytics.default_client+ if client is None:+ return+ client._dedicated_ai_endpoint = True+ for consumer in client.consumers or []:+ consumer.dedicated_ai_endpoint = True++def feature_enabled_or_false(key: str,distinct_id: Number | str | UUID | int,diff --git a/posthog/test/test_ph_client.py b/posthog/test/test_ph_client.pyindex c905fba2f318..6e2ef0346bff 100644--- a/posthog/test/test_ph_client.py+++ b/posthog/test/test_ph_client.py@@ -1,8 +1,10 @@from django.test import SimpleTestCase, override_settings+import posthoganalyticsfrom parameterized import parameterized+from posthoganalytics import Posthog-from posthog.ph_client import get_client+from posthog.ph_client import enable_dedicated_ai_endpoint_for_default_client, get_clientfrom posthog.settings.ingestion import (DedicatedAIEndpointRollout as Rollout,_parse_dedicated_ai_rollout,@@ -44,3 +46,24 @@ class TestDedicatedAIEndpointRollout(SimpleTestCase):)def test_parse_rollout_falls_back_to_off_on_invalid(self, value, expected):self.assertEqual(_parse_dedicated_ai_rollout(value), expected)++ @parameterized.expand(+ [+ (Rollout.OFF, False),+ (Rollout.RUNNER, False),+ (Rollout.ALL, True),+ ]+ )+ def test_default_client_routes_ai_events_only_at_full_rollout(self, rollout, expected):+ client = Posthog("test-key", send=False, enable_local_evaluation=False)+ original = posthoganalytics.default_client+ posthoganalytics.default_client = client # ty: ignore[invalid-assignment]+ try:+ with override_settings(POSTHOG_DEDICATED_AI_ENDPOINT_ROLLOUT=rollout):+ enable_dedicated_ai_endpoint_for_default_client()+ finally:+ posthoganalytics.default_client = original+ self.assertEqual(client._dedicated_ai_endpoint, expected)+ self.assertTrue(client.consumers)+ for consumer in client.consumers:+ self.assertEqual(consumer.dedicated_ai_endpoint, expected)