Mintplex-Labs/anything-llm · error

errors[0] || "Failed to embed document"

Error message

errors[0] || "Failed to embed document"

What it means

Thrown when Document.addDocuments reports at least one path in failedToEmbed; the message is errors[0] (the first underlying embedder/vector error) or the generic fallback when errors is empty. By this point the file has already been copied into storage/documents/custom-documents and the source unlinked, and the finally block deletes the parsed-file row - so recovery must target the copied document, not a retry of this call. Caught internally and returned as { success: false, error, document: null }.

Source

Thrown at server/models/workspaceParsedFiles.js:149

        fs.mkdirSync(customDocsPath, { recursive: true });

      // Copy the file to custom-documents
      const targetPath = path.join(customDocsPath, path.basename(location));
      fs.copyFileSync(sourceFile, targetPath);
      fs.unlinkSync(sourceFile);

      const {
        failedToEmbed = [],
        errors = [],
        embedded = [],
      } = await Document.addDocuments(
        workspace,
        [`custom-documents/${path.basename(location)}`],
        parsedFile.userId
      );

      if (failedToEmbed.length > 0)
        throw new Error(errors[0] || "Failed to embed document");

      const document = await Document.get({
        workspaceId: workspace.id,
        docpath: embedded[0],
      });
      return { success: true, error: null, document };
    } catch (error) {
      console.error("Failed to move and embed file:", error);
      return { success: false, error: error.message, document: null };
    } finally {
      await this.delete({
        id: parseInt(fileId),
        ...(user ? { userId: user.id } : {}),
        workspaceId: workspace.id,
      });
    }
  },

View on GitHub (pinned to 20f6d3546c)

Solutions

  1. Check the server log - the full cause is console.error'd as "Failed to move and embed file" and errors[0] is surfaced in the returned error
  2. Verify the embedder works (System Settings -> AI Providers -> Embedder) by embedding a fresh small document
  3. Ensure the storage directory is writable and has free space
  4. Recover by embedding the already-copied file via Document.addDocuments(workspace, ["custom-documents/<name>"], userId), or re-upload the original

Example fix

// before
const { success, error } = await WorkspaceParsedFiles.moveToDocumentsAndEmbed(user, fileId, workspace);
if (!success) throw new Error(error);

// after - the file was already copied; re-embed that copy instead of retrying the parsed file
const { success, error } = await WorkspaceParsedFiles.moveToDocumentsAndEmbed(user, fileId, workspace);
if (!success && /embed/i.test(error)) {
  await Document.addDocuments(workspace, [`custom-documents/${fileName}`], parsedFile.userId);
}
Defensive patterns

Strategy: retry

Validate before calling

// Probe the embedder before batch-processing uploads
const { success } = await Document.addDocuments(workspace, [tinyProbeFile], systemUser);
if (!success) {
  return res.status(503).json({ error: "Embedder unavailable - check AI provider settings" });
}

Try / catch

const { success, error } = await WorkspaceParsedFiles.moveToDocumentsAndEmbed(user, fileId, workspace);
if (!success && /embed/i.test(error)) {
  // The file was already copied to storage/documents/custom-documents;
  // retry embedding THAT copy - do not call moveToDocumentsAndEmbed again.
  await withBackoff(() =>
    Document.addDocuments(workspace, [`custom-documents/${fileName}`], parsedFile.userId)
  );
}

Prevention

When it happens

Trigger: Embedder unavailable or misconfigured (local embedding model not downloaded, remote embedder key invalid); vector store not writable (disk full, permissions); document text extraction fails on the format; embedding dimension mismatch with existing workspace vectors.

Common situations: Switching the embedder after a workspace already has vectors; storage permission changes after a container rebuild; oversized or scanned (textless) PDFs; upgrading with an incompatible vector index.

Related errors


AI-assisted analysis of Mintplex-Labs/anything-llm@20f6d3546c (2026-08-18). Data as JSON: /api/errors/80f79cac5f165d08. Report an issue: GitHub.