Skip to content

anshulkumar1997/Introduction-to-Langchain-Python

Repository files navigation

🔗 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: uv is also required in Module 2, Lesson 1 to run the MCP server with uvx
  • 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-foundations

Make a copy of example.env

# Create .env file
cp example.env .env

Edit the .env file to include the keys below for Models and optionally LangSmith

  • Get an OpenAI API Key here.
  • Optional for Module1/Lesson1, get an Anthropic API Key here and a Google API Key here.
  • Optional, Create a LangSmith account and API Key.
# Manual installs for checking: uv

# Required
OPENAI_API_KEY='your_openai_api_key_here'
TAVILY_API_KEY='your_tavily_api_key_here'

# optional, only used in Module1, Lesson 1 once
ANTHROPIC_API_KEY='your_anthropic_api_key_here'
GOOGLE_API_KEY='your_google_api_key_here'

# Optional for evaluation and tracing
LANGSMITH_API_KEY='your_langsmith_api_key_here'
# uncomment to set tracing to true when you set up your LangSmith account
#LANGSMITH_TRACING=true
LANGSMITH_PROJECT=lca-lc-foundation
# Uncomment the following if you are on the EU instance:
#LANGSMITH_ENDPOINT=https://eu.api.smith.langchain.com

Make a virtual environment and install dependencies. More info

Using uv (recommended)
uv sync
Using pip
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
pip install -r requirements.txt

Setup Verification

After completing the Setup section, we recommend you run the following command to verify your environment.

Using uv
uv run python env_utils.py
Using pip
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
python env_utils.py

If the script flags issues, see this section below.

Run Notebooks More Info

Using uv (recommended)
uv run jupyter lab
Using pip
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
jupyter lab

Run Studio (optional)

Ensure you are in the notebooks/module-1 or notebooks/module-3 directory

Using uv (recommended)
uv run langgraph dev
Using 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
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:

  1. Do nothing and accept the system environment variable value
  2. Unset the conflicting system environment variables for this shell session (commands provided in warning)
  3. Use load_dotenv(override=True) in your notebooks to force .env values to take precedence
  4. 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:

  1. Set a valid LangSmith API key in your .env file, or
  2. Comment out or set LANGSMITH_TRACING=false in 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: Run uv sync which 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.txt

Model 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.

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
LangSmith Dashboard LangSmith API Keys
  • Update the .env file you created with your new LangSmith API Key.
  • Check that LANGSMITH_TRACING is uncommented and set to true.

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors