Sub topic 8
LangChain: components and integrations
This chapter assumes you can call a provider SDK, as in the earlier chapters, and that you know basic Python and virtual environments. You should also know what a Pydantic model is.
What LangChain is
LangChain is a Python library of small, swappable components for building LLM applications. The components include chat models, prompt templates, output parsers, retrievers, vector stores, and tools. Each component follows a shared interface, so you can replace one without rewriting the code around it.
The library is split into packages. langchain-core holds the interfaces and the composition syntax. Provider packages such as langchain-openai hold the model classes. Install only what you use:
pip install langchain-core langchain-openaiOne interface for chat models
The same call from the SDK chapter looks like this in LangChain:
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o-mini")
result = llm.invoke("Explain what an API client library is in one sentence.")
print(result.content)invoke takes a string or a list of messages and returns an AIMessage object, not a string. The text is in .content. Other providers have their own classes, such as ChatAnthropic, that expose the same invoke method. Switching providers is mostly a change of class and model name.
Composing components with the pipe operator
LangChain Expression Language (LCEL) chains components with |. The output of each step becomes the input of the next:
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
prompt = ChatPromptTemplate.from_messages([
("system", "You are a concise assistant."),
("user", "Explain {topic} in one sentence."),
])
llm = ChatOpenAI(model="gpt-4o-mini")
chain = prompt | llm | StrOutputParser()
print(chain.invoke({"topic": "an API client library"}))The prompt template fills in topic and produces messages. The model turns those into an AIMessage. The parser extracts the text. The chain itself is also a component, so it has invoke, plus stream and batch, without extra code.
Integrations
Much of LangChain's value is its catalog of integrations. These are ready-made classes for vector stores, document loaders, embedding models, and external tools. Many live in langchain-community or in separate partner packages. They save you from writing and maintaining each client yourself.
Integrations vary in quality. A partner package maintained with the provider is usually kept current. A community integration may lag behind the service it wraps. Check the package's recent release history before you depend on it.
Common errors
- Old import paths. Examples from older tutorials use
from langchain.chat_models import ChatOpenAI. Current code imports from the provider package, such aslangchain_openai. - Treating the result as a string. Calling a model directly returns a message. Add a parser or read
.content. - Unpinned versions. Interfaces change between releases, so pin versions in your requirements file.
- Not knowing what was sent. A chain hides the final prompt. Print the output of the prompt step alone with
prompt.invoke({...})when the model's answer looks wrong.
When it fits
LangChain suits projects that use many providers or integrations, or that need streaming and batching without custom code. For a single provider and one prompt, the direct SDK call is shorter and has fewer moving parts.
Exercise
A teammate's chain returns an AIMessage object when they expected plain text. The chain is prompt | llm. What do you add, and what does it change?
Show answer
Add StrOutputParser() as the last step, giving prompt | llm | StrOutputParser(). The parser takes the AIMessage and returns its .content as a plain string. Reading .content from the result also works.