fix: use the Youtube Proxy URL for YouTube transcripts even when HTTPS_PROXY is set (#35124)

With HTTPS_PROXY set on the server, fetching a YouTube transcript went through that proxy instead of the configured Youtube Proxy URL, without any message saying so. Servers behind a company proxy got the "YouTube blocked the transcript request" error even though they had set a separate proxy for YouTube. The Youtube Proxy URL now always wins when it is set, and HTTPS_PROXY is still used when it is not. Custom CA certificates set through REQUESTS_CA_BUNDLE keep working either way.

Fixes #35121
This commit is contained in:
Classic298 authored and GitHub committed 2026-11-27 02:36:54 +04:00
1 parent 8103755d47
commit ee188b4316
1 file changed
+15 -1
@@ -3,6 +3,7 @@ from typing import Any, Dict, Generator, List, Optional, Sequence, Union
from urllib.parse import parse_qs, urlparse
from xml.etree.ElementTree import ParseError
import requests
from langchain_core.documents import Document
log = logging.getLogger(__name__)
@@ -22,6 +23,17 @@ class YoutubeTranscriptError(Exception):
"""A YouTube transcript could not be retrieved."""
class _YoutubeProxySession(requests.Session):
"""A session whose own proxies win over the environment's proxies."""
def merge_environment_settings(
self, url: str, proxies: dict | None, stream: bool | None, verify: bool | str | None, cert: str | tuple | None
) -> dict:
settings = super().merge_environment_settings(url, proxies, stream, verify, cert)
settings['proxies'] = self.proxies
return settings
def _transcript_error_message(error: Exception, video_id: str) -> str:
name = type(error).__name__
@@ -116,11 +128,13 @@ class YoutubeLoader:
if self.proxy_url:
youtube_proxies = GenericProxyConfig(http_url=self.proxy_url, https_url=self.proxy_url)
http_client = _YoutubeProxySession()
log.debug('Using proxy URL: %s...', self.proxy_url[:14])
else:
youtube_proxies = None
http_client = None
transcript_api = YouTubeTranscriptApi(proxy_config=youtube_proxies)
transcript_api = YouTubeTranscriptApi(proxy_config=youtube_proxies, http_client=http_client)
try:
transcript_list = transcript_api.list(self.video_id)
except Exception as e: