fix(gateway): apply posthog property headers on openai routes#60725
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds PostHog header propagation to OpenAI-compatible LLM Gateway routes so request-scoped PostHog properties/flags are available during OpenAI request handling and callbacks.
Changes:
- Adds
Requestinjection andapply_posthog_context_from_headerscalls across OpenAI chat, responses, and transcription endpoints. - Adds a regression test verifying
x-posthog-property-*headers reach the request context for OpenAI chat completions. - Adds a type cast for non-streaming transcription handler return typing.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
services/llm-gateway/src/llm_gateway/api/openai.py |
Applies PostHog context extraction on OpenAI routes before delegating to shared handlers. |
services/llm-gateway/tests/test_openai.py |
Adds coverage confirming OpenAI route property headers populate request context. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
Prompt To Fix All With AIFix the following 1 code review issue. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 1
services/llm-gateway/tests/test_openai.py:94-141
**Test covers only one of eight modified endpoints**
The PR applies `apply_posthog_context_from_headers` to eight route handlers (`chat_completions`, `chat_completions_with_product`, `responses_v1`, `responses_v1_with_product`, `responses`, `responses_with_product`, `audio_transcriptions`, `audio_transcriptions_with_product`), but the new test only exercises `/v1/chat/completions`. Given the team's preference for parameterised tests, parametrising this test over the full set of relevant routes (at a minimum the three non-transcription variants) would give confidence that the header-to-context wiring holds for each endpoint, not just the one that was already easiest to reach.
Reviews (1): Last reviewed commit: "fix(llm-gateway): apply posthog property..." | Re-trigger Greptile |
brandonleung
approved these changes
May 29, 2026
PR overviewAll previously flagged issues have been addressed. No open security concerns remain on this pull request. Security reviewNo open security issues remain on this pull request. Fixed/addressed: 1 · PR risk: 0/10 |
The OpenAI chat-completions, responses, and transcription routes never called apply_posthog_context_from_headers, so x-posthog-property-* headers (e.g. a shared-key caller's team_id attribution override) were silently dropped — only the Anthropic routes applied them. As a result $ai_generation events fell back to the API key owner's team instead of the caller-supplied team. Apply the headers inline in each OpenAI endpoint, mirroring the Anthropic routes, and add an end-to-end test asserting the property reaches the request context during the LLM call. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
a4f4c6d to
5b0109e
Compare
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.
We were not applying headers to OpenAI routes, only to Anthropic ones, this adds support for doing that so we can use it for passing things like team id in django.