I am calling OpenAI’s api using the response_format of “json_object”, as I have been doing for months, and all the sudden it is broken. Getting this error:
Error: [400 Bad Request]>
<CIMultiDictProxy(‘Date’: ‘Wed, 28 Aug 2024 22:17:25 GMT’, ‘Content-Type’: ‘application/json’, ‘Content-Length’: ‘219’, ‘Connection’: ‘keep-alive’, ‘Access-Control-Expose-Headers’: ‘X-Request-ID’, ‘openai-organization’: ‘dd-org-nbmhlv’, ‘openai-processing-ms’: ‘7’, ‘openai-version’: ‘2020-10-01’, ‘strict-transport-security’: ‘max-age=15552000; includeSubDomains; preload’, ‘x-ratelimit-limit-requests’: ‘10000’, ‘x-ratelimit-limit-tokens’: ‘30000000’, ‘x-ratelimit-remaining-requests’: ‘9999’, ‘x-ratelimit-remaining-tokens’: ‘29999980’, ‘x-ratelimit-reset-requests’: ‘6ms’, ‘x-ratelimit-reset-tokens’: ‘0s’, ‘x-request-id’: ‘req_d2cd0d452badb366ca68300a049326c5’, ‘CF-Cache-Status’: ‘DYNAMIC’, ‘Set-Cookie’: ‘__cf_bm=WWI0_ct0l8SD2Y__kI6cgxYryca_qxd13CS6j9pX.q4-1724883445-1.0.1.1-RaV0QG06dcVCnIQ2E27LgRJNS8w52a.6cu9KMz3HAicbl2Ukw9DiOgUTS4_H0nhuiMZzFwYZtsVXyv9m5MoU7g; path=/; expires=Wed, 28-Aug-24 22:47:25 GMT; domain=.api.openai.com; HttpOnly; Secure; SameSite=None’, ‘X-Content-Type-Options’: ‘nosniff’, ‘Set-Cookie’: ‘_cfuvid=FPrHPzkiQO3TEluVpWroYemxzH0dihAUjC.StcdW0jI-1724883445207-0.0.1.1-604800000; path=/; domain=.api.openai.com; HttpOnly; Secure; SameSite=None’, ‘Server’: ‘cloudflare’, ‘CF-RAY’: ‘8ba7abdb1ed331bb-LAX’, ‘alt-svc’: ‘h3=“:443”; ma=86400’)>
Code here:
def jsonGptCall(prompt):
url = 'https://api.openai.com/v1/chat/completions'
data = {
"model": "gpt-4o",
"messages": [
{"role": "user", "content": prompt }
],
"response_format": {"type": "json_object"}
}
headers = {
'Authorization': 'Bearer im-hiding-my-api-key',
'Content-Type': 'application/json'
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
completion_data = response.json()
response_content = completion_data['choices'][0]['message']['content']
# print(response_content)
response_json = json.loads(response_content)
return response_json
else:
return {"error": "Failed to fetch response", "status_code": response.status_code}
Again, this has been working for me for at least 2 months now without fail, and stopped working today at some point.