Implementation:Langchain ai Langchain ChatOpenAI Constructor For Streaming
Appearance
| Knowledge Sources | |
|---|---|
| Domains | NLP, Streaming |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
Concrete tool for initializing OpenAI chat models with streaming configuration provided by the LangChain OpenAI integration.
Description
The same ChatOpenAI class is used for streaming, with specific attention to streaming-related fields: streaming (default streaming behavior), disable_streaming (conditional override), and stream_usage (token usage in chunks). When streaming=True, even invoke() calls use the streaming path internally.
Usage
Initialize ChatOpenAI with streaming=True for applications that primarily use streaming, or leave it as False and use stream() explicitly.
Code Reference
Source Location
- Repository: langchain
- File: libs/partners/openai/langchain_openai/chat_models/base.py
- Lines: L513-2260 (BaseChatOpenAI), L2262-3493 (ChatOpenAI)
Signature
class BaseChatOpenAI(BaseChatModel):
# Streaming-relevant fields:
streaming: bool = False
disable_streaming: bool | Literal["tool_calling"] = False
stream_usage: bool = True
Import
from langchain_openai import ChatOpenAI
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| streaming | bool | No (default: False) | Enable streaming by default in invoke() |
| disable_streaming | bool or Literal["tool_calling"] | No (default: False) | Disable streaming globally or for tool calls |
| stream_usage | bool | No (default: True) | Include token usage metadata in stream chunks |
Outputs
| Name | Type | Description |
|---|---|---|
| instance | ChatOpenAI | Model configured for streaming |
Usage Examples
Streaming Configuration
from langchain_openai import ChatOpenAI
# Streaming by default (invoke() will stream internally)
llm = ChatOpenAI(model="gpt-4o-mini", streaming=True)
# Explicit streaming (invoke() does not stream, stream() does)
llm = ChatOpenAI(model="gpt-4o-mini")
for chunk in llm.stream("Tell me a story"):
print(chunk.content, end="", flush=True)
Related Pages
Implements Principle
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment