Skip to content

feat(ai): add posthog_provider_override to the OpenAI wrapper - #822

Open
marco-g-pm wants to merge 3 commits into
mainfrom
marco/add-provider-override
Open

feat(ai): add posthog_provider_override to the OpenAI wrapper#822
marco-g-pm wants to merge 3 commits into
mainfrom
marco/add-provider-override

Conversation

@marco-g-pm

Copy link
Copy Markdown
Contributor

Adds a per-call posthog_provider_override to the OpenAI wrapper so OpenAI-compatible providers report their real provider.

Why

posthog.ai.openai hardcodes provider="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 custom base_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 posthogProviderOverride in POSTHOG_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_provider stays "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

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>
@github-actions

github-actions Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-07-31 21:25:04 UTC
Duration: 338590ms

✅ All Tests Passed!

111/111 tests passed


Capture_V1 Tests

94/94 tests passed

View Details
Test Status Duration
Endpoint And Method.Targets V1 Endpoint 513ms
Endpoint And Method.Does Not Use Legacy Endpoints 1007ms
Required Headers.Has Authorization Bearer Header 1007ms
Required Headers.Has Content Type Json 1008ms
Required Headers.Has Posthog Sdk Info Format 1007ms
Required Headers.Has Posthog Attempt Header 1007ms
Required Headers.Has Posthog Request Id 1007ms
Required Headers.Has Posthog Request Timestamp 1007ms
Required Headers.Has User Agent 1008ms
Body Format.Body Has Created At And Batch 1006ms
Body Format.No Api Key In Body 1008ms
Body Format.No Sent At In Body 1006ms
Event Format.Event Has Required Root Fields 1007ms
Event Format.Event Uuid Is Valid 1008ms
Event Format.Event Timestamp Is Rfc3339 1007ms
Event Format.Distinct Id Is String 1007ms
Event Format.Distinct Id At Root Not Properties 1008ms
Event Format.Custom Properties Preserved 1007ms
Event Format.Set Properties Preserved 1007ms
Event Format.Set Once Properties Preserved 1007ms
Event Format.Groups Properties Preserved 1007ms
Event Format.Sdk Generates Uuid If Not Provided 1007ms
Event Format.Event Has Required Root Fields Batch 1011ms
Event Format.Event Uuid Is Valid Batch 1010ms
Event Format.Event Timestamp Is Rfc3339 Batch 1011ms
Event Format.Distinct Id Is String Batch 1009ms
Event Format.Distinct Id At Root Not Properties Batch 1009ms
Event Format.Custom Properties Preserved Batch 1010ms
Event Format.Set Properties Preserved Batch 1010ms
Event Format.Set Once Properties Preserved Batch 1010ms
Event Format.Groups Properties Preserved Batch 1009ms
Event Format.Sdk Generates Uuid If Not Provided Batch 1010ms
Batch Behavior.Multiple Events In Single Batch 1505ms
Batch Behavior.Batch Envelope Smoke 1011ms
Batch Behavior.Flush With No Events Sends Nothing 1004ms
Batch Behavior.Flush At Triggers Batch 1507ms
Batch Behavior.Created At Reflects Batch Creation Time 1008ms
Deduplication.Generates Unique Uuids 1506ms
Deduplication.Different Events Same Content Different Uuids 1506ms
Deduplication.Preserves Uuid On Retry 7509ms
Deduplication.Preserves Timestamp On Retry 7515ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 7506ms
Deduplication.No Duplicate Events In Batch 1506ms
Header Behavior On Retry.Attempt Header Starts At One 1007ms
Header Behavior On Retry.Attempt Header Increments On Retry 14520ms
Header Behavior On Retry.Request Id Preserved On Retry 7508ms
Header Behavior On Retry.Different Requests Have Different Request Ids 3512ms
Header Behavior On Retry.Request Timestamp Changes On Retry 7511ms
Response Format Validation.Success Response Has Uuid Keyed Results 1006ms
Response Format Validation.Success Response Has Ok For Each Event 1505ms
Response Format Validation.Success No Retry After When All Ok 1506ms
Response Format Validation.Success Retry After Present When Retry Events 2509ms
Response Format Validation.Success No Retry After When Drop Only 1506ms
Response Format Validation.Response Echoes Request Id 1007ms
Retry Behavior.Retries On 408 7514ms
Retry Behavior.Retries On 500 7509ms
Retry Behavior.Retries On 503 9514ms
Retry Behavior.Retries On 504 7511ms
Retry Behavior.Retryable Errors Have Retry After 4507ms
Retry Behavior.Respects Retry After On Retryable Error 12515ms
Retry Behavior.Does Not Retry On 400 3502ms
Retry Behavior.Does Not Retry On 401 3506ms
Retry Behavior.Does Not Retry On 402 3507ms
Retry Behavior.Does Not Retry On 413 3507ms
Retry Behavior.Does Not Retry On 415 3506ms
Retry Behavior.Non Retryable Errors Have No Retry After 3507ms
Retry Behavior.Implements Backoff 23528ms
Retry Behavior.Max Retries Respected 23520ms
Partial Batch Handling.Handles 200 Full Success 2998ms
Partial Batch Handling.Handles 200 With All Ok 4506ms
Partial Batch Handling.Does Not Retry Dropped Events 4508ms
Partial Batch Handling.Does Not Retry Limited Events 4504ms
Partial Batch Handling.Prunes Ok Events On Partial Retry 7511ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry 7515ms
Partial Batch Handling.Retries Only Retry Events From Partial 7505ms
Partial Batch Handling.Partial Retry Preserves Uuids 7510ms
Partial Batch Handling.Partial Retry Attempt Header Increments 7513ms
Partial Batch Handling.Partial Retry Request Id Preserved 7507ms
Partial Batch Handling.Respects Retry After On Partial 9515ms
Partial Batch Handling.Unknown Result Treated As Terminal 4502ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry 4509ms
Compression.Sends Gzip Content Encoding 1006ms
Compression.No Content Encoding When Disabled 1007ms
Compression.Compressed Body Is Decompressible 1007ms
Error Handling.Does Not Retry On Unknown 4Xx 3507ms
Event Options.Cookieless Mode Override 1007ms
Event Options.Disable Skew Correction Override 1007ms
Event Options.Process Person Profile Override 1007ms
Event Options.Product Tour Id Override 1008ms
Event Options.Unset Options Omitted 1007ms
Event Options.Options Override In Batch 1010ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties 1006ms
Geoip And Historical Migration.Historical Migration Set In Body 1008ms
Geoip And Historical Migration.Historical Migration Absent By Default 1007ms

