Skip to main content

Analyze legal contract risk with Google Gemini hybrid RAG and Supabase

Workflow preview

Workflow preview
100%
Analyze legal contract risk with Google Gemini hybrid RAG and Supabase preview
Open on n8n.io

1. Workflow Overview

What This Workflow Does This workflow transforms any PDF legal contract into a detailed AI powered risk report — in under 5 minutes. Upload a contract, and the system automatically splits it int...

Best for

  • Document Extraction automation workflows
  • AI RAG automation workflows
  • advanced n8n builders looking for reusable templates

Tools used

n8n-nodes-base.extractfromfile, @n8n/n8n-nodes-langchain.googlegemini, n8n-nodes-base.code, n8n-nodes-base.httprequest, n8n-nodes-base.wait, n8n-nodes-base.merge, n8n-nodes-base.set, @n8n/n8n-nodes-langchain.agent

Source and attribution

This workflow is cataloged by N8N Workflows and links back to its original n8n.io source page by Divyanshu Gupta.

Original n8n.io source

1.1 Workflow description

Title
Analyze legal contract risk with Google Gemini hybrid RAG and Supabase
Workflow name
Analyze legal contract risk with Google Gemini hybrid RAG and Supabase

🚀 What This Workflow Does

This workflow transforms any PDF legal contract into a detailed AI-powered risk report — in under 5 minutes. Upload a contract, and the system automatically splits it into clauses, analyses each one using Hybrid RAG (semantic + keyword search), scores risk as HIGH / MEDIUM / LOW, and delivers plain-English explanations with safer alternative wording.


🔥 Why Hybrid RAG?

Most dangerous clauses don't use obvious legal keywords. "The Client accepts full responsibility for all third-party claims" is an indemnification clause — but keyword search misses it. Hybrid RAG combines:

  • Vector Search (pgvector) — finds semantically similar risky patterns
  • BM25 Keyword Search — catches explicit legal red flags
  • RRF Reranking — merges both results with clause-type boosting

🔍 What It Does

  • Accepts a PDF contract via webhook (with async job_id tracking)
  • Splits contract into individual numbered clauses
  • Classifies each clause type using Google Gemini (indemnification, IP, termination, etc.)
  • Generates vector embeddings and searches a Supabase knowledge base
  • Scores each clause HIGH / MEDIUM / LOW using regex + AI
  • AI Agent (Gemini Flash) explains risk in plain language + suggests safer wording
  • Aggregates all results into a single JSON report
  • Saves report to Supabase (frontend polls for result asynchronously)

⚙️ Architecture (Two Pipelines)

Pipeline 1 — Ingestion: Builds the knowledge base of risky clause patterns in Supabase Pipeline 2 — Query: Analyses new contracts against the knowledge base

Both pipelines run in the same workflow — the branch splits at Extract Embedding.


🧠 Key Technical Decisions

  • Async architecture — Frontend fires request + polls Supabase. No timeout issues.
  • job_id tracking — Preserved across all nodes via ...$json spread
  • RRF Reranking — Combines vector + BM25 scores with type-based boost multipliers
  • Regex Risk Scorer — First-pass risk classification before expensive LLM call
  • Gemini Flash — Fast, cost-efficient LLM for per-clause annotation

📦 Requirements

  • Google Gemini API key — for clause classification + embeddings + AI Agent
  • Supabase project — with pgvector extension enabled
  • Supabase tables: legal_clauses (knowledge base) + reports (results)
  • Supabase functions: match_clauses() + keyword_search_clauses()
  • Frontend (optional): HTML/CSS/JS web app hosted on Netlify

💡 Example Use Cases

  • Freelancers reviewing client contracts before signing
  • Startups evaluating vendor or investor agreements
  • Legal ops teams standardising contract review at scale
  • Business owners catching risky clauses without legal fees

🎯 Output

  • Per-clause: risk_level, plain-English explanation, risk_reason, safer_alternative, key_obligations, legal_area
  • Summary: overall_risk_score, risk_distribution, legal_areas map, high_risk_clauses list
  • Stored as JSON in Supabase reports table, keyed by job_id

