Quick Overview
This subworkflow ingests uploaded documents (PDF, DOCX, TXT, or CSV), extracts text, uses Google Gemini to generate metadata and embeddings, and stores chunked vectors in PostgreSQL with pgvector for semantic search while skipping unchanged duplicates.
How it works
- Receives document metadata (
user_id, file_name, optional mime_type) and a binary file in binary.data from an Execute Workflow trigger.
- Validates required fields, enforces allowed file types and a 20 MB limit, and routes the file to the appropriate text extraction method (PDF/DOCX/TXT/CSV).
- Consolidates the extracted output into a single normalized text body and stops with an error if the extracted text is too short.
- Calls the Google Gemini generateContent API to extract document metadata (title, type, summary, keywords, language) and parses the JSON response.
- Hashes the extracted text and upserts a document record in PostgreSQL, detecting duplicates by comparing the stored content hash for the same user and filename.
- For new or changed documents, splits the text into overlapping chunks, generates a 768-dimension embedding for each chunk with the Google Gemini embedding API, and formats the vector for pgvector.
- Inserts each chunk, embedding, and metadata into PostgreSQL (
document_chunks) and updates the document status to indexed, returning an ingestion result with the stored chunk count.
Setup
- Create a Google Gemini API key and configure an HTTP Header Auth credential that sends
x-goog-api-key to the Gemini HTTP Request nodes.
- Set up a PostgreSQL database with the companion SQL (including
documents and document_chunks tables and pgvector with 768 dimensions) and add a PostgreSQL credential in n8n.
- Ensure the calling workflow passes
json.user_id, json.file_name, optional json.mime_type, and the uploaded file as binary.data to this subworkflow.
- If you change embedding dimensionality, update both the Gemini embedding request (
outputDimensionality) and the pgvector column/type in PostgreSQL to match.
- Verify your n8n version supports DOCX extraction in Extract From File, and add an OCR/conversion step upstream for scanned PDFs or unsupported formats.