OpenAI Studio icon

OpenAI Studio

Generate content using OpenAI GPT API

Overview

The OpenAI Studio node for n8n enables you to generate content using OpenAI's GPT models via the Chat Completion API. Specifically, with the Chat Completion operation, you can send a prompt (and optionally previous conversation history) to an OpenAI model and receive a generated response. This is useful for building conversational agents, chatbots, customer support assistants, or any workflow that requires natural language generation or understanding.

Practical examples:

  • Automating customer support responses based on user queries.
  • Generating creative writing or summaries from prompts.
  • Building interactive chatbots that remember previous messages in a conversation.

Properties

Name Type Meaning
Model options The OpenAI model to use for generating completions (e.g., GPT-4o, GPT-3.5 Turbo).
Prompt string The text prompt sent to the model; forms the basis of the generated response.
Include Previous Messages boolean Whether to include previous messages from input data to provide context for the conversation.
Previous Messages Field string The field name in the input JSON containing previous messages (used if above is enabled).
Advanced Parameters collection Additional settings to control model behavior (see below for details).

Advanced Parameters (collection):

  • Temperature (number): Controls randomness of output (0 = deterministic, 2 = most random).
  • Top P (number): Nucleus sampling; considers tokens with top_p probability mass.
  • Maximum Output Tokens (number): Maximum number of tokens to generate in the response.
  • System Prompt (string): System message to set assistant behavior.
  • Presence Penalty (number): Penalizes new tokens based on their presence in prior text.
  • Frequency Penalty (number): Penalizes new tokens based on frequency in prior text.
  • Response Format (options): Desired format of the response (Auto, JSON, Text).
  • Tool Calling (boolean): Enable tool calling functionality (advanced use).
  • Tools Definition (string): JSON array defining tools the model can use (if Tool Calling is enabled).

Output

The node outputs a single item per input, with the following structure in the json field:

{
  // ...original input fields,
  "openaiResponse": {
    // Full response object returned by the OpenAI API for chat completion.
    // Typical fields include:
    "id": "...",
    "object": "chat.completion",
    "created": 1234567890,
    "model": "...",
    "choices": [
      {
        "index": 0,
        "message": {
          "role": "assistant",
          "content": "..."
        },
        "finish_reason": "stop"
      }
    ],
    "usage": {
      "prompt_tokens": ...,
      "completion_tokens": ...,
      "total_tokens": ...
    }
    // ...other possible OpenAI fields
  }
}
  • The openaiResponse field contains the raw response from OpenAI, including the generated message(s), token usage, and metadata.

Dependencies

  • External Service: Requires access to the OpenAI API.
  • API Key: You must provide a valid OpenAI API key via n8n credentials (openAiApi). Optionally, an organization ID and custom API endpoint can be configured.
  • n8n Configuration: No special configuration beyond setting up the OpenAI API credentials.

Troubleshooting

Common Issues:

  • Missing or Invalid API Key: If the API key is missing or incorrect, you'll receive an authentication error from OpenAI.
  • Model Not Supported: Using a model not available to your account may result in an error.
  • Input Validation Errors: If required fields like "Prompt" are empty, or if "Previous Messages Field" does not exist in the input, errors will occur.
  • Invalid Tools Definition: If you enable tool calling but provide invalid JSON in "Tools Definition", the node will throw an error indicating invalid tools definition.

Error Messages:

  • OpenAI API Error: <status> - <message>: Indicates an error returned by the OpenAI API. Check the status code and message for details (e.g., invalid parameters, quota exceeded).
  • Image URL is required for image analysis: (Not relevant for Chat Completion, but may appear if wrong operation selected.)
  • Invalid tools definition: <error>: The "Tools Definition" field contains invalid JSON or structure.

How to resolve:

  • Double-check all required fields and ensure they are correctly filled.
  • Ensure your OpenAI API key is active and has sufficient quota.
  • For advanced features (like tool calling), validate your JSON before submitting.

Links and References

Discussion