1.2 Logical Blocks

This catalog entry is organized from the workflow JSON. The node-level section below shows the executable blocks available for review before importing the template.

2. Block-by-Block Analysis

Block 1 - Extract from File

Type / Role
n8n-nodes-base.extractFromFile - extractFromFile
Config choices
Version 1.1

Block 2 - Classify Clause Type

Type / Role
@n8n/n8n-nodes-langchain.googleGemini - googleGemini
Config choices
Version 1.1

Block 3 - extract JSON from Gemini response

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 4 - Generate Embedding

Type / Role
n8n-nodes-base.httpRequest - httpRequest
Config choices
Version 4.4

Block 5 - Prepare Embedding Request

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 6 - Extract Embedding

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 7 - Insert to Supabase

Type / Role
n8n-nodes-base.httpRequest - httpRequest
Config choices
Version 4.4

Block 8 - Vector Search

Type / Role
n8n-nodes-base.httpRequest - httpRequest
Config choices
Version 4.4

Block 9 - Wait

Type / Role
n8n-nodes-base.wait - wait
Config choices
Version 1.1

Block 10 - Keyword Search

Type / Role
n8n-nodes-base.httpRequest - httpRequest
Config choices
Version 4.4

Block 11 - Merge

Type / Role
n8n-nodes-base.merge - merge
Config choices
Version 3.2

Block 12 - Clause Splitter

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 13 - Merge1

Type / Role
n8n-nodes-base.merge - merge
Config choices
Version 3.2

Block 14 - Vector

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 15 - BM25

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 16 - RRF Reranker

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 17 - Risk Scorer

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 18 - AI Agent

Type / Role
@n8n/n8n-nodes-langchain.agent - agent
Config choices
Version 3.1

Block 19 - Prepare LLM data

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 20 - Extract LLM Response

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 21 - Aggregate Node

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 22 - Webhook

Type / Role
n8n-nodes-base.webhook - webhook
Config choices
Version 2.1

Block 23 - Google Gemini Chat Model

Type / Role
@n8n/n8n-nodes-langchain.lmChatGoogleGemini - lmChatGoogleGemini
Config choices
Version 1

Block 24 - get job id

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Showing the first 24 of 37 workflow blocks. Download the JSON for the full node graph.

3. Summary Table

Workflow Analyze legal contract risk with Google Gemini hybrid RAG and Supabase
Complexity advanced
Nodes 37
Categories Document Extraction, AI RAG
Author Divyanshu Gupta
Published 26 Apr 2026

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/15307/15307.json as the source template for this automation.

  2. 2. Import the template into n8n

    Open n8n, import the downloaded JSON, and review each node before activating the workflow.

  3. 3. Configure credentials and variables

    Replace placeholder credentials, API keys, webhook URLs, account IDs, and environment-specific values with your own settings.

  4. 4. Test with sample data

    Run the workflow manually or in a staging workspace, inspect node output, and confirm downstream systems receive the expected data.

  5. 5. Activate and monitor

    Enable the workflow only after testing, then monitor executions, errors, and rate limits during the first production runs.

5. General Notes & Resources

Review imported nodes carefully before activation. This catalog entry is intended to help you inspect the workflow structure, understand required services, and find related templates faster.

Node names, credentials, schedules, webhook paths, and external service limits may need adjustment for your workspace.

Frequently asked questions

What does Analyze legal contract risk with Google Gemini hybrid RAG and Supabase do?

What This Workflow Does This workflow transforms any PDF legal contract into a detailed AI powered risk report — in under 5 minutes. Upload a contract, and the system automatically splits it int...

What do I need before importing this workflow?

Review the workflow JSON, configure any required credentials in n8n, and test the automation in a safe workspace before using it in production.

Can I customize this workflow?

Yes. Use the block-by-block analysis and the downloadable JSON to inspect each node, then adjust credentials, prompts, schedules, filters, or destinations for your Document Extraction, AI RAG use case.