N8N Pipe for Open-WebUI
This module implements a robust, asynchronous pipe for Open-WebUI that serves as an interface between the application and n8n workflows. It allows redirecting chat messages to n8n workflows and incorporating responses back into the conversation.
Based on/forked from https://openwebui.com/f/coleam/n8n_pipe. Many thanks to the original author.
Hosted in openwebui: https://openwebui.com/f/quintana/n8n_pipe_ng
Features
- ✅ Fully asynchronous interface compatible with Open-WebUI
- ✅ Enhanced HTTP and general error handling with retry mechanism
- ✅ Verification of expected response fields
- ✅ Uses
httpxfor asynchronous HTTP requests - ✅ Configurable timeout to avoid request blocking
- ✅ Message history management
- ✅ Comprehensive input validation
- ✅ Detailed status updates during processing
- ✅ Robust error handling for edge cases
Installation
-
Ensure you have the required dependencies installed:
pip install httpx pydantic -
Add the pipe file to your Open-WebUI installation.
Configuration
The Pipe class uses a "valves" system to configure its behavior:
| Parameter | Description | Default Value |
|---|---|---|
n8n_host |
Base URL for n8n server (without trailing slash) | http://localhost:5678 |
n8n_webhook_id |
Webhook ID from n8n | your-webhook-id-here |
n8n_test_mode |
Whether to use test mode URLs for n8n webhooks | False |
n8n_bearer_token |
Bearer authentication token | your-token-here |
input_field |
Input field name in the request | chatInput |
response_field |
Response field name in the JSON response | output |
emit_interval |
Interval between status emissions (seconds) | 2.0 |
enable_status_indicator |
Enable or disable status indicators | True |
timeout |
HTTP request timeout (seconds) | 30.0 |
max_retries |
Maximum number of retries for failed requests | 2 |
history_limit |
Maximum number of messages to keep in history | 10 |
Handling Test Mode vs Production
The pipe automatically handles the difference between n8n test mode and production:
-
When
n8n_test_modeis set toFalse(default), it uses the URL format:{n8n_host}/webhook/{n8n_webhook_id} -
When
n8n_test_modeis set toTrue, it uses the URL format:{n8n_host}/webhook-test/{n8n_webhook_id}
This allows you to easily switch between testing and production environments without changing the webhook URL manually.
Data Format
Data Sent to n8n
The data format sent to the n8n webhook is as follows:
{
"sessionId": "user_id - content_of_first_message",
"chatInput": "question_extracted_from_last_message"
}
Where:
sessionIdis a string combining the user ID and the content of the first message (limited to 100 characters)- The input field (default
chatInput) is configured viavalves.input_field
Data Received from n8n
The expected response from n8n must be in JSON format and contain a field specified by valves.response_field (default "output"):
{
"output": "Text response from the n8n workflow"
}
Status Emission Format
When an event emitter is provided, the module emits events in the following format:
{
"type": "status",
"data": {
"status": "in_progress|complete",
"level": "info|warning|error",
"description": "Message describing the current status",
"done": false|true
}
}
Usage Flow
- The user sends a message in the Open-WebUI interface
- The pipe intercepts this message and sends it to the configured n8n webhook
- n8n processes the message according to its configured workflow
- The pipe receives the response from n8n and adds it to the conversation
- The interface displays the response as if it came directly from the language model
Error Handling
The pipe includes comprehensive error handling:
- HTTP Errors: All HTTP errors are caught, logged, and reported back with appropriate status codes.
- Retry Mechanism: Failed requests can be automatically retried based on the
max_retriessetting. - Validation: Input validation ensures proper URLs and field structures.
- Message Validation: Checks for empty messages, non-user messages, etc.
- Response Field Validation: Ensures the expected response field exists in the n8n response.
Configuring n8n
To configure n8n to work with this pipe:
- Create a workflow in n8n
- Add a Webhook node as an entry point (trigger)
- Configure this webhook to accept POST requests with Bearer authentication
- Process the input data from the field configured by
input_field(default: "chatInput") - Return the response in a JSON object containing the field configured by
response_field(default: "output")
Troubleshooting
Common issues and solutions:
-
Error "No messages found in the request body"
- Check that the request body contains a non-empty
messagesarray
- Check that the request body contains a non-empty
-
Error "Last message is not from user"
- Ensure the last message in the array has
role: "user"
- Ensure the last message in the array has
-
Error "Empty question received"
- The last user message has empty content
-
HTTP error when calling n8n
- Verify that the webhook URL is correct
- Check that the Bearer token is valid
- Ensure that the n8n webhook is accessible from Open-WebUI
-
Missing response field
- Verify that your n8n workflow returns a JSON with the field configured in
response_field
- Verify that your n8n workflow returns a JSON with the field configured in
-
Retry failed
- If all retries fail, check your n8n server availability
- Consider increasing the
max_retriesortimeoutvalues
Testing
Comprehensive tests are provided to ensure code reliability. To run them:
python -m pytest -xvs
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.