feat(ai): add posthog_provider_override to the OpenAI wrapper - #822
Open
marco-g-pm wants to merge 3 commits into
Open
feat(ai): add posthog_provider_override to the OpenAI wrapper#822marco-g-pm wants to merge 3 commits into
marco-g-pm wants to merge 3 commits into
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
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