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.

2 Likes

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.

I have been using response_format={ā€œtypeā€: ā€œjson_objectā€},

but it ends up adding a bunch of new line and tab characters (\n and \t) in the end.
This is too unclean and not really a valid json object. I am using gpt-4-1106-preview

\n \t are whitespace and valid in json, if you use a parser it will just remove it if its within the syntax (outside of the actual data), if its in the data, \n is newline, \t is tab, just do string replacements if you donā€™t want those characters in your data.