JSON Formatted Response API Call All The Sudden Broken

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.

Huh, that’s odd.

Can you copy the exact JSON object that’s being sent? I usually debug bad request errors by looking at the JSON object produced after the code executes.

It’s possible they changed the schema or url endpoint recently. All we need to do is compare the current output to an output that returns a 200.

2 Likes

{‘model’: ‘gpt-4o’, ‘messages’: [{‘role’: ‘user’, ‘content’: ‘Hello this is a test’}], ‘response_format’: {‘type’: ‘json_object’}}

Here is the printed “data” variable that is being passed into the posted request in the “json” field.

Thanks for the response by the way. Im curious if this code works on your machine with your own OpenAI key?

You are missing part of the error message.

else:
        return {"error": "Failed to fetch response", "status_code": response.status_code}

This is a big no-no. There is so much more to an error that you should be reading.
If you were to read the error message text you would see a message telling you why your request is now failing.

You need to explicitly ask for JSON in your response.

2 Likes