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 Convert Input For Streaming

From Leeroopedia
Knowledge Sources
Domains NLP, Data_Preprocessing
Last Updated 2026-02-11 00:00 GMT

Overview

Concrete tool for normalizing inputs in the streaming path provided by langchain-core (same method as non-streaming).

Description

The BaseChatModel._convert_input() method is called at the beginning of both stream() and astream() to normalize the input. It is the exact same method used in the non-streaming invoke() path.

Usage

Automatically invoked within stream() and astream(). Users pass the same input formats as invoke().

Code Reference

Source Location

  • Repository: langchain
  • File: libs/core/langchain_core/language_models/chat_models.py
  • Lines: L375-386 (_convert_input), L480-560 (used in stream), L562-654 (used in astream)

Signature

def _convert_input(self, model_input: LanguageModelInput) -> PromptValue:

Import

from langchain_core.language_models import BaseChatModel

I/O Contract

Inputs

Name Type Required Description
model_input LanguageModelInput Yes str, list[BaseMessage], or PromptValue

Outputs

Name Type Description
return PromptValue Normalized prompt for the streaming pipeline

Usage Examples

Streaming with Various Input Types

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o-mini")

# All input types work identically for streaming:
for chunk in llm.stream("Hello!"):
    print(chunk.content, end="")

for chunk in llm.stream([("system", "Be brief."), ("human", "Hello!")]):
    print(chunk.content, end="")

Related Pages

Implements Principle

Page Connections

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