InvalidRequestError: Unrecognized request argument supplied: response_format

InvalidRequestError Traceback (most recent call last) in <cell line: 45>() 44 # Assuming nearby_restaurants is the list of restaurants you have 45 for restaurant in nearby_restaurants: —> 46 details = generate_details_with_gpt3(restaurant) 47 if details: 48 restaurant.update(details) 5 frames /usr/local/lib/python3.10/dist-packages/openai/api_requestor.py in _interpret_response_line(self, rbody, rcode, rheaders, stream) 773 stream_error = stream and “error” in resp.data 774 if stream_error or not 200 <= rcode < 300: → 775 raise self.handle_error_response( 776 rbody, rcode, resp.data, rheaders, stream_error=stream_error 777 ) InvalidRequestError: Unrecognized request argument supplied: response_format

why it doesn’t work?


The response_format parameter works only for the newer call for chat.completion.create from Openai v1.0.0+.

This call from the previous version does not have that as a parameter. You can prompt it through, to always return the output as a json and that should work in your case

The json mode is not working, I do have the latest version. what do you think?

AttributeError: ‘OpenAI’ object has no attribute ‘Completions’

and it say that

The function call is not correct. I’ll attach a link to the API doc where the call is : OpenAI Platform

but essentially, it need to look like:

from openai import OpenAI
client = OpenAI()

completion = client.chat.completions.create()

The link will have how to call on the JSON format parameter as well

1 Like

I just released a new YouTube tutorial on how to get a response in JSON format. Also, see my GitHub repository with full code for the tutorial.

import os
from openai import OpenAI
client = OpenAI()
OpenAI.api_key = os.getenv('OPENAI_API_KEY')

completion = client.chat.completions.create(
  model="gpt-4-1106-preview",
  messages=[
    {"role": "system", "content": "You are a helpful assistant. Your response should be in JSON format."},
    {"role": "user", "content": "Hello!"}
  ],
  response_format={"type": "json_object"}
)

print(completion.choices[0].message.content)
1 Like

Here’s how to use the response format correctly.

The only thing using it got me on gpt-3.5-turbo-1106 was the wrong answer, the score of the last game instead of the series stats.

from openai import OpenAI
client = OpenAI(timeout=20)
response = client.chat.completions.create(
  max_tokens=50,
  response_format = {"type": "json_object"},
  model="gpt-3.5-turbo-1106",
  messages=[
    {"role": "system", "content": "You are a helpful assistant who responds in json."},
    {"role": "user", "content": "Print baseball World Series stats for 2021:"
     "keys: ['team1', 'team2', 'team1_score', 'team2_score']"},
  ]
)
print(response.choices[0].message.content)

{
“team1”: “Atlanta Braves”,
“team2”: “Houston Astros”,
“team1_score”: 7,
“team2_score”: 0
}

without:

from openai import OpenAI
client = OpenAI(timeout=20)
response = client.chat.completions.create(
  max_tokens=50,
  # response_format = {"type": "json_object"},
  model="gpt-3.5-turbo-1106",
  messages=[
    {"role": "system", "content": "You are a helpful assistant who responds in json."},
    {"role": "user", "content": "Print baseball World Series stats for 2021:"
     "keys: ['team1', 'team2', 'team1_score', 'team2_score']"},
  ]
)
print(response.choices[0].message.content)

{
“team1”: “Atlanta Braves”,
“team2”: “Houston Astros”,
“team1_score”: 4,
“team2_score”: 2
}

1 Like