Skip to content

sveneisenschmidt/openwebui-n8n-function

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Open WebUI n8n Pipeline

A simplified n8n pipeline for Open WebUI that lets you connect your chats directly to n8n workflows, agents, and AI models.

Files in this Repository

  • n8n_pipeline.py - The main pipeline code (recommended for production)
  • n8n_pipeline_remote.py - Remote import version that loads code from GitHub
  • n8n_workflow.json - Example n8n workflow to get started
  • README.md - This file

What does it do?

This pipeline sends messages from Open WebUI to your n8n workflows and streams the responses back. It enables you to:

  • Embed n8n agents directly into Open WebUI
  • Run n8n workflows as chat models
  • Use AI models hosted in n8n without needing separate middleware
  • Build custom AI assistants with n8n's visual workflow editor
  • Configure multiple models with different n8n webhooks

Think of it as a bridge: Open WebUI handles the chat interface, n8n handles the AI logic and model orchestration.

Why use this?

  • Direct integration - Talk directly to n8n, no need for LiteLLM or OpenRouter as middleware
  • Streaming support - See responses appear in real-time
  • Session tracking - Each chat gets a unique session ID so n8n knows which conversation is which
  • Simple setup - Just webhook URLs and optional bearer token
  • Use any model - If n8n can call it, Open WebUI can use it
  • Multiple models - Configure different workflows as separate models in the UI
  • Leverage n8n features - Use n8n's credential store, environment variables, and enterprise features for API key management

Note: You can still use services like OpenRouter or LiteLLM - just call them directly from within your n8n workflows. This gives you the benefit of n8n's credential management and environment handling rather than configuring them separately in Open WebUI.

Installation

Option 1: Direct Installation (Recommended for Production)

  1. Copy the n8n_pipeline.py file
  2. In Open WebUI, go to Workspace → Functions
  3. Click + to add a new function
  4. Paste the code
  5. Configure your settings (see below)

Option 2: Remote Import (Easy Updates & Reusability)

If you prefer not to copy/paste the entire code, you can use the remote import option. This makes it easy to reuse the function across multiple Open WebUI instances.

Option 2a: Load from GitHub (public)

  1. Copy the n8n_pipeline_remote.py file
  2. In Open WebUI, go to Workspace → Functions
  3. Click + to add a new function
  4. Paste the code as-is
  5. The function will automatically load the latest version from this GitHub repository

Option 2b: Load from your own server (recommended for production)