Feature_Flags Tests

17/17 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 1005ms
Request Payload.Flags Request Uses V2 Query Param 1006ms
Request Payload.Flags Request Hits Flags Path Not Decide 1006ms
Request Payload.Flags Request Omits Authorization Header 1005ms
Request Payload.Token In Flags Body Matches Init 1006ms
Request Payload.Groups Round Trip 1005ms
Request Payload.Groups Default To Empty Object 1006ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 1006ms
Request Payload.Disable Geoip Omitted Defaults To False 1005ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 1006ms
Request Lifecycle.No Flags Request On Init Alone 502ms
Request Lifecycle.No Flags Request On Normal Capture 1506ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 1009ms
Request Lifecycle.Mock Response Value Is Returned To Caller 1002ms
Retry Behavior.Retries Flags On 502 1006ms
Retry Behavior.Retries Flags On 504 1006ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 1507ms

posthog_provider_override is a new public parameter on the OpenAI wrapper,
so the recorded surface changed.
@marco-g-pm
marco-g-pm marked this pull request as ready for review July 31, 2026 21:18
@marco-g-pm
marco-g-pm requested a review from a team as a code owner July 31, 2026 21:18
@marco-g-pm
marco-g-pm requested a review from a team July 31, 2026 21:18
@marco-g-pm marco-g-pm self-assigned this Jul 31, 2026
@greptile-apps

greptile-apps Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "Merge branch 'main' into marco/add-provi..." | Re-trigger Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants