Skip to main content

OpenAI Integration

The OpenAI integration allows you to leverage OpenAI’s powerful AI models including GPT-4, DALL-E, Whisper, and Sora for a variety of tasks including text generation, image generation and analysis, audio transcription and generation, and video generation.

Available Nodes

n8n provides two types of OpenAI nodes:

OpenAI Node

Specialized operations: image generation (DALL-E), audio (Whisper, TTS), and video generation (Sora)

OpenAI Chat Model

For text generation, reasoning, and tools - use with AI Agent
For most text generation and LLM tasks, use the AI Agent node with OpenAI Chat Model instead of the standalone OpenAI node. The OpenAI node is optimized for specialized operations like image, audio, and video generation.

Prerequisites

Before you begin, you’ll need:
  • An OpenAI account
  • An OpenAI API key (get one from OpenAI Platform)
  • Sufficient API credits in your OpenAI account

Setup

1

Create OpenAI API Credentials

  1. Go to the OpenAI API Keys page
  2. Click “Create new secret key”
  3. Give your key a name and copy it immediately (you won’t be able to see it again)
  4. Store the key securely
2

Add Credentials in n8n

  1. In your n8n workflow, add an OpenAI node
  2. Click on the Credential to connect with dropdown
  3. Click Create New Credential
  4. Paste your API key
  5. (Optional) Configure custom base URL if using a proxy
  6. Click Save
3

Test the Connection

Test your credentials by running a simple operation to ensure everything is configured correctly.

OpenAI Node

The OpenAI node provides specialized operations for image, audio, and video generation. It supports multiple resources and operations.

Available Resources

Generate text responses and perform classifications using GPT models.Operations:
  • Message: Send messages to GPT models and get responses
  • Classify: Classify text using moderation models
Example Use Cases:
  • Generate text completions
  • Content moderation
  • Multi-turn conversations
  • Structured output with JSON mode

Advanced Features

Function Calling / Tools

Connect custom n8n tools to the OpenAI node to enable function calling:
  1. Add tool nodes to your workflow (e.g., HTTP Request Tool, Code Tool)
  2. Connect them to the Tools input on the OpenAI node
  3. OpenAI will automatically call these tools when needed

Structured Output

Use JSON mode or response format to get structured data:
{
  "responseFormat": "json_object",
  "prompt": "Extract the name, email, and phone from this text as JSON: ..."
}
When using JSON mode, include the word “json” in your prompt and use models released after November 2023.

Streaming Responses

Enable streaming for real-time responses:
{
  "options": {
    "stream": true
  }
}

OpenAI Chat Model

The OpenAI Chat Model node is designed for use with LangChain components, particularly the AI Agent.

Configuration

1

Add Chat Model Node

Add the OpenAI Chat Model node to your workflow.
2

Select Model

Choose from available models:
  • GPT-4 Turbo: Most capable, best for complex tasks
  • GPT-4: High capability, balanced performance
  • GPT-3.5 Turbo: Fast and cost-effective
  • o1/o3 models: Advanced reasoning capabilities
3

Configure Options

Set temperature, max tokens, and other parameters:
  • Temperature: Controls randomness (0-2)
  • Max Tokens: Limit response length
  • Top P: Nucleus sampling parameter
  • Frequency Penalty: Reduce repetition
  • Presence Penalty: Encourage new topics
4

Connect to AI Agent

Connect the Chat Model to an AI Agent node:

Model Selection

ModelBest ForContext WindowCapabilities
GPT-4 TurboComplex reasoning, latest features128K tokensVision, JSON mode, function calling
GPT-4Balanced performance8K tokensHigh accuracy, reliable
GPT-3.5 TurboSpeed and cost16K tokensFast responses, good for simple tasks
o1-previewAdvanced reasoning128K tokensComplex problem solving
o3-miniEfficient reasoning128K tokensCost-effective reasoning

Response Formats

{
  "responseFormat": "text"
}

Common Use Cases

1. Content Generation

Generate blog posts, product descriptions, or marketing copy:

2. Image Analysis Pipeline

Analyze images and take actions based on content:

3. Audio Transcription

Transcribe audio files and process the text:

4. AI Agent with Tools

Create an intelligent agent that can use multiple tools:

Best Practices

1

Choose the Right Model

  • Use GPT-4 for complex reasoning and high-quality outputs
  • Use GPT-3.5 Turbo for speed and cost efficiency
  • Use specialized models (DALL-E, Whisper) for specific tasks
2

Optimize Token Usage

  • Set appropriate max_tokens limits
  • Use system messages to set context efficiently
  • Consider caching responses for repeated queries
3

Handle Errors Gracefully

  • Implement retry logic for rate limits
  • Check error responses and handle them appropriately
  • Monitor API usage and costs
4

Use Streaming for Long Responses

  • Enable streaming for better user experience
  • Process chunks as they arrive
  • Handle connection interruptions
5

Secure Your API Keys

  • Never commit API keys to version control
  • Use n8n’s credential system
  • Rotate keys regularly
  • Monitor usage for anomalies

Troubleshooting

Rate Limits

If you encounter rate limit errors:
  1. Implement exponential backoff
  2. Upgrade your OpenAI plan
  3. Use batch processing where possible
  4. Cache responses to reduce API calls

Token Limits

If responses are cut off:
  1. Increase max_tokens parameter
  2. Split large inputs into chunks
  3. Use models with larger context windows
  4. Summarize previous context

Model Not Found

If a model is unavailable:
  1. Check your OpenAI account access level
  2. Verify the model name is correct
  3. Ensure your API key has access to the model
  4. Check OpenAI’s status page for outages

Resources