If you don't want to load code directly from GitHub, you can host the file on your own server:

  1. Upload n8n_pipeline.py to your own web server (e.g., https://your-server.com/n8n_pipeline.py)
  2. Copy n8n_pipeline_remote.py and modify the URL:
   response = requests.get(
       "https://your-server.com/n8n_pipeline.py"  # Your server URL
   )
  1. In Open WebUI, go to Workspace → Functions
  2. Click + to add a new function
  3. Paste your modified remote import code

Benefits of Remote Import:

  • Easy updates - Update the main file once, all instances automatically use the new version on restart
  • Reusability - Use the same small import script across multiple Open WebUI installations
  • Version control - Keep the main code on your infrastructure under your control
  • Less clutter - Functions in Open WebUI stay small and readable

Note: The remote import requires internet access from your Open WebUI server and will fetch the code on each Open WebUI restart. For maximum reliability and security in production environments, Option 1 (direct installation) or Option 2b (your own server) are recommended.

Configuration

Configure these settings in the function's valves:

Setting Description Required Default
MODELS JSON object mapping model names to webhook URLs Yes {"Default Agent": "https://..."}
BEARER_TOKEN Bearer token for authenticating with webhooks No ""
RESPONSE_FIELD Field name for the response in the payload No "output"
SEND_CONVERSATION_HISTORY Send full conversation history to n8n No false
TIMEOUT Request timeout in seconds No System default
SHOW_STATUS_MESSAGES Show debug messages No false

Configuring Multiple Models

The MODELS configuration uses a simple JSON format where the key is the model name and the value is the full webhook URL:

{
  "GPT-4 Agent": "https://n8n.example.com/webhook/gpt4-agent/chat",
  "Claude Agent": "https://n8n.example.com/webhook/claude-agent/chat",
  "Research Assistant": "https://n8n.example.com/webhook/research-bot/chat"
}

Each model will appear individually in the Open WebUI model selector.

Real-World Example: Complete Model Management via n8n

Here's an example of how this pipeline can centralize your AI infrastructure. Instead of managing multiple middleware services, handle everything through n8n:

Setup: Remove LiteLLM/OpenRouter from Open WebUI, create n8n workflows instead.

Configuration:

{
  "Workflow Builder Agent": "https://n8n.example.com/webhook/22f25747-3187-48fe-a271-b206a93e0898/chat",
  "Claude Haiku 3.5": "https://n8n.example.com/webhook/a54379c4-ad02-4bbe-919b-a79307f37687/chat",
  "Claude Opus 4.1": "https://n8n.example.com/webhook/e1999694-2603-468e-957d-43edc204eafc/chat",
  "Claude Sonnet 4.5": "https://n8n.example.com/webhook/ee0c7b37-5553-40b1-9ee2-dc12fc8aac00/chat"
}

Result:

  • Three standard Claude models (Haiku, Opus, Sonnet) - all managed through n8n
  • One specialized agent with a custom system prompt for building n8n workflows
  • All appearing as native models in Open WebUI's model selector
  • Full control over prompts, memory, and behavior in n8n's visual editor
  • API keys managed securely in n8n's credential store
  • Environment-specific configurations handled by n8n

Benefits:

  • Centralized credential management - Store API keys in n8n's credential store instead of multiple places
  • Environment handling - Use n8n's environment variables for different deployments (dev/staging/prod)
  • Single point of control - All AI model configurations, routing, and logic in one place
  • Flexibility - Mix direct API calls, OpenRouter, LiteLLM, or any provider within n8n workflows
  • Enterprise features - If using n8n Enterprise, benefit from SSO, audit logs, and access controls

What gets sent to n8n?

Your n8n workflow receives this payload:

{
  "systemPrompt": "...",
  "currentMessage": "user's current message",
  "chatInput": "user's current message",
  "sessionId": "unique-chat-id",
  "userId": "user-id",
  "userEmail": "user@example.com",
  "userName": "User Name"
}

Optional: If you enable SEND_CONVERSATION_HISTORY, the payload will also include:

{
  ...
  "messages": [
    {"role": "user", "content": "previous message"},
    {"role": "assistant", "content": "previous response"},
    {"role": "user", "content": "current message"}
  ]
}

Note: By default, this pipeline does NOT send conversation history. Only the current message is sent. For managing conversation context, we recommend using n8n's built-in memory function with the sessionId to store and retrieve conversation state. This gives you more control and flexibility in n8n.

n8n Workflow Setup

Required Configuration

For your n8n workflows to work correctly with this pipeline, configure them as follows:

1. Chat Node (Webhook & Trigger)

  • Add a Chat node - this acts as both the webhook and entry point
  • Set Mode to Embedded Chat
  • Set Response Mode to Streaming
  • Important: Copy the full URL from the Chat URL field
    • This URL ends with /chat in the path
    • Use this complete URL in your MODELS configuration

Example Chat URL:

https://n8n.example.com/webhook/abc123/chat

2. AI Agent Node

  • Connect an AI Agent node to the Chat node
  • Enable Enable Streaming option

3. Memory (Optional but Recommended)

  • Add a Memory Tool (e.g., Simple Memory)
  • Use default settings
  • The sessionId from Open WebUI's chat_id will be automatically used for session management
  • This allows n8n to maintain conversation context across messages

Response Format

Note: When using the Chat and AI Agent nodes as described above, the response format is handled automatically. The following information is only relevant if you're building custom workflows without the AI Agent node.

Streaming (Recommended): Send individual JSON chunks with content:

{"content": "This "}
{"content": "is "}
{"content": "streaming"}

Non-streaming: Return a JSON object with your configured RESPONSE_FIELD (default: "output"):

{
  "output": "This is my response"
}

Example Workflow

An example n8n workflow with the correct configuration is included in this repository: n8n_workflow.json

You can import it directly into n8n to get started quickly. The example includes:

  • Properly configured Chat node with streaming
  • AI Agent with streaming enabled
  • Memory tool for conversation context

Use Cases

  • AI Agent hosting - Build complex agents in n8n, use them in Open WebUI
  • Custom model routing - Route different requests to different models in n8n
  • RAG workflows - Implement retrieval-augmented generation entirely in n8n
  • Tool-using agents - Use n8n's integrations as tools for your AI
  • Model fallbacks - Try multiple models in n8n until one works
  • Cost optimization - Route to cheaper models for simple queries
  • Multi-model setups - Different workflows for different purposes (coding, writing, research, etc.)

Credits

This project is based on owndev's n8n streaming pipeline.

Original Author: owndev
Original Project: Open-WebUI-Functions

Full credit to owndev for the streaming architecture, n8n integration patterns, and core functionality design. This simplified version builds on their excellent foundation.

Changes from Original

  • Removed encryption support (simplified deployment)
  • Removed Cloudflare Access support
  • Removed conversation history by default (optional via setting) - use n8n's memory function for context management
  • Added mandatory sessionId tracking
  • Added multi-model support with configurable webhook URLs
  • Connection reuse with persistent sessions
  • Adjusted code structure
  • Removed think block complexity

Compatibility

Tested with Open WebUI v0.6.33

License

Apache License 2.0 (same as original)

Issues?

If something doesn't work:

  1. Check your webhook URLs are correct in the MODELS configuration
  2. Make sure your n8n workflows are active
  3. Enable SHOW_STATUS_MESSAGES to see what's happening
  4. Check Open WebUI logs for errors
  5. Test your n8n webhooks directly first (use curl or Postman)
  6. Verify your JSON format in MODELS is valid

Check the original project for more detailed n8n workflow examples and templates.

About

Simplified and optimized n8n pipeline for Open WebUI. Stream responses from n8n workflows directly into your chats with session tracking.

Resources

License

Stars

12 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages