Principle:Langchain ai Langchain Input Preparation
| 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)