Mintplex-Labs/anything-llm · error
Invalid video id!
Error message
Invalid video id!
What it means
Thrown by the YoutubeLoader constructor when the `videoId` argument is falsy (null, undefined, empty string, or 0). This is an in-house port of LangChain's YouTube loader; the constructor is the single gate that guarantees a downstream fetch URL can be built. It fires before any network call, so it is purely an input-contract violation, not a transient failure.
Source
Thrown at collector/utils/extensions/YoutubeTranscript/YoutubeLoader/index.js:16
const { validYoutubeVideoUrl } = require("../../../url");
/*
* This is just a custom implementation of the Langchain JS YouTubeLoader class
* as the dependency for YoutubeTranscript is quite fickle and its a rat race to keep it up
* and instead of waiting for patches we can just bring this simple script in-house and at least
* be able to patch it since its so flaky. When we have more connectors we can kill this because
* it will be a pain to maintain over time.
*/
class YoutubeLoader {
#videoId;
#language;
#addVideoInfo;
constructor({ videoId = null, language = null, addVideoInfo = false } = {}) {
if (!videoId) throw new Error("Invalid video id!");
this.#videoId = videoId;
this.#language = language;
this.#addVideoInfo = addVideoInfo;
}
/**
* Extracts the videoId from a YouTube video URL.
* @param url The URL of the YouTube video.
* @returns The videoId of the YouTube video.
*/
static getVideoID(url) {
const videoId = validYoutubeVideoUrl(url, true);
if (videoId) return videoId;
throw new Error("Failed to get youtube video id from the url");
}
/**
* Creates a new instance of the YoutubeLoader class from a YouTube videoView on GitHub (pinned to 526360e320)
Solutions
- Validate that `videoId` is a non-empty 11-character string before constructing YoutubeLoader.
- Prefer the `YoutubeLoader.createFromUrl(url, config)` static factory, which extracts and validates the id in one step.
- If constructing directly, source the id only from `YoutubeLoader.getVideoID(url)` / `YoutubeTranscript.retrieveVideoId(input)` so it can never be falsy.
- Wrap construction in try/catch and surface a user-facing 'invalid YouTube link' message instead of crashing the ingestion pipeline.
Example fix
// before
const loader = new YoutubeLoader({ videoId: maybeMissing });
// after
if (!videoId || videoId.length !== 11) throw new Error('A valid 11-char YouTube video id is required');
const loader = new YoutubeLoader({ videoId }); Defensive patterns
Strategy: validation
Validate before calling
function assertVideoId(videoId) {
if (typeof videoId !== 'string' || !/^[A-Za-z0-9_-]{11}$/.test(videoId)) {
throw new Error('A valid 11-character YouTube video id is required');
}
return videoId;
}
// run before constructing YoutubeLoader
const loader = new YoutubeLoader({ videoId: assertVideoId(maybeId) }); Type guard
function isValidVideoId(v) {
return typeof v === 'string' && /^[A-Za-z0-9_-]{11}$/.test(v);
} Try / catch
try {
const loader = new YoutubeLoader({ videoId });
} catch (e) {
if (/Invalid video id/i.test(e.message)) {
return { error: 'Please provide a valid YouTube video link.' };
}
throw e;
} Prevention
- Always obtain the videoId via YoutubeLoader.getVideoID(url) or YoutubeTranscript.retrieveVideoId(input) rather than passing raw user input.
- Prefer the createFromUrl factory so extraction and construction are atomic.
- Unit-test the loader with empty/null/undefined ids to lock the contract.
When it happens
Trigger: Calling `new YoutubeLoader({})`, `new YoutubeLoader({ videoId: null })`, or `new YoutubeLoader({ videoId: '' })` directly. Also reached indirectly when `YoutubeLoader.createFromUrl(url)` succeeds in parsing but a caller passes the result's videoId as undefined, or when a code path constructs the loader from an unvalidated user-supplied field that happens to be empty.
Common situations: A connector or data-source ingestion job receives a YouTube record whose URL/id field is missing or was stripped by an upstream sanitizer; a refactor renames the `videoId` field but forgets one call site; tests that instantiate the loader without a fixture id.
Related errors
- Impossible to retrieve Youtube video ID.
- Failed to get youtube video id from the url
- Filename is required!
- Invalid scope: ${JSON.stringify(v)}
- Content must be a non-empty string
AI-assisted analysis of Mintplex-Labs/anything-llm@526360e320 (2026-08-13).
Data as JSON: /api/errors/cd4f1f0470caf0e0.
Report an issue: GitHub.