Using response_format in chat completion throws error

Do this:

import os
from openai import OpenAI
from openai.types.chat.completion_create_params import ResponseFormat

client = OpenAI(api_key=os.environ("OPENAI_API_KEY"))
response_format = ResponseFormat(type="json_object")

response = client.chat.completions.create(
    model="gpt-4-vision-preview",
    response_format=response_format,
    messages=[
        {
            "role": "system",
            "content": "You model sentences as arrays of JSON nodes and edges." 
        },
        {
            "role": "user",
            "content": "Sentence: ..."
        }
    ],
    max_tokens=300,
)

You get this error:

BadRequestError: Error code: 400 - {'error': {'message': '1 validation error for Request\nbody -> response_format\n  extra fields not permitted (type=value_error.extra)', 'type': 'invalid_request_error', 'param': None, 'code': None}}

I tried using requests and calling the endpoint directly, not via the Python library, and got the same error.

1 Like

Use

response_format={"type": "json_object"},

in the completions create

Thanks, but that throws the same error. It’s the reason I went down the path of trying response_format = ResponseFormat(type="json_object").

The issue seems to be that response_format isn’t recognized. It’s getting invalidated as an extra, unexpected field.

Okay, USER ERROR!

I was using the wrong model :frowning:

This works: gpt-4-1106-preview.

This doesn’t: gpt-4-vision-preview.

So, note that you can’t get JSON output with the vision model, at least with the current preview.

2 Likes

Hello, I’m looking for a response_format doing this:

response_format: { type: ‘json_list’ },

Any ideas on how to do it? The purpose is to return list with consistently valid JSON format to be parsed after, for now I’m just adding tokens in the prompt to achieve this result.

I’m not aware that “json_list” is a valid response format.

I think if you want to do what you’re asking, you’ll need to try one of these approaches:

  • Use json_object and prompt for an array in the result. For example, include something like, “Return a list of X” in your prompt. Then you should get an array object in your JSON.
  • Use function call. This will give you more control. You can define JSON Schema that specifies an array attribute to guarantee getting back an array.
1 Like

thanks, implicite description of the expected result in the system and user prompt still seems to be an ideal solution at the moment, without the use of the json_object param.