Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .sampo/changesets/openai-stream-options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
pypi/posthog: patch
---

Fix streaming OpenAI chat completions when `stream_options` is `None` or `NOT_GIVEN` (it raised a `TypeError`), and stop adding `include_usage` to the `stream_options` dict the caller passed in.
9 changes: 6 additions & 3 deletions posthog/ai/openai/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,12 @@ def _create_streaming(
):
start_time = time.time()
state = _ChatCompletionsStreamState()
if "stream_options" not in kwargs:
kwargs["stream_options"] = {}
kwargs["stream_options"]["include_usage"] = True
# The caller's dict is left alone, and None / NOT_GIVEN mean "no options"
stream_options = kwargs.get("stream_options")
kwargs["stream_options"] = {
**(stream_options if isinstance(stream_options, dict) else {}),
"include_usage": True,
}
response = self._original.create(**kwargs)

def generator():
Expand Down
9 changes: 6 additions & 3 deletions posthog/ai/openai/openai_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,12 @@ async def _create_streaming(
start_time = time.time()
state = _ChatCompletionsStreamState()

if "stream_options" not in kwargs:
kwargs["stream_options"] = {}
kwargs["stream_options"]["include_usage"] = True
# The caller's dict is left alone, and None / NOT_GIVEN mean "no options"
stream_options = kwargs.get("stream_options")
kwargs["stream_options"] = {
**(stream_options if isinstance(stream_options, dict) else {}),
"include_usage": True,
}
response = await self._original.create(**kwargs)

async def async_generator():
Expand Down
64 changes: 64 additions & 0 deletions posthog/test/ai/openai/test_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest

try:
from openai import NOT_GIVEN
from openai.types.chat import ChatCompletion, ChatCompletionMessage
from openai.types.chat.chat_completion import Choice
from openai.types.chat.chat_completion_chunk import (
Expand Down Expand Up @@ -460,6 +461,33 @@ def test_embeddings(mock_client, mock_embedding_response):
assert isinstance(props["$ai_latency"], float)


@pytest.mark.parametrize("stream_options", [None, NOT_GIVEN, {"foo": "bar"}])
def test_streaming_stream_options_are_not_mutated(
mock_client, streaming_tool_call_chunks, stream_options
):
captured = {}

def mock_create(self, **kwargs):
captured["kwargs"] = kwargs
return iter(streaming_tool_call_chunks)

with patch("openai.resources.chat.completions.Completions.create", new=mock_create):
client = OpenAI(api_key="test-key", posthog_client=mock_client)
stream = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello"}],
stream=True,
stream_options=stream_options,
)
list(stream)

sent = captured["kwargs"]["stream_options"]
assert sent["include_usage"] is True
if isinstance(stream_options, dict):
assert sent == {"foo": "bar", "include_usage": True}
assert stream_options == {"foo": "bar"}


def test_groups(mock_client, mock_openai_response):
with patch(
"openai.resources.chat.completions.Completions.create",
Expand Down Expand Up @@ -1421,6 +1449,42 @@ async def chunk_iterable():
assert isinstance(props["$ai_latency"], float)


@pytest.mark.asyncio
@pytest.mark.parametrize("stream_options", [None, NOT_GIVEN, {"foo": "bar"}])
async def test_async_streaming_stream_options_are_not_mutated(
mock_client, streaming_tool_call_chunks, stream_options
):
captured = {}

async def mock_create(self, **kwargs):
captured["kwargs"] = kwargs

async def chunk_iterable():
for chunk in streaming_tool_call_chunks:
yield chunk

return chunk_iterable()

with patch(
"openai.resources.chat.completions.AsyncCompletions.create", new=mock_create
):
client = AsyncOpenAI(api_key="test-key", posthog_client=mock_client)
stream = await client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello"}],
stream=True,
stream_options=stream_options,
)
async for _ in stream:
pass

sent = captured["kwargs"]["stream_options"]
assert sent["include_usage"] is True
if isinstance(stream_options, dict):
assert sent == {"foo": "bar", "include_usage": True}
assert stream_options == {"foo": "bar"}


@pytest.mark.asyncio
async def test_async_embeddings_create(mock_client, mock_embedding_response):
mock_create = AsyncMock(return_value=mock_embedding_response)
Expand Down
Loading