Skip to content

The agent engineering platform.

partners[openai]: only add stream_options to kwargs if requested (#23552)

Krista Pratico85e36b0f50d8
diff --git a/libs/partners/openai/langchain_openai/chat_models/base.py b/libs/partners/openai/langchain_openai/chat_models/base.py
index 1c50e80b90..b9c758024a 100644
--- a/libs/partners/openai/langchain_openai/chat_models/base.py
+++ b/libs/partners/openai/langchain_openai/chat_models/base.py
@@ -1672,7 +1672,13 @@ class ChatOpenAI(BaseChatOpenAI):
) -> Iterator[ChatGenerationChunk]:
"""Set default stream_options."""
stream_usage = self._should_stream_usage(stream_usage, **kwargs)
- kwargs["stream_options"] = {"include_usage": stream_usage}
+ # Note: stream_options is not a valid parameter for Azure OpenAI.
+ # To support users proxying Azure through ChatOpenAI, here we only specify
+ # stream_options if include_usage is set to True.
+ # See https://learn.microsoft.com/en-us/azure/ai-services/openai/whats-new
+ # for release notes.
+ if stream_usage:
+ kwargs["stream_options"] = {"include_usage": stream_usage}
return super()._stream(*args, **kwargs)
@@ -1681,7 +1687,8 @@ class ChatOpenAI(BaseChatOpenAI):
) -> AsyncIterator[ChatGenerationChunk]:
"""Set default stream_options."""
stream_usage = self._should_stream_usage(stream_usage, **kwargs)
- kwargs["stream_options"] = {"include_usage": stream_usage}
+ if stream_usage:
+ kwargs["stream_options"] = {"include_usage": stream_usage}
async for chunk in super()._astream(*args, **kwargs):
yield chunk