JSON Syntax Error with GPT-Generated API Call

I’ve encountered a problem while using GPT to generate content for API calls. Specifically, the API call succeeds when the GPT model outputs actual values to be passed to the API. However, when the model generates Python objects to be serialized to JSON, it results in a syntax error.

Here’s a step-by-step description of the issue:

  1. I used GPT to create content for an API call, which required passing a dictionary of data.
  2. The first time, GPT provided actual values (e.g., {"total_sales": 1000, "number_of_orders": 50}), which the API accepted without issues.
  3. On a subsequent attempt, GPT outputted Python objects (e.g., {"x": values, "label": labels} where values and labels are Python lists), which resulted in a JSON serialization error.

It seems there is an inconsistency in how GPT outputs data for API calls—literal values work fine, but Python object references do not serialize properly into JSON format.

I am looking to understand:

  • Why there is a difference in the output that GPT provides for seemingly similar tasks.
  • How to ensure that GPT consistently provides JSON-serializable output for API calls.

Has anyone else faced this issue, or is there a known workaround or solution?

Hi!

Have you tried adding

Don’t forget to turn it into a proper JSON string/object/we!

to your instruction?

. It still gives json error when ever the generated parameter involves a python object. This was the generated error.

The error in the last attempt to create the bar chart was an ApiSyntaxError related to JSON parsing. Specifically, the error message indicated: “Could not parse API call kwargs as JSON: exception=Expecting value: line 1 column 1211 (char 1210).” This suggests there was an issue with the JSON formatting or the structure of the data being passed to the API