You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Langchain YoutubeLoader加载视频时出现KeyError及PytubeError问题

解决Langchain YoutubeLoader调用Pytube获取视频标题时的KeyError问题

问题描述

使用Langchain的YoutubeLoader加载YouTube视频,当设置add_video_info=True时,会触发Pytube库的KeyError——访问vid_info['videoDetails']['title']失败,进而抛出PytubeError。即使将Pytube升级到最新版本(包括从GitHub安装源码版),问题依然存在。

复现代码

from langchain.document_loaders import YoutubeLoader
loader = YoutubeLoader.from_youtube_url("https://www.youtube.com/watch?v=QsYGlZkevEg", add_video_info=True)
loader.load()

报错信息

KeyError                                  Traceback (most recent call last)
File d:\.......\langchain\.venv\lib\site-packages\pytube\__main__.py:341, in YouTube.title(self)
    340 try:
--> 341     self._title = self.vid_info['videoDetails']['title']
    342 except KeyError:
    343     # Check_availability will raise the correct exception in most cases
    344     #  if it doesn't, ask for a report.

KeyError: 'videoDetails'

During handling of the above exception, another exception occurred:

PytubeError                               Traceback (most recent call last)
Cell In[8], line 1
----> 1 loader.load()

File d:\......\Open AI\langchain\.venv\lib\site-packages\langchain\document_loaders\youtube.py:133, in YoutubeLoader.load(self)
    128 metadata = {"source": self.video_id}
    130 if self.add_video_info:
    131     # Get more video meta info
    132     # Such as title, description, thumbnail url, publish_date
--> 133     video_info = self._get_video_info()
    134     metadata.update(video_info)
    136 try:
...
    350         )
    351     )
    353 return self._title

PytubeError: Exception while accessing title of https://youtube.com/watch?v=QsYGlZkevEg. Please file a bug report at https://github.com/pytube/pytube

解决方案

1. 关闭视频元信息获取(快速临时解决)

如果不需要视频标题、描述等元信息,直接将add_video_info设为False,跳过Pytube获取标题的逻辑即可避免报错:

from langchain.document_loaders import YoutubeLoader
loader = YoutubeLoader.from_youtube_url("https://www.youtube.com/watch?v=QsYGlZkevEg", add_video_info=False)
loader.load()

2. 修改Pytube源码修复标题提取逻辑

找到虚拟环境中Pytube的__main__.py文件(路径类似d:\xxx\langchain\.venv\lib\site-packages\pytube\__main__.py),定位到title属性的代码块,修改异常处理逻辑,尝试从其他字段提取标题:

# 原代码
try:
    self._title = self.vid_info['videoDetails']['title']
except KeyError:
    self.check_availability()
    raise PytubeError(
        f"Exception while accessing title of {self.watch_url}. Please file a bug report at https://github.com/pytube/pytube"
    )

# 修改后的代码
try:
    self._title = self.vid_info['videoDetails']['title']
except KeyError:
    # 尝试从player_response字段提取
    try:
        self._title = self.player_response['videoDetails']['title']
    except KeyError:
        # 兜底返回视频ID作为标题
        self._title = f"Video_{self.video_id}"

3. 手动拆分加载逻辑

如果需要元信息,可以单独用Pytube获取视频数据,再结合Langchain加载字幕:

from pytube import YouTube
from langchain.document_loaders import YoutubeLoader

# 单独获取视频元信息
yt = YouTube("https://www.youtube.com/watch?v=QsYGlZkevEg")
video_metadata = {
    "source": yt.video_id,
    "title": yt.title if hasattr(yt, 'title') else f"Video_{yt.video_id}",
    "description": yt.description,
    "publish_date": yt.publish_date
}

# 加载字幕
loader = YoutubeLoader.from_youtube_url("https://www.youtube.com/watch?v=QsYGlZkevEg", add_video_info=False)
docs = loader.load()
# 手动添加元信息到文档
for doc in docs:
    doc.metadata.update(video_metadata)

内容的提问来源于stack exchange,提问作者Akaza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 02:37:45