diff --git a/.sampo/changesets/openai-stream-options.md b/.sampo/changesets/openai-stream-options.md new file mode 100644 index 000000000..8d5fb4778 --- /dev/null +++ b/.sampo/changesets/openai-stream-options.md @@ -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. diff --git a/posthog/ai/openai/openai.py b/posthog/ai/openai/openai.py index ccdf6e69a..d8654f090 100644 --- a/posthog/ai/openai/openai.py +++ b/posthog/ai/openai/openai.py @@ -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(): diff --git a/posthog/ai/openai/openai_async.py b/posthog/ai/openai/openai_async.py index 07e0de670..95a898139 100644 --- a/posthog/ai/openai/openai_async.py +++ b/posthog/ai/openai/openai_async.py @@ -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(): diff --git a/posthog/test/ai/openai/test_openai.py b/posthog/test/ai/openai/test_openai.py index a0a550191..6a5f34cf4 100644 --- a/posthog/test/ai/openai/test_openai.py +++ b/posthog/test/ai/openai/test_openai.py @@ -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 ( @@ -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", @@ -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)