LangChain Core โ The Building Blocks
Lesson 2: LangChain Core โ The Building Blocks
LangChain is the foundation layer: a collection of reusable components for talking to LLMs, plus the glue for composing them. If LangGraph is the brain's control flow, LangChain is the toolbox โ models, prompts, parsers, retrievers, and tools that you wire together.
What's actually in the box
The core value is standardized abstractions. Instead of learning each vendor's SDK quirks, you program against a common interface and swap the backend. The most important abstractions:
| Abstraction | What it does | Examples |
|---|---|---|
| Chat models | Unified interface for chat LLMs | ChatOpenAI, ChatAnthropic, ChatGoogle, local via ChatOllama |
| Prompt templates | Reusable, parameterized prompts | ChatPromptTemplate, PromptTemplate |
| Output parsers | Turn model text into structured data | StrOutputParser, Pydantic parsers, structured output |
| Retrievers & vector stores | Find relevant context for RAG | FAISS, Chroma, PGVector + Retriever wrapper |
| Document loaders & splitters | Ingest and chunk source material | PyPDFLoader, RecursiveCharacterTextSplitter |
| Embedding models | Turn text into vectors | OpenAIEmbeddings, HuggingFaceEmbeddings |
| Tools | Expose functions the model can call | @tool decorator, built-in search/calculator tools |
| Message history / memory | Remember prior turns | InMemoryChatMessageHistory, LangGraph checkpoints |
BaseChatModel; run the same code against OpenAI, Anthropic, or a local model by changing one line. The same pattern repeats across every abstraction.
A minimal example
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
model = ChatOpenAI(model="gpt-4o-mini")
prompt = ChatPromptTemplate.from_messages([
("system", "You are a terse technical writer."),
("user", "Explain {topic} in three sentences."),
])
chain = prompt | model | StrOutputParser()
result = chain.invoke({"topic": "retrieval augmented generation"})
print(result)
Notice the | operator: that's LCEL (LangChain Expression Language), the composition syntax we'll dissect in the next lesson. Each piece is a runnable โ prompt in, text out โ and they chain together.
Package landscape
LangChain is not one package; it's an ecosystem of pip-installable pieces, which keeps your dependency tree lean:
langchain-coreโ the abstractions, runnables, and interfaces (no vendor code)langchainโ the main framework: chains, agents, memory, utilitieslangchain-openai,langchain-anthropic,langchain-google-genai, โฆ โ per-vendor integrationslangchain-communityโ community-maintained integrations (broad, less guaranteed)langchain-text-splitters,langchain-docloadersโ ingestion utilitieslanggraphโ the orchestration engine (next lesson)
langchain.llms.OpenAI or long-deprecated chains, the API has moved โ modern code imports from langchain_openai / langchain_core and prefers LCEL composition. Check the docs' version notes rather than cargo-culting old snippets.
Where LangChain stops
LangChain gives you components and one-shot pipelines. The moment you need loops, branching, persistence, or human approval โ the hallmarks of a real agent โ a linear chain isn't enough. That's the gap LangGraph fills. LangChain's own guidance has shifted: build the pipeline with LangChain, orchestrate the agent with LangGraph, and don't force everything into a chain.
๐ง Knowledge Check
1. What is the main value of LangChain's abstractions like BaseChatModel?
2. Which package holds the core interfaces (BaseChatModel, prompts, runnables)?
3. When is LangChain + LCEL alone NOT enough?