: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(replay): route ai regex through the ai gateway (#128188)
Jonathan Mieloof9fea0e8edf8
diff --git a/posthog/session_recordings/openai_client.py b/posthog/session_recordings/openai_client.pydeleted file mode 100644index 39ce0e8f35c3..000000000000--- a/posthog/session_recordings/openai_client.py+++ /dev/null@@ -1,39 +0,0 @@-"""OpenAI client for the replay AI regex endpoint.--Lifted out of the retired session-summaries package, which owned it only because that was the first-replay feature to call OpenAI directly.-"""--import os--from django.conf import settings--import posthoganalytics-from posthoganalytics.ai.openai import OpenAI-from posthoganalytics.client import Client-from rest_framework import exceptions--from posthog.cloud_utils import is_cloud--BASE_LLM_CALL_TIMEOUT_S = 600.0---def _get_default_posthog_client() -> Client:- """Return the default analytics client after validating the environment."""- if not settings.DEBUG and not is_cloud():- raise exceptions.ValidationError("AI features are only available in PostHog Cloud")-- if not os.environ.get("OPENAI_API_KEY"):- raise exceptions.ValidationError("OpenAI API key is not configured")-- client = posthoganalytics.default_client- if not client:- raise exceptions.ValidationError("PostHog analytics client is not configured")-- return client---def get_openai_client() -> OpenAI:- """Get configured OpenAI client or raise appropriate error."""- client = _get_default_posthog_client()- return OpenAI(posthog_client=client, timeout=BASE_LLM_CALL_TIMEOUT_S, base_url=settings.OPENAI_BASE_URL)diff --git a/posthog/session_recordings/session_recording_api.py b/posthog/session_recordings/session_recording_api.pyindex a72d09e20d67..d60dfa600dd2 100644--- a/posthog/session_recordings/session_recording_api.py+++ b/posthog/session_recordings/session_recording_api.py@@ -78,6 +78,7 @@ from posthog.event_usage import report_user_actionfrom posthog.exceptions import ClickHouseAtCapacityfrom posthog.exceptions_capture import capture_exceptionfrom posthog.helpers.impersonation import is_impersonated+from posthog.llm.gateway_client import build_openai_client, openai_gateway_configuredfrom posthog.models import Organization, Team, Userfrom posthog.models.activity_logging.activity_log import Detail, log_activityfrom posthog.models.comment import Comment@@ -96,7 +97,6 @@ from posthog.session_recordings.ai_data.ai_regex_prompts import AI_REGEX_PROMPTSfrom posthog.session_recordings.ai_data.ai_regex_schema import AiRegexSchemafrom posthog.session_recordings.models.session_recording import SessionRecordingfrom posthog.session_recordings.models.session_recording_event import SessionRecordingViewed-from posthog.session_recordings.openai_client import get_openai_clientfrom posthog.session_recordings.queries.console_logs import (MAX_CONSOLE_LOGS_PAGE_SIZE,MAX_CONSOLE_MESSAGE_LENGTH,@@ -2009,18 +2009,22 @@ class SessionRecordingViewSet(user_content=clean_prompt_whitespace(request.data["regex"]),)- client = get_openai_client()+ if not openai_gateway_configured():+ raise exceptions.ValidationError("AI features are only available in PostHog Cloud")++ distinct_id = self._distinct_id_from_request(request)+ client = build_openai_client(+ "django",+ ai_product="session_replay",+ distinct_id=distinct_id,+ properties={"ai_feature": "ai_regex", "team_id": str(self.team.id)},+ )completion = client.beta.chat.completions.parse(model=SESSION_REPLAY_AI_REGEX_MODEL,messages=messages,response_format=AiRegexSchema,- posthog_distinct_id=self._distinct_id_from_request(request),- posthog_properties={- "ai_product": "session_replay",- "ai_feature": "ai_regex",- "team_id": self.team.id,- },+ user=distinct_id,)if not completion.choices or not completion.choices[0].message.content: