LangChain breaks large model invocation into three layers: prompt construction, model execution, and result parsing. This design solves common problems such as messy multi-model integration, poor prompt reuse, and unstable outputs. This article focuses on three core capabilities—Prompt Template, Chat Model, and Output Parser—and includes practical, production-ready code examples. Keywords: LangChain, Prompt Template, Output Parser.
Technical Specification Snapshot
| Parameter | Description |
|---|---|
| Core Topic | LangChain Model I/O |
| Language | Python |
| Supported Protocols | OpenAI-Compatible API, Hugging Face API |
| Typical Models | qwen-plus, qwen-turbo, deepseek-v3, text-embedding-v3 |
| Core Dependencies | langchain, langchain-openai, langchain-core, langchain-huggingface, python-dotenv |
| Source Format | Reworked from technical blog notes |
| Tag Focus | LLM, Prompt Engineering, RAG |
LangChain Model I/O provides a composable abstraction for LLM calls.
LangChain splits model usage into three parts: input prompting, model invocation, and output parsing. The value of this split lies in decoupling how you ask, which model you call, and how you consume the result. That makes workflows easier to reuse, test, and extend.
AI Visual Insight: This image shows the three-stage structure of LangChain Model I/O, moving from Prompt input to Model execution and finally to Parser-based output processing. It highlights a pluggable invocation chain with clear module responsibilities, which is ideal for building standardized LLM applications.
This pattern is especially useful in multi-model environments. You can keep the prompt structure fixed while switching models, or keep the model unchanged and replace only the parser to constrain free-form text into JSON, CSV, or XML.
AI Visual Insight: This image further illustrates where Prompt, Model, and Parser sit within the broader LangChain architecture. It emphasizes their role as the application entry layer, typically placed before chain orchestration and data processing, making them foundational building blocks for RAG and Agent systems.
A minimal invocation chain quickly illustrates the responsibility of each layer.
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
import os
# Initialize the model. This example connects to Qwen through an OpenAI-compatible protocol.
model = ChatOpenAI(
api_key=os.getenv("api_key"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
model="qwen-plus"
)
# Define a prompt template: inject dynamic variables into a fixed instruction.
prompt = PromptTemplate.from_template(
"You are a professional programmer. Briefly describe {text}"
)
# Generate the final input text.
final_input = prompt.format(text="LangChain")
# Invoke the model and get the result.
result = model.invoke(final_input)
print(result.content)
This code demonstrates that Prompt is responsible for assembling the input, Model is responsible for inference, and the final output remains text that can still be processed further.
Prompt Template turns prompts from hardcoded strings into reusable assets.
A PromptTemplate is essentially a template with variable placeholders. It does not solve whether you can send a request. It solves how to repeatedly construct high-quality inputs in a stable and efficient way. This becomes highly valuable in batch processing, automated generation, and multi-scenario workflows.
Common LangChain templates include string templates, chat templates, few-shot templates, partial formatting templates, pipeline templates, and custom templates. Among them, string templates and chat templates are the most commonly used.
Chat templates fit modern conversational models.
Chat models naturally distinguish roles such as system, human, and ai, so ChatPromptTemplate is better than plain strings for describing task boundaries, user input, and behavioral constraints.
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain.prompts.chat import ChatPromptTemplate
load_dotenv()
chat_prompt = ChatPromptTemplate.from_messages([
("system", "You are a translation expert who specializes in translating {input_language} into {output_language}"),
("human", "{text}")
])
model = ChatOpenAI(
api_key=os.getenv("QW_KEY"),
base_url=os.getenv("QW_URL"),
model="qwen-turbo"
)
# Fill variables and generate a structured message list.
messages = chat_prompt.format_messages(
input_language="Chinese",
output_language="English",
text="I love you, China!"
)
result = model.invoke(messages)
print(result.content)
This example shows how chat templates explicitly separate role definition from user input, improving prompt stability and maintainability.
Few-shot templates turn examples into reasoning constraints.
FewShotPromptTemplate teaches the model to answer in a specified format and style through examples. It is especially useful for classification, extraction, format normalization, and rule-based question answering.
from langchain_core.prompts import PromptTemplate, FewShotPromptTemplate
examples = [
{"input": "2+2", "output": "4", "description": "addition"},
{"input": "5-2", "output": "3", "description": "subtraction"},
]
example_prompt = PromptTemplate.from_template(
"Expression: {input}, Result: {output}, Type: {description}"
)
prompt = FewShotPromptTemplate(
examples=examples,
example_prompt=example_prompt,
prefix="You are a math expert. Use the following examples as reference:",
suffix="Please calculate: {input}",
input_variables=["input"]
)
# Generate the final few-shot prompt.
print(prompt.format(input="2*5"))
The key idea in this example is to place sample cases before the actual task so the model is less likely to drift from the intended output pattern.
LangChain unifies the invocation style of LLMs, Chat Models, and Embedding Models.
An LLM takes text input and returns text output. A Chat Model takes a list of messages and returns a message object. An Embedding Model maps text into vectors. Together, these three model types provide the foundation for most AI applications.
In real-world systems, Chat Models support interactive generation, Embedding Models support retrieval and similarity computation, and traditional LLM interfaces remain useful for simple completion tasks or backward compatibility with older systems.
Embedding models are foundational components in RAG retrieval pipelines.
import os
from dotenv import load_dotenv
from langchain_community.embeddings import DashScopeEmbeddings
load_dotenv()
embeddings = DashScopeEmbeddings(
dashscope_api_key=os.getenv("api_key"),
model="text-embedding-v3"
)
# Vectorize documents for building a vector index.
doc_vectors = embeddings.embed_documents(["large language model", "LangChain"])
# Vectorize a query for similarity retrieval.
query_vector = embeddings.embed_query("What is LangChain?")
print(len(doc_vectors), len(query_vector))
This code shows how the same embedding model can serve both document ingestion and query retrieval stages.
Output parsers make LLM results consumable in engineering workflows.
If you only keep raw natural language output, downstream systems often cannot consume it reliably. The value of an Output Parser is that it converts model output into structured formats such as lists, dictionaries, or XML, which reduces the cost of string cleanup and post-processing.
Common LangChain parsers include CommaSeparatedListOutputParser, JsonOutputParser, and XMLOutputParser. For business applications, JSON parsing is the most common choice because it works well for API transmission, database storage, and frontend consumption.
JsonOutputParser can return structured results directly.
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import JsonOutputParser
load_dotenv()
llm = ChatOpenAI(
api_key=os.getenv("QW_KEY"),
base_url=os.getenv("QW_URL"),
model="qwen-turbo"
)
prompt = ChatPromptTemplate.from_messages([
("system", "You are a professional programmer. Please return a structured result."),
("user", "{input}")
])
parser = JsonOutputParser()
# LCEL pipeline: prompt -> model -> JSON parsing.
chain = prompt | llm | parser
result = chain.invoke({"input": "Explain what LangChain is in JSON"})
print(result)
The value of this example is that model output goes directly into a Python dictionary, making downstream business logic much easier to implement.
The right Model I/O combination depends on your task goal.
If your goal is highly reusable text generation, start with PromptTemplate + ChatModel. If your goal is retrieval-augmented generation, add Embedding. If your goal is system integration, you should add an Output Parser to enforce structured output.
For enterprise applications, a common combination is this: ChatPromptTemplate constructs role-based input, ChatOpenAI performs inference, and JsonOutputParser returns a stable structure that can then connect to an API or workflow system.
FAQ Structured Q&A
How should you choose between PromptTemplate and ChatPromptTemplate in LangChain?
If the model interface accepts plain text, use PromptTemplate first. If the interface accepts structured messages such as system and human roles, use ChatPromptTemplate first. The latter is a better fit for modern conversational models.
Can Output Parser fully guarantee valid JSON output from the model?
No. It cannot guarantee valid JSON in every case, but it can significantly improve the success rate of structured output. In production, you should still add retries, validation, and exception fallback logic.
Can Embedding Models and Chat Models replace each other?
No. Embedding Models are responsible for vector representation and retrieval, while Chat Models are responsible for generation and conversation. In RAG scenarios, they usually work together rather than replacing one another.
[AI Readability Summary]
This article systematically reconstructs the core capabilities of LangChain Model I/O. It explains Prompt Template, LLM/Chat/Embedding models, and Output Parser, and uses examples with Qwen, DashScope, and related integrations to show how to quickly build structured and scalable LLM invocation pipelines.