Skip to main content

5.2 LangChain Basics

AI-Generated Content

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

BlockClassRole
Prompt templateChatPromptTemplateFormats variables into a prompt
LLMChatOpenAI, ChatAnthropicCalls the model
Output parserStrOutputParser, JsonOutputParserParses the raw response
RetrieverVectorStoreRetrieverFetches 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

Common Mistakes
  1. Importing from deprecated modules — LangChain restructured into langchain-core, langchain, and langchain-community. Use langchain_core.prompts, not langchain.prompts.
  2. Not specifying template variablesChatPromptTemplate.from_template uses {var} syntax. Forgetting to pass a variable raises a KeyError at invoke time.
  3. Forgetting StrOutputParser — without a parser, the chain returns an AIMessage object, not a plain string.
  4. Using chain.run() (deprecated) — the modern method is chain.invoke(), chain.stream(), or chain.batch().

Quick Quiz

Test Your Understanding

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