Examples
End-to-end walkthroughs: chat, RAG, agents with tools, streaming, and structured output.
Third-party documentation. This is independently authored analysis of the public LangChain codebase — not the official docs, and not reviewed or endorsed by the LangChain team.
Examples
The examples in this module's manifest cover the four shapes that cover ~90% of real LangChain applications: a chat pipeline, retrieval-augmented generation, an agent with tool-calling, and structured (JSON) output. All use the same LCEL composition and the same constructor pattern for the model.
From example to production
Each example is deliberately minimal. The production version of each adds the same four things:
- Tracing — a LangSmith project so every prompt/call is inspectable.
- Error handling — retry parsers, fallback models, and timeouts around
invoke. - Persistence — a real vector store (pgvector, Pinecone, Weaviate) instead of an in-memory FAISS index for RAG.
- Concurrency —
batch/abatchwhere throughput matters.
The example code is what you'd run in a notebook first; the production hardening is mechanical on top of the same shapes.
Examples
A minimal chat pipeline
from langchain_core.prompts import ChatPromptTemplatefrom langchain_core.output_parsers import StrOutputParserfrom langchain_openai import ChatOpenAIprompt = ChatPromptTemplate.from_messages([("system", "You are a concise assistant."),("human", "{question}"),])model = ChatOpenAI(model="gpt-4o-mini")parser = StrOutputParser()chain = prompt | model | parserprint(chain.invoke({"question": "What is LCEL?"}))
Three runnables composed with |. invoke() runs them in sequence; stream()/ainvoke() work on the same object with no rewiring.
Retrieval-augmented generation
from langchain_community.document_loaders import TextLoaderfrom langchain_text_splitters import RecursiveCharacterTextSplitterfrom langchain_openai import OpenAIEmbeddingsfrom langchain_community.vectorstores import FAISSfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_openai import ChatOpenAIfrom langchain.chains.combine_documents import create_stuff_documents_chainfrom langchain.chains import create_retrieval_chain# 1. Load + splitloader = TextLoader("notes.txt")docs = loader.load()splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)chunks = splitter.split_documents(docs)# 2. Embed + indexvectorstore = FAISS.from_documents(chunks, OpenAIEmbeddings())retriever = vectorstore.as_retriever(search_kwargs={"k": 3})# 3. Prompt + generation chainprompt = ChatPromptTemplate.from_template("""Answer using only this context:\n{context}\n\nQuestion: {input}""")llm = ChatOpenAI(model="gpt-4o-mini")combine = create_stuff_documents_chain(llm, prompt)# 4. Retrieval chainrag = create_retrieval_chain(retriever, combine)result = rag.invoke({"input": "What is the refund policy?"})print(result["answer"])
The classic RAG flow: split → embed → index → retrieve → stuff-into-prompt → generate. create_retrieval_chain wires retrieval into the answer step.
An agent with tool-calling
from langchain_core.tools import toolfrom langchain_openai import ChatOpenAIfrom langchain.agents import create_tool_calling_agent, AgentExecutorfrom langchain_core.prompts import ChatPromptTemplate@tooldef get_weather(city: str) -> str:"""Get the current weather for a city."""# ... call a weather API ...return f"Sunny, 22C in {city}"tools = [get_weather]model = ChatOpenAI(model="gpt-4o-mini")prompt = ChatPromptTemplate.from_messages([("system", "You are a helpful assistant."),("human", "{input}"),("placeholder", "{agent_scratchpad}"),])agent = create_tool_calling_agent(model, tools, prompt)executor = AgentExecutor(agent=agent, tools=tools)print(executor.invoke({"input": "What039;s the weather in Lisbon?"}))
The agent loop: the model decides to call a tool, the tool result feeds back in, until the model returns a final answer.
Structured output (JSON) from the model
from langchain_core.prompts import ChatPromptTemplatefrom langchain_openai import ChatOpenAIfrom langchain_core.output_parsers import JsonOutputParserfrom pydantic import BaseModelclass Article(BaseModel):title: strsummary: strtags: list[str]parser = JsonOutputParser(pydantic_object=Article)prompt = ChatPromptTemplate.from_template("Summarize the article into JSON.\n{format_instructions}\n\n{input}")chain = prompt | ChatOpenAI(model="gpt-4o-mini") | parserresult = chain.invoke({"input": "LangChain is a framework for LLM apps.","format_instructions": parser.get_format_instructions(),})print(result) # -> {"title": ..., "summary": ..., "tags": [...]}
JsonOutputParser drives the model toward a validated Pydantic shape — no manual JSON parsing, and failures surface as parser errors.
Edge Cases
- Empty retrieval results produce a context-free answer rather than an error — check whether the retriever actually returned documents before trusting the answer.
- Tool-calling agents can loop if a tool keeps returning empty/ambiguous output — set max_iterations and add a stop condition to AgentExecutor.
- Structured output parsers fail loudly on malformed model output; wrap them with retry logic or fall back to plain-text generation for critical paths.
