GPT-4o function calling returns malformed JSON intermittently

Alex Petrov
Alex PetrovJun 15, 2023

About 3-5% of my function calling requests to gpt-4o return malformed JSON in the arguments field. This is causing production issues.

Example of a broken response:

{
  "tool_calls": [{
    "id": "call_abc123",
    "function": {
      "name": "get_weather",
      "arguments": "{\"location\": \"San Francisco\", \"unit\":"
    }
  }]
}

The JSON is truncated. This happens with both simple and complex function schemas. I'm using gpt-4o-2024-05-13.

Has anyone else noticed this? My workaround is to retry on JSON parse failure, but this increases latency and cost.

Edit: Structured Outputs fixed this completely. See accepted answer below.

8.7k views45 replies112 likesSolved

Log in to reply to this topic.