5.2 LangChain Basics
AI-generated content may contain errors. Always verify against official sources.
5.2 LangChain Basics
Key Concepts: Chains · Prompts · Output parsers · LCEL
Official Docs: LangChain LCEL · LangChain Prompt Templates
What is LangChain?
LangChain is an open-source framework for building LLM applications. Its core abstraction is LCEL (LangChain Expression Language) — a declarative way to compose chains using the | (pipe) operator.
pip install langchain langchain-openai
Core Building Blocks
| Block | Class | Role |
|---|---|---|
| Prompt template | ChatPromptTemplate | Formats variables into a prompt |
| LLM | ChatOpenAI, ChatAnthropic | Calls the model |
| Output parser | StrOutputParser, JsonOutputParser | Parses the raw response |
| Retriever | VectorStoreRetriever | Fetches relevant documents |
Your First LCEL Chain
The | operator pipes the output of one component as input to the next:
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
# 1. Prompt template
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant. Be concise."),
("human", "{question}"),
])
# 2. Model
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
# 3. Output parser
parser = StrOutputParser()
# 4. Chain: prompt | model | parser
chain = prompt | llm | parser
# 5. Invoke
result = chain.invoke({"question": "What is LCEL?"})
print(result)
Prompt Templates
# Multiple input variables
prompt = ChatPromptTemplate.from_template(
"Translate the following text from {source_lang} to {target_lang}:\n\n{text}"
)
# Format and inspect before sending
formatted = prompt.format_messages(
source_lang="English",
target_lang="Arabic",
text="Hello, welcome to our service."
)
print(formatted)
Output Parsers
String Output
from langchain_core.output_parsers import StrOutputParser
chain = prompt | llm | StrOutputParser()
result = chain.invoke({"question": "What is a neural network?"})
print(type(result)) # str
JSON Output
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.prompts import PromptTemplate
parser = JsonOutputParser()
prompt = PromptTemplate(
template="Return JSON with keys 'capital' and 'population' for {country}. {format_instructions}",
input_variables=["country"],
partial_variables={"format_instructions": parser.get_format_instructions()},
)
chain = prompt | llm | parser
result = chain.invoke({"country": "France"})
print(result["capital"]) # Paris
Streaming with LCEL
for chunk in chain.stream({"question": "Tell me about transformers."}):
print(chunk, end="", flush=True)
Batch Processing
questions = [
{"question": "What is Python?"},
{"question": "What is JavaScript?"},
{"question": "What is Rust?"},
]
results = chain.batch(questions) # runs concurrently
for q, r in zip(questions, results):
print(f"Q: {q['question']}\nA: {r[:80]}\n")
Common Mistakes
- Importing from deprecated modules — LangChain restructured into
langchain-core,langchain, andlangchain-community. Uselangchain_core.prompts, notlangchain.prompts. - Not specifying template variables —
ChatPromptTemplate.from_templateuses{var}syntax. Forgetting to pass a variable raises aKeyErrorat invoke time. - Forgetting
StrOutputParser— without a parser, the chain returns anAIMessageobject, not a plain string. - Using
chain.run()(deprecated) — the modern method ischain.invoke(),chain.stream(), orchain.batch().
Quick Quiz
Q1. What does the | operator do in LCEL?
A1. It composes Runnables — the output of the left Runnable becomes the input of the right Runnable (pipe operator).
Q2. What does StrOutputParser do?
A2. Extracts the plain string content from the AIMessage returned by the LLM, so you get a str instead of an object.
Q3. What method runs the same chain on multiple inputs concurrently?
A3. chain.batch(list_of_inputs).
Student Exercise
Exercise 5.2 — Translation chain
Build an LCEL chain that: (1) takes a text and target_language as inputs, (2) translates the text using gpt-4o-mini, (3) returns a JSON object with keys original, translated, and language. Use JsonOutputParser and validate the output with Pydantic.
Further Reading
Next → 5.3 Sequential Chains