Module 1 cheat sheet: LangChain Basics

Talk to any LLM with one interface, build prompts from templates, turn text into data, join steps with | and give the chat a memory.

All modules📖 Glossary

Key ideas

  • Chat model: messages in, AIMessage out. invoke · stream · batch · ainvoke. Lab 01 →
  • Templates have {blanks}; missing variable → KeyError. Lab 02 →
  • Parsers turn text into data; with_structured_output is the best default. Lab 03 →
  • LCEL: prompt | llm | StrOutputParser(). Lab 04 →
  • LLMs are stateless: memory = resend old messages per session_id. Lab 05 →

Colab + Groq setup

from google.colab import userdata
import os
os.environ['GROQ_API_KEY'] = userdata.get('groq_key')
from langchain_groq import ChatGroq
llm = ChatGroq(model="openai/gpt-oss-20b")

Messages

ClassRole
SystemMessagerules
HumanMessagequestion
AIMessageanswer / few-shot example
ToolMessagetool result

Runnables

RunnableDoes
a | boutput of a → b
{"x": chain}build a dict (parallel)
RunnablePassthrough.assignkeep input, add key
RunnableParallelbranches at once
RunnableLambdaany Python function
RunnableBranchif / else routing
.with_fallbacks / .with_retryproduction safety

Parsers

ParserGives
StrOutputParserstr
CommaSeparatedListlist
JsonOutputParserdict
PydanticOutputParservalidated object
with_structured_outputobject via tool calling

Common mistakes

  • Using retired Groq Llama 3 models: use openai/gpt-oss-20b.
  • User text inside the system message.
  • Unescaped { } in JSON examples inside templates.
  • Building an agent when a fixed chain is enough.
  • Unlimited chat history: trim it.

Created by Ajeetkumar · EasyLearnSpace