user3709406
user3709406

Reputation: 19

Getting Invalid JSON Error with dynamic values when trying to initialize openai api call

im trying to initilaize this api call with dynamic values in the <>:

``
{
    "model": "gpt-4o-mini",
    "messages": [
        {
            "role": "user",
            "content": <prompt>
        }
    ],
    "temperature": <temperature>
   "max_tokens": <max_tokens>
}

The dynamic values are:

  1. Key: prompt Value: "tell me a joke"
  2. Key: temperature Value: 1
  3. Key: max_tokens Value: 4000

it's coming back with this error:

There was an issue setting up your call.

Raw response for the API Status code 400 { "error": { "message": "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please contact us through our help center at help.openai.com.)", "type": "invalid_request_error", "param": null, "code": null } ``

when i run through json validator it gives this:

Invalid JSON!
Error: Parse error on line 6:
...         "content": <prompt>        } 
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'

I tried using a completely static JSON body (i.e., without any dynamic values) to ensure that the API call works without dynamic inputs and it did.

But can't work with dynamic. What should I do?

Upvotes: 0

Views: 160

Answers (1)

Abdul Mueez
Abdul Mueez

Reputation: 1

You can always use jsonlint or some other service for validating your json payloads.

Upvotes: -1

Related Questions