LangChain Hands-on · Module 4 · Lab 16

LangChain Classic: Reading Old Code

⏱ 20 min + hands-on🎯 Beginner🖼 1 diagram🧪 2 live demos🐍 Python code

Most YouTube videos and blogs were written for LangChain 0.x: LLMChain, ConversationChain, RetrievalQA, initialize_agent. In v1 these moved to the package langchain-classic. They still run (with deprecation warnings), so you can read and convert old code. For new projects, use the v1 way you learned in Labs 01–15.

In this session

  • Why langchain-classic exists
  • LLMChain → prompt | llm | parser
  • SimpleSequentialChain and SequentialChain
  • ConversationChain with buffer, window and summary memory
  • RetrievalQA and create_retrieval_chain
  • Old → new conversion table

01The big idea: old notes, new syllabus

📒

Think of your senior’s old notes. The concepts are still right, but the syllabus changed: chapter names moved and some methods are no longer taught. You can still learn from them if you know the mapping. This lab is that mapping, from LangChain 0.x to v1.

Classic (0.x)• from langchain.chains import LLMChain• ConversationChain + memory classes• RetrievalQA.from_chain_type• initialize_agent / AgentExecutor• .run(), .predict()v1• prompt | llm | StrOutputParser()• checkpointer + thread_id• LCEL RAG chain (Lab 11)• create_agent (Lab 13)• .invoke()
Same ideas, new building blocks.
LangChainDeprecationWarning: The class `LLMChain` was deprecated in LangChain 0.1.17 and will be removed in 2.0.0. Use `RunnableSequence, e.g., `prompt | llm`` instead.

The real warning printed by the hands-on run below. Classic classes are scheduled for removal in 2.0.

02Old → new cheat sheet

Old (classic)New (v1)
LLMChain(llm, prompt)prompt | llm | StrOutputParser()
SimpleSequentialChain / SequentialChain{"x": chain1} | chain2, RunnablePassthrough.assign
ConversationChain + ConversationBufferMemoryRunnableWithMessageHistory or agent + checkpointer
ConversationBufferWindowMemorytrim_messages
ConversationSummaryMemorySummarizationMiddleware
RetrievalQALCEL RAG chain (Lab 11)
initialize_agent, AgentExecutorcreate_agent
.run(), .predict().invoke()
from langchain.chains import …from langchain_classic.chains import …
Demo · Window memory with k=1: what does the bot remember?

03Hands-on: run it here

Old and new side by side: LLMChain returns a dict with text, LCEL returns the string directly. Window memory with k=1 forgets the name, just like the notebook says.

lab16_classic.py
import warnings; warnings.filterwarnings("ignore")
from standins import RuleModel, KeywordEmbeddings
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_classic.chains import LLMChain, SimpleSequentialChain, ConversationChain, RetrievalQA
from langchain_classic.memory import ConversationBufferMemory, ConversationBufferWindowMemory
from langchain_community.vectorstores import FAISS

llm = RuleModel(rules={"organic tea": "Chai Bloom", "company name for electric": "VoltRide",
                       "slogan": "VoltRide: Silent rides, bright future.", "upi limit": "Rs 25,000 in the first 24 hours."}, default="OK")
prompt = PromptTemplate.from_template("Suggest one name for a {business} company")
print("LLMChain (old):", LLMChain(llm=llm, prompt=prompt).invoke({"business": "organic tea"}))
print("LCEL     (new):", (prompt | llm | StrOutputParser()).invoke({"business": "organic tea"}))

name_chain = LLMChain(llm=llm, prompt=PromptTemplate.from_template("Give one company name for electric {product}. Only the name."))
slogan_chain = LLMChain(llm=llm, prompt=PromptTemplate.from_template("Write a slogan for the company {name}."))
print("SimpleSequentialChain:", SimpleSequentialChain(chains=[name_chain, slogan_chain]).invoke("scooters"))

class Rememberer(RuleModel):
    def _pick(self, messages):
        t = messages[-1].content
        hist = t.split("Current conversation:")[-1].rsplit("Human:", 1)[0]
        q = t.rsplit("Human:", 1)[-1].lower()
        if "name" in q: return "Your name is Ajeet." if "Ajeet" in hist else "I'm sorry, I don't know your name."
        return "Nice to meet you!"
for mem in [ConversationBufferMemory(), ConversationBufferWindowMemory(k=1)]:
    conv = ConversationChain(llm=Rememberer(), memory=mem)
    conv.invoke({"input": "My name is Ajeet."}); conv.invoke({"input": "I like cricket."})
    print(f"{type(mem).__name__:32s} -> {conv.invoke({'input': 'What is my name?'})['response']}")

retriever = FAISS.from_texts(["Report a lost card within 3 days for zero liability.", "UPI limit for a new payee is Rs 25,000 in the first 24 hours.",
                              "Video KYC is available from 9 AM to 9 PM."], KeywordEmbeddings()).as_retriever(search_kwargs={"k": 1})
out = RetrievalQA.from_chain_type(llm=llm, retriever=retriever, chain_type="stuff", return_source_documents=True).invoke({"query": "What is the UPI limit for a new payee?"})
print("RetrievalQA:", out["result"], "| source:", out["source_documents"][0].page_content)
LLMChain (old): {'business': 'organic tea', 'text': 'Chai Bloom'} LCEL (new): Chai Bloom SimpleSequentialChain: {'input': 'scooters', 'output': 'VoltRide: Silent rides, bright future.'} ConversationBufferMemory -> Your name is Ajeet. ConversationBufferWindowMemory -> I'm sorry, I don't know your name. RetrievalQA: Rs 25,000 in the first 24 hours. | source: UPI limit for a new payee is Rs 25,000 in the first 24 hours.
Runs here without any API key. A small stand-in plays the LLM (RuleModel or ScriptedModel from standins.py), so every output below is from a real run of this code. In Colab you swap one line: llm = ChatGroq(model="openai/gpt-oss-20b").

04Hands-on: Ajeetkumar’s Colab notebook

Colab setup (same in every notebook): click 🔑 Secrets in the Colab sidebar, add groq_key with your free key from console.groq.com, switch on Notebook access, then run the cells from the top.
Demo · Notebook player · Lab 16

05Summary

06Quick quiz

1. Where do LLMChain and RetrievalQA live in v1?

2. v1 replacement for LLMChain(llm, prompt)?

3. ConversationBufferWindowMemory(k=1) keeps…

4. v1 replacement for initialize_agent?

5. .run() and .predict() became…

07References

Created by Ajeetkumar