Skip to main content

🎬 langchain-youtube

PyPI version CI Python 3.9+ License: MIT

LangChain retrievers for YouTube — search videos, fetch playlists, extract transcripts, retrieve video metadata, and fetch comments as LangChain Document objects.

Perfect for building RAG applications, video summarizers, and AI-powered YouTube tools.

✨ Features

Retriever What it does API Key Required?
YouTubeSearchRetriever Search videos by keyword ✅ Yes
YouTubePlaylistRetriever Fetch all videos from a playlist ✅ Yes
YouTubeTranscriptRetriever Extract video transcripts/captions ❌ No!
YouTubeVideoRetriever Get detailed video metadata ✅ Yes
YouTubeCommentsRetriever Fetch video comments with engagement data ✅ Yes

📦 Installation

pip install langchain-youtube

🔑 Setup (for API key features)

  1. Go to the Google Cloud Console
  2. Create a project and enable the YouTube Data API v3
  3. Create an API key under Credentials

Note: The YouTubeTranscriptRetriever does NOT need an API key!

🚀 Quick Start

Search YouTube Videos

from langchain_youtube import YouTubeSearchRetriever

retriever = YouTubeSearchRetriever(
    api_key="YOUR_API_KEY",
    max_results=5,
    order="relevance",  # or "date", "viewCount", "rating"
)

docs = retriever.invoke("LangChain RAG tutorial")

for doc in docs:
    print(f"📹 {doc.metadata['url']}")
    print(f"   {doc.page_content[:100]}...")
    print(f"   👁 {doc.metadata['view_count']:,} views")

Extract Video Transcripts (No API Key!)

from langchain_youtube import YouTubeTranscriptRetriever

retriever = YouTubeTranscriptRetriever(
    languages=["en", "es"],  # Preferred languages
    chunk_size=3000,          # Split long transcripts for LLM context limits
)

docs = retriever.invoke("https://www.youtube.com/watch?v=VIDEO_ID")

for doc in docs:
    print(f"📝 Chunk {doc.metadata['chunk_index'] + 1}/{doc.metadata['total_chunks']}")
    print(doc.page_content[:200])

Fetch Playlist Videos

from langchain_youtube import YouTubePlaylistRetriever

retriever = YouTubePlaylistRetriever(
    api_key="YOUR_API_KEY",
    playlist_url="https://www.youtube.com/playlist?list=PLxxxxxx",
    max_results=100,
)

docs = retriever.invoke("fetch")  # Query is unused for playlists

for doc in docs:
    print(f"#{doc.metadata['position']}{doc.page_content[:80]}")

Get Video Metadata

from langchain_youtube import YouTubeVideoRetriever

retriever = YouTubeVideoRetriever(api_key="YOUR_API_KEY")

docs = retriever.invoke("https://www.youtube.com/watch?v=dQw4w9WgXcQ")

video = docs[0]
print(f"Title: {video.page_content}")
print(f"Views: {video.metadata['view_count']:,}")
print(f"Likes: {video.metadata['like_count']:,}")
print(f"Duration: {video.metadata['duration']}")

Fetch Video Comments 🆕

from langchain_youtube import YouTubeCommentsRetriever

retriever = YouTubeCommentsRetriever(
    api_key="YOUR_API_KEY",
    max_results=50,       # Fetch top 50 comments
    order="relevance",    # or "time" for newest first
)

docs = retriever.invoke("https://www.youtube.com/watch?v=VIDEO_ID")

for doc in docs:
    print(f"💬 {doc.metadata['author']}: {doc.page_content[:100]}")
    print(f"   👍 {doc.metadata['like_count']} likes | 💬 {doc.metadata['reply_count']} replies")

🔗 Use with LangChain Chains

from langchain_youtube import YouTubeTranscriptRetriever
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

# Set up retriever and LLM
retriever = YouTubeTranscriptRetriever(languages=["en"])
llm = ChatOpenAI(model="gpt-4o-mini")

prompt = ChatPromptTemplate.from_template(
    "Summarize this YouTube video transcript in 5 bullet points:\n\n{context}"
)

# Build a simple chain
chain = (
    {"context": retriever}
    | prompt
    | llm
    | StrOutputParser()
)

summary = chain.invoke("https://www.youtube.com/watch?v=VIDEO_ID")
print(summary)

🧪 Development

# Clone the repo
git clone https://github.com/urraf/langchain-youtube.git
cd langchain-youtube

# Install in dev mode
pip install -e ".[dev]"

# Run unit tests (no API key needed)
pytest tests/unit_tests/ -v

# Run integration tests (requires API key)
YOUTUBE_API_KEY=your_key pytest tests/integration_tests/ -v

# Lint
ruff check src/ tests/

📄 License

MIT — see LICENSE for details.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_youtube-0.2.1.tar.gz (15.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_youtube-0.2.1-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file langchain_youtube-0.2.1.tar.gz.

File metadata

  • Download URL: langchain_youtube-0.2.1.tar.gz
  • Upload date:
  • Size: 15.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.3

File hashes

Hashes for langchain_youtube-0.2.1.tar.gz
Algorithm Hash digest
SHA256 a89403f43c1fb249e91322bb6e21fad26845016ba22f2fa2ea1a06c0712397f7
MD5 6ad6c9bd529214dcf4a0ad1aed7e3933
BLAKE2b-256 8c5d55ce6ba64aac1db1b2d8d0cf2b57c676f98d88328cb6c6236553ebb36d56

See more details on using hashes here.

File details

Details for the file langchain_youtube-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_youtube-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1953e337764d16c2ff94b54126b3191b8eb6176e5508492d8edde7902da259ad
MD5 e0c0b382b8d99920f762614bc0e591e6
BLAKE2b-256 7a960285165def2b7ab5582f1f62404e50948d43157d384974d07f0857b609c1

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 files

0.2.0

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page