🔗 Introduction to LangChain - Python
Introduction
Welcome to LangChain Academy's Introduction to LangChain course!
This repository is the companion to the course located HERE.
🚀 Setup
Prerequisites
- The Chrome browser is recommended
- git is recommended
- A package/project manager: uv (recommended) or pip
- note:
uvis also required in Module 2, Lesson 1 to run the MCP server withuvx - The course requires Python >=3.12, <3.14 If you use
uv, it will take care of this for you. More info
Installation
Download the course repository
# Clone the repo
git clone --depth 1 https://github.com/langchain-ai/lca-lc-foundations.git
cd lca-lc-foundationsMake a copy of example.env
# Create .env file
cp example.env .envChoose one chat setup in .env or your shell. Direct provider access is the default:
OPENAI_API_KEY=your_openai_api_key_here
TAVILY_API_KEY=your_tavily_api_key_hereFor LangSmith LLM Gateway, your workspace administrator must configure the provider secrets and grant access to the models used in the lessons. You do not need local OpenAI, Anthropic, or Google keys for supported chat models:
LANGSMITH_GATEWAY=true
LANGSMITH_API_KEY=your_langsmith_api_key_here
TAVILY_API_KEY=your_tavily_api_key_hereIf you use different credentials for gateway requests and tracing, set LANGSMITH_GATEWAY_API_KEY for chat. It takes precedence over LANGSMITH_API_KEY for gateway requests; tracing still uses LANGSMITH_API_KEY. Gateway access does not require tracing to be enabled. Keep the agent tracing settings separate.
For another region, set LANGSMITH_GATEWAY to your gateway base URL, such as https://eu.gateway.smith.langchain.com, https://apac.gateway.smith.langchain.com, or https://aws.gateway.smith.langchain.com. Do not append a provider path: the integrations add /openai/v1, /anthropic, or /gemini. LANGSMITH_ENDPOINT configures tracing, not gateway routing. Remove explicit provider base URL overrides if you want automatic gateway routing.
Unset LANGSMITH_GATEWAY, or set it to false, 0, no, or an empty value, to use direct provider access. Export shell variables when using a shell instead of .env. The notebooks use load_dotenv() without overriding exported variables; restart the notebook kernel after changing settings.
The optional Anthropic and Gemini examples in Module 1 need their corresponding provider key in direct mode, or provider access configured in the gateway workspace in gateway mode. The Gemini example uses the Gemini Developer API, not Vertex AI. Web search still requires TAVILY_API_KEY in both modes.
Bonus RAG only: notebooks/module-2/bonus_rag.ipynb uses OpenAIEmbeddings, which does not use LANGSMITH_GATEWAY. That lesson still needs a direct OPENAI_API_KEY; gateway-only chat setup is sufficient for the other chat examples.
After installing the dependencies below, check chat routing without local provider keys by leaving OPENAI_API_KEY, ANTHROPIC_API_KEY, GOOGLE_API_KEY, and GEMINI_API_KEY unset and running:
from dotenv import load_dotenv
from langchain.chat_models import init_chat_model
load_dotenv()
model = init_chat_model("openai:gpt-5-nano")
print(model.invoke("Reply with: gateway chat works").content)A reply confirms an authenticated model call. env_utils.py checks configuration presence, not whether a key is valid or a model is authorized. If the request fails with an authentication or access error, verify the gateway region, workspace API key, provider secret, and model permissions. Use uv sync or pip install -r requirements.txt to install the gateway-compatible integration versions declared by this repository.
Make a virtual environment and install dependencies. More info
Using uv (recommended)
uv syncUsing pip
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
pip install -r requirements.txtSetup Verification
After completing the Setup section, we recommend you run the following command to verify your environment.
Using uv
uv run python env_utils.pyUsing pip
source .venv/bin/activate # On Windows: .venv\Scripts\activate
python env_utils.pyIf the script flags issues, see this section below.
Run Notebooks More Info
Using uv (recommended)
uv run jupyter labUsing pip
source .venv/bin/activate # On Windows: .venv\Scripts\activate
jupyter labRun Studio (optional)
Ensure you are in the notebooks/module-1 or notebooks/module-3 directory
Using uv (recommended)
uv run langgraph devUsing pip
source .venv/bin/activate # On Windows: .venv\Scripts\activate
langgraph dev📚 Lessons
This repository contains three Modules that serve as introductions to many of LangChain's most-used features.
Module 1: Create Agent
- Foundational models
- Tools
- Short-Term Memory
- Multimodal Messages
- Project: Personal Chef
Module 2: Advanced Agent
- Model Context Protocol (MCP)
- Context and State
- Multi-Agent Systems
- Project: Wedding Planner
Module 3: Production-Ready Agent
- What is Middleware?
- Managing Long Conversations
- Human In The Loop (HITL)
- Dynamic Agents
- Project: Email Assistant
- Bonus: Agent Chat UI
📖 Related Resources
Setup Verification Issues
What the verification procedure checks:
- ✅ Python executable location and version (must be >=3.12, <3.14)
- ✅ Virtual environment is properly activated
- ✅ Required packages are installed with correct versions
- ✅ Packages are in the correct Python version's site-packages
- ✅ Environment variables (API keys) are properly configured
Configuration Issues and Solutions:
ImportError when running env_utils.py
If you see an error like ModuleNotFoundError: No module named 'dotenv', you're likely running Python outside the virtual environment.
Solution:
- Use
uv run python env_utils.py(recommended), or - Activate the virtual environment first:
- macOS/Linux:
source .venv/bin/activate - Windows:
.venv\Scripts\activate
- macOS/Linux:
Environment Variable Conflicts
If you see a warning about "ENVIRONMENT VARIABLE CONFLICTS DETECTED", you have API keys set in your system environment that differ from your .env file. Since load_dotenv() doesn't override existing variables by default, your system values will be used.
Solutions:
- Do nothing and accept the system environment variable value
- Unset the conflicting system environment variables for this shell session (commands provided in warning)
- Use
load_dotenv(override=True)in your notebooks to force .env values to take precedence - Update your .env file or shell init so the values are in agreement
LangSmith Tracing Errors
If you see "LANGSMITH_TRACING is enabled but LANGSMITH_API_KEY still has the example/placeholder value", you need to either:
- Set a valid LangSmith API key in your .env file, or
- Comment out or set
LANGSMITH_TRACING=falsein your .env file
Note: LangSmith is optional for evaluation and tracing. The course works without it.
Wrong Python Version
If you see a warning about Python version not satisfying requirements, you need Python >=3.12 and <3.14.
Solution:
- If using
uv: Runuv syncwhich will automatically install the correct Python version - If using pip: Install Python 3.12 or 3.13 using pyenv or from python.org
Python Virtual Environments
Managing your Python version is often best done with virtual environments. This allows you to select a Python version for the course independent of the system Python version.
Using uv (recommended)
uv will install a version of Python compatible with the versions specified in the pyproject.toml in the .venv directory when running the uv sync specified above. It will use this version when invoking with uv run. For additional information, please see uv.
Using pyenv + pip
If you are using pip instead of uv, you may prefer using pyenv to manage your Python versions. For additional information, please see pyenv.
pyenv install 3.12
pyenv local 3.12
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtModel Providers
If you don't have an OpenAI API key, you can sign up here. The course primarily uses gpt-5-nano which is very inexpensive. If desired, you may also obtain additional API keys for Anthropic or Google.
This course has been created using particular models and model providers. You can use other providers, but you will need to update the API keys in the .env file and make some necessary code changes. LangChain supports many chat model providers. More Info.
For OpenAI models that require the Responses API, such as gpt-5.6-luna, initialize the model explicitly and pass it to create_agent:
from langchain.chat_models import init_chat_model
from langchain.agents import create_agent
model = init_chat_model(
"gpt-5.6-luna",
model_provider="openai",
use_responses_api=True,
)
agent = create_agent(model=model)The introductory OpenAI model examples also select the Responses API explicitly. The course keeps gpt-5-nano as its default; model availability depends on your OpenAI account. The gpt-audio example uses Chat Completions instead.
Tavily is a search provider that returns search results in an LLM-friendly way. They have a generous free tier. Tavily
Getting Started with LangSmith
- Create a LangSmith account
- Create a LangSmith API key
- Create an agent in your LangSmith workspace and copy its agent ID into
LANGSMITH_AGENT_IDin.env;lca-lc-foundationsin the templates is an example ID, not a workspace or project name - Set
LANGSMITH_AGENT_ENVIRONMENT=localfor notebook runs; the supported environments arelocal,development,staging, andproduction - Update
.envwith your LangSmith API key and uncommentLANGSMITH_TRACING=trueonly after configuring the agent and environment - Remove legacy
LANGSMITH_PROJECTandLANGCHAIN_PROJECTsettings from.envand your shell; setting a project alongside an agent address prevents implicitly routed traces from being recorded
The agent ID is a lowercase slug, not a UUID. Use the ID shown in LangSmith rather than the agent's display name. Set LANGSMITH_ENDPOINT to your account's region if needed, using the EU example above for EU accounts. After changing .env, restart the notebook kernel so the new settings take effect.
For more information on LangSmith, see our docs here.
Note: If you enable LangSmith tracing by setting LANGSMITH_TRACING=true in your .env file, make sure you have a valid LANGSMITH_API_KEY set. The environment verification script (env_utils.py) will warn you if tracing is enabled without a valid key.
Environment Variables
This course uses the dotenv module to read key-value pairs from the .env file and set them in the environment in the Jupyter notebooks. They do not need to be set globally in your system environment.
Note: If you have API keys already set in your system environment, they may conflict with the ones in your .env file. The env_utils.py verification script will detect and warn you about such conflicts. By default, load_dotenv() does not override existing environment variables.
Development Environment
The course uses Jupyter notebooks. The Jupyter package is installed in the virtual environment and can be run as described above. Jupyter notebooks can also be edited and run in VSCode or other VSCode variants such as Windsurf or Cursor.