-
Notifications
You must be signed in to change notification settings - Fork 76
fix(ai): route async OpenAI streaming through capture_streaming_event #861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
marandaneto
merged 7 commits into
PostHog:main
from
ckarnell:fix/openai-async-streaming-parity
Aug 12, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bcd5797
fix(ai): route async OpenAI streaming through capture_streaming_event
ckarnell 6e9e243
Fix CI: silence F811 on imported fixtures, refresh the API snapshot
ckarnell ade11ac
Move the shared OpenAI fixtures into conftest.py
ckarnell ea6ebc0
chore: add OpenAI async streaming changeset
marandaneto b35d5b9
fix(ai): address async streaming review feedback
marandaneto 049e213
fix(ai): preserve async streaming token source
marandaneto ab2a639
Merge branch 'main' into fix/openai-async-streaming-parity
marandaneto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| pypi/posthog: patch | ||
| --- | ||
|
|
||
| Fix async OpenAI streaming captures to include token usage and other generation properties emitted by synchronous streams. |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| """Fixtures shared by the OpenAI test modules. | ||
|
|
||
| Defined here rather than in a test module so pytest supplies them by discovery. Importing them | ||
| between test files bound the names in the importing module and tripped Ruff F811. | ||
| """ | ||
|
|
||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
| from openai.types.chat.chat_completion_chunk import ( | ||
| ChatCompletionChunk, | ||
| ChoiceDelta, | ||
| ChoiceDeltaToolCall, | ||
| ChoiceDeltaToolCallFunction, | ||
| ) | ||
| from openai.types.chat.chat_completion_chunk import ( | ||
| Choice as ChoiceChunk, | ||
| ) | ||
| from openai.types.completion_usage import CompletionUsage | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_client(): | ||
| with patch("posthog.client.Client") as mock_client: | ||
| mock_client.privacy_mode = False | ||
| yield mock_client | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def streaming_tool_call_chunks(): | ||
| return [ | ||
| ChatCompletionChunk( | ||
| id="chunk1", | ||
| model="gpt-4", | ||
| object="chat.completion.chunk", | ||
| created=1234567890, | ||
| choices=[ | ||
| ChoiceChunk( | ||
| index=0, | ||
| delta=ChoiceDelta( | ||
| role="assistant", | ||
| tool_calls=[ | ||
| ChoiceDeltaToolCall( | ||
| index=0, | ||
| id="call_abc123", | ||
| type="function", | ||
| function=ChoiceDeltaToolCallFunction( | ||
| name="get_weather", | ||
| arguments='{"location": "', | ||
| ), | ||
| ) | ||
| ], | ||
| ), | ||
| finish_reason=None, | ||
| ) | ||
| ], | ||
| ), | ||
| ChatCompletionChunk( | ||
| id="chunk2", | ||
| model="gpt-4", | ||
| object="chat.completion.chunk", | ||
| created=1234567891, | ||
| choices=[ | ||
| ChoiceChunk( | ||
| index=0, | ||
| delta=ChoiceDelta( | ||
| tool_calls=[ | ||
| ChoiceDeltaToolCall( | ||
| index=0, | ||
| id="call_abc123", | ||
| type="function", | ||
| function=ChoiceDeltaToolCallFunction( | ||
| arguments='San Francisco"', | ||
| ), | ||
| ) | ||
| ], | ||
| ), | ||
| finish_reason=None, | ||
| ) | ||
| ], | ||
| ), | ||
| ChatCompletionChunk( | ||
| id="chunk3", | ||
| model="gpt-4", | ||
| object="chat.completion.chunk", | ||
| created=1234567892, | ||
| choices=[ | ||
| ChoiceChunk( | ||
| index=0, | ||
| delta=ChoiceDelta( | ||
| tool_calls=[ | ||
| ChoiceDeltaToolCall( | ||
| index=0, | ||
| id="call_abc123", | ||
| type="function", | ||
| function=ChoiceDeltaToolCallFunction( | ||
| arguments=', "unit": "celsius"}', | ||
| ), | ||
| ) | ||
| ], | ||
| ), | ||
| finish_reason=None, | ||
| ) | ||
| ], | ||
| ), | ||
| ChatCompletionChunk( | ||
| id="chunk4", | ||
| model="gpt-4", | ||
| object="chat.completion.chunk", | ||
| created=1234567893, | ||
| choices=[ | ||
| ChoiceChunk( | ||
| index=0, | ||
| delta=ChoiceDelta( | ||
| content="The weather in San Francisco is 15掳C.", | ||
| ), | ||
| finish_reason=None, | ||
| ) | ||
| ], | ||
| usage=CompletionUsage( | ||
| prompt_tokens=20, | ||
| completion_tokens=15, | ||
| total_tokens=35, | ||
| ), | ||
| ), | ||
| ] |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.