Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Langchain ai Langchain BaseChatModel Convert Input

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

Overview

Concrete tool for normalizing diverse chat model inputs into a unified PromptValue provided by langchain-core.

Description

The BaseChatModel._convert_input() method accepts any LanguageModelInput (a union of str, list[BaseMessage], and PromptValue) and returns a PromptValue. For strings, it wraps them in a StringPromptValue. For message lists, it wraps them in a ChatPromptValue. For PromptValue instances, it passes them through unchanged.

Usage

This is an internal method called automatically by invoke(), stream(), and generate(). It is not typically called directly by users.

Code Reference

Source Location

  • Repository: langchain
  • File: libs/core/langchain_core/language_models/chat_models.py
  • Lines: L375-386

Signature

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

Import

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

I/O Contract

Inputs

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

Outputs

Name Type Description
return PromptValue Normalized prompt containing messages for the model

Usage Examples

Automatic Conversion in invoke()

from langchain_openai import ChatOpenAI

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

# All of these work — _convert_input handles normalization:

# String input
response = llm.invoke("Hello, world!")

# Message list input
from langchain_core.messages import HumanMessage, SystemMessage
response = llm.invoke([
    SystemMessage(content="You are a helpful assistant."),
    HumanMessage(content="Hello!"),
])

# Tuple/dict shorthand
response = llm.invoke([
    ("system", "You are a helpful assistant."),
    ("human", "Hello!"),
])

Related Pages

Implements Principle

Page Connections

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