feat(ai): add posthog_provider_override to the OpenAI wrapper - #822
Merged
Conversation
posthog.ai.openai.OpenAI is also used against OpenAI-compatible endpoints (DeepSeek, Groq, Mistral, Together, Fireworks, xAI, Perplexity, Ollama, Cerebras, gateways) via a custom base_url, but always reported $ai_provider: "openai", breaking cost attribution for those calls. Adds a per-call posthog_provider_override parameter, matching the JS SDK's posthogProviderOverride, across chat completions, the Responses API, .parse(), and embeddings, for sync, async, and streaming. Omitting it leaves $ai_provider as "openai" exactly as before. Scoped to posthog/ai/openai/ only: the override is folded into posthog_properties (which every capture path already merges in last) rather than touching the provider value used for response-format selection, so the shared posthog/ai/utils.py helpers and the Anthropic/Gemini wrappers are untouched. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Contributor
posthog-python Compliance ReportDate: 2026-07-31 21:25:04 UTC ✅ All Tests Passed!111/111 tests passed Capture_V1 Tests✅ 94/94 tests passed View Details
Feature_Flags Tests✅ 17/17 tests passed View Details
|
posthog_provider_override is a new public parameter on the OpenAI wrapper, so the recorded surface changed.
marco-g-pm
marked this pull request as ready for review
July 31, 2026 21:18
Contributor
|
Reviews (1): Last reviewed commit: "Merge branch 'main' into marco/add-provi..." | Re-trigger Greptile |
marandaneto
approved these changes
Aug 1, 2026
Angiephoenix
pushed a commit
to Angiephoenix/context-mill
that referenced
this pull request
Aug 5, 2026
Matches PostHog/posthog#75976 ("update docs to match SDK use over OTel"), which rewrites all 40 AIO installation pages: OTel out (-207 mentions, -92 PostHogSpanProcessor, -46 TracerProvider), wrapper client + manual $ai_span in (+69). The stated reason is that OTel makes the full session tree hard to build and maintain, which is this skill's whole deliverable. - 2-install: providers and gateways now install just the PostHog SDK beside the vendor SDK. No opentelemetry packages at all. OTel packages narrowed to the opentelemetry-* variants and LlamaIndex. - 3-instrument: wrapper client is the default mechanism; OTel demoted to the variants whose docs still call for it. Adds the gateway base_url shape. - 4-nesting: rewritten. Identity is per call (posthog_distinct_id, posthog_trace_id, posthog_properties). Trace id is per turn and must be shared across a turn's calls. Reverses the previous rule against authoring spans: the docs now require capturing each tool execution as an $ai_span event, since the wrapper never sees the app's dispatch loop. Framework variants still emit their own. - 1-begin: restores the gateway-provider consequence, now real for the right reason - the wrapper reports openai whatever host it targets, and pricing keys on $ai_model + $ai_provider (see PostHog/posthog-python#822). - 5-verify: expectations follow the new tree, including a $ai_provider check. The three upstream PRs are still open, so the bundled install pages in each zip remain the old OTel content until they merge and the docs are re-fetched. 3-instrument carries a note that the wrapper shape wins over a stale bundled page; it can come out once #75976 lands. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a per-call
posthog_provider_overrideto the OpenAI wrapper so OpenAI-compatible providers report their real provider.Why
posthog.ai.openaihardcodesprovider="openai"when building events. That's correct for OpenAI, but the same wrapper is what users point at OpenAI-compatible providers — DeepSeek, Groq, Mistral, Together, Fireworks, xAI, Perplexity, Ollama, Cerebras and various gateways — by passing a custombase_url.PostHog's ingestion looks up token pricing by
$ai_model+$ai_provider, so a DeepSeek call currently reports$ai_provider: "openai"with$ai_model: "deepseek-chat", and cost attribution is wrong or fails to match.This came up while updating the AI observability installation docs to recommend the SDK wrappers over OpenTelemetry (PostHog/posthog#75976). Those pages work around it today by passing
posthog_properties={"$ai_provider": ...}; this gives them a first-class parameter instead.Parity
The JS SDK already has this as
posthogProviderOverrideinPOSTHOG_PARAMS_MAP. Python had no equivalent.Scope
OpenAI wrapper only. Anthropic and Gemini are used with their actual providers and don't have this problem, so they're untouched.
Covers sync, async, and streaming. Omitting the parameter leaves behavior byte-identical —
$ai_providerstays"openai".Testing
71 passed, 1 skipped (pre-existing, needs a live
OPENAI_API_KEY). ruff format/check and repo-wide mypy clean.Tests assert the override applies, that the default is unchanged, and that the parameter never leaks into the payload sent to OpenAI.
🤖 Generated with Claude Code