# Invalid JSON response when using Structured Output

**URL:** <https://community.openai.com/t/invalid-json-response-when-using-structured-output/1121650>\
**Category:** Bugs\
**Tags:** json, json-mode\
**Created:** [February 15, 2025, 7:47pm UTC](https://community.openai.com/t/invalid-json-response-when-using-structured-output/1121650 "2025-02-15T19:47:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![CycleMost](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/cyclemost/32/552979_2.png) [@CycleMost](https://community.openai.com/u/CycleMost)\
**Post date:** [February 15, 2025, 7:47pm UTC](https://community.openai.com/t/invalid-json-response-when-using-structured-output/1121650/1 "2025-02-15T19:47:10Z")

</div>

I’m using the assistant API to run a thread and get a response back in JSON format, using a specified schema (json\_schema format). In some cases (maybe 1% of the time, and not reproducible except randomly), the response contains invalid JSON. For example, here is a simplified version of the response that contains invalid json:

```auto
{
  "summary": "blah blah",
  "details": [
  ]
},
"notes": "blah blah"
}

```

There is an extra } which is causing the issue. The response should have been:

```auto
{
  "summary": "blah blah",
  "details": [
  ],
  "notes": "blah blah"
}

```

… and I am using `strict: true` in my schema definitions.

Has anyone else experienced something similar? I assumed that when using the json\_schema output type, the output would always be valid JSON - not just 99% of the time.

---

<div class="post-metadata">

**Author:** ![dal1e](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/dal1e/32/450761_2.png) [@dal1e](https://community.openai.com/u/dal1e)\
**Post date:** [February 15, 2025, 8:39pm UTC](https://community.openai.com/t/invalid-json-response-when-using-structured-output/1121650/2 "2025-02-15T20:39:26Z")

</div>

Yes 8 from the 10871 calls made, using the `https://api.openai.com/v1/chat/completions` API endpoint. To be a bit more specific, the invalid JSON was the stringified JSON data within the `choices[0].message.content` property

The workaround for me is to check if the stringified JSON data returned is actually parsable, if not do a retry.

```JSON
"choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "{ \"date\" : [ <RANDOM CUTOFF> }"

```

---

<div class="post-metadata">

**Author:** ![CycleMost](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/cyclemost/32/552979_2.png) [@CycleMost](https://community.openai.com/u/CycleMost)\
**Post date:** [February 16, 2025, 12:29am UTC](https://community.openai.com/t/invalid-json-response-when-using-structured-output/1121650/3 "2025-02-16T00:29:54Z")

</div>

Thank you. I was going to implement a similar retry strategy. Also, looking at the [documentation](https://platform.openai.com/docs/guides/structured-outputs) for structured outputs, it says this:

> Structured Outputs can still contain mistakes. If you see mistakes, try adjusting your instructions, [etc…]

---

<div class="post-metadata">

**Author:** ![sps](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/sps/32/7736_2.png) [@sps](https://community.openai.com/u/sps)\
**Post date:** [February 16, 2025, 6:40am UTC](https://community.openai.com/t/invalid-json-response-when-using-structured-output/1121650/4 "2025-02-16T06:40:42Z")

</div>

@CycleMost @dal1e

Are you prompting the model in the system message to respond with a valid JSON object?

---

<div class="post-metadata">

**Author:** ![dal1e](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/dal1e/32/450761_2.png) [@dal1e](https://community.openai.com/u/dal1e)\
**Post date:** [February 16, 2025, 6:23pm UTC](https://community.openai.com/t/invalid-json-response-when-using-structured-output/1121650/5 "2025-02-16T18:23:23Z")

</div>

@sps Currently not, but it is a trivial to add. But as I mentioned, only 8 of the 10871 calls, that is statistically reasonable, one could argue that it aligns with the expected rare event probability. Currently my system prompt - among other instructions only - instructs to stick to the schema provided.

```auto
[...]
- Strictly adhere to the described <XYZ> schema in the response.
[...]
```
