[Fixed] OpenAI ChatGPT (GPT-3.5) API error: "'messages' is a required property" when testing the API with Postman – Openai-api

by
Ali Hasan
chatgpt-api openai-api

Quick Fix: The ‘messages’ parameter is required when using the Chat Completions API (i.e., the GPT-3.5 API). Ensure to include it in your request body, as demonstrated in the example provided.

The Problem:

When testing the OpenAI ChatGPT (GPT-3.5) API with Postman, users encounter an error stating "’messages’ is a required property" despite including the "messages" property in the request body. This error prevents successful completion generation. The request includes the necessary model, max_tokens, top_p, temperature, frequency_penalty, presence_penalty, and prompt parameters, but the error persists.

The Solutions:

Solution 1: Chat Completions API requires `messages` parameter

When using the Chat Completions API (i.e., the GPT-3.5 API), the messages parameter is required. Unlike the Completions API, the prompt parameter is not valid in this context.

Corrected Request:

POST https://api.openai.com/v1/chat/completions

Body:

{
    "model": "gpt-3.5-turbo",
    "messages": [{
        "role": "user",
        "content": "Hello!"
    }],
    "max_tokens": 512,
    "top_p": 1,
    "temperature": 0.5,
    "frequency_penalty": 0,
    "presence_penalty": 0
}

Solution 2: Provide ‘messages’ Property in Request Body

OpenAI’s error message indicates that your request body lacks the required property messages. To address this, you must provide this property within your request body. The messages property should contain a list of message objects, each representing a single turn or message in the conversation.

Here’s an example of a corrected request body:

{
    "model": "gpt-3.5-turbo",
    "messages": [
        {
            "role": "user",
            "content": "Tell me a joke."
        }
    ],
    "temperature": 1,
    "max_tokens": 512,
    "top_p": 1,
    "temperature": 0.5,
    "frequency_penalty": 0,
    "presence_penalty": 0
}

By including the messages property and specifying the user’s prompt as the content of the first message, you’ll be able to successfully make the request and receive a completion from the model.

Q&A

What is the error when testing the API with Postman?

The ‘messages’ property is required in the request body.

What is the difference between using the Completions API and Chat Completions API?

The Completions API uses ‘prompt’ while the Chat Completions API uses ‘messages’.

Video Explanation:

The following video, titled "Related Video", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

This video provides further insights and detailed explanations related to the content discussed in the article.