Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Langchain ai Langchain BaseChatModel Should Stream

From Leeroopedia
Knowledge Sources
Domains Control_Flow, Streaming
Last Updated 2026-02-11 00:00 GMT

Overview

Concrete tool for determining the streaming execution path provided by langchain-core.

Description

The BaseChatModel._should_stream() method inspects the model's configuration and capabilities to decide whether to use streaming. It checks whether the subclass has overridden _stream() or _astream(), respects the disable_streaming flag, and handles the special case of disabling streaming only during tool calling.

Usage

This is an internal method. It is called within invoke() to determine whether to use _generate() or _stream() for processing the request.

Code Reference

Source Location

  • Repository: langchain
  • File: libs/core/langchain_core/language_models/chat_models.py
  • Lines: L439-477

Signature

def _should_stream(
    self,
    *,
    async_api: bool,
    run_manager: CallbackManagerForLLMRun
    | AsyncCallbackManagerForLLMRun
    | None = None,
    **kwargs: Any,
) -> bool:

Import

# Internal method — accessed via BaseChatModel instance
from langchain_core.language_models import BaseChatModel

I/O Contract

Inputs

Name Type Required Description
async_api bool Yes Whether the async API path is being used
run_manager CallbackManagerForLLMRun or None No Callback manager for the current run
**kwargs Any No Additional keyword arguments (checked for tool binding)

Outputs

Name Type Description
return bool True if streaming path should be used, False for non-streaming

Usage Examples

Controlling Streaming Behavior

from langchain_openai import ChatOpenAI

# Streaming is enabled by default when _stream() is implemented
llm = ChatOpenAI(model="gpt-4o-mini")

# Disable streaming entirely
llm = ChatOpenAI(model="gpt-4o-mini", disable_streaming=True)

# Disable streaming only during tool calling
llm = ChatOpenAI(model="gpt-4o-mini", disable_streaming="tool_calling")

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment