🎬 langchain-youtube
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)
- Go to the Google Cloud Console
- Create a project and enable the YouTube Data API v3
- Create an API key under Credentials
Note: The
YouTubeTranscriptRetrieverdoes 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.
Metadata
Release files for langchain-youtube 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| langchain_youtube-0.2.1.tar.gz | 15.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| langchain_youtube-0.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 29.7 kB
Release files / langchain_youtube-0.2.1.tar.gz
| Download URL | langchain_youtube-0.2.1.tar.gz |
|---|---|
| Size | 15.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a89403f43c1fb249e91322bb6e21fad26845016ba22f2fa2ea1a06c0712397f7
|
|
BLAKE2b-256 checksum How to use checksums |
8c5d55ce6ba64aac1db1b2d8d0cf2b57c676f98d88328cb6c6236553ebb36d56
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.3
|
Release files / langchain_youtube-0.2.1-py3-none-any.whl
| Download URL | langchain_youtube-0.2.1-py3-none-any.whl |
|---|---|
| Size | 13.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1953e337764d16c2ff94b54126b3191b8eb6176e5508492d8edde7902da259ad
|
|
BLAKE2b-256 checksum How to use checksums |
7a960285165def2b7ab5582f1f62404e50948d43157d384974d07f0857b609c1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.3
|