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.

Principle:Langchain ai Langchain Input Preparation

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

Overview

A normalization step that converts heterogeneous user inputs into a unified message format suitable for chat model consumption.

Description

LangChain chat models accept multiple input formats for developer convenience: raw strings, lists of message objects (HumanMessage, SystemMessage, etc.), lists of tuples/dicts, and PromptValue objects from prompt templates. Input preparation normalizes all these formats into a single PromptValue containing a list of BaseMessage objects.

This normalization is critical because it decouples the user-facing API (which is flexible) from the provider-facing API (which requires structured messages). It ensures that downstream processing (caching, rate limiting, provider API calls) always receives a consistent data format.

Usage

This principle is applied automatically within the invoke(), stream(), and generate() methods. Developers do not call it directly but benefit from the input flexibility it enables.

Theoretical Basis

The normalization follows a type-dispatch pattern:

# Abstract algorithm (not real code)
if input is str:
    messages = [HumanMessage(content=input)]
elif input is list[BaseMessage]:
    messages = input
elif input is PromptValue:
    messages = input.to_messages()
else:
    messages = convert_to_messages(input)  # handles dicts, tuples
return PromptValue(messages)

Related Pages

Implemented By

Page Connections

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