JSON Schema Generation Occasionally Returns Invalid JSON Output #13437

Closed
opened 2026-02-21 19:12:09 -05:00 by yindo · 1 comment
Owner

Originally created by @QuantumGhost on GitHub (Apr 27, 2025).

Although we have explicitly configured
the models to avoid generating markdown code blocks when outputting JSON schemas,
certain models (such as Google Gemini 2.0 Flash) still produce JSON with leading backticks.
This issue prevents the client from parsing the output JSON correctly, resulting in a broken UI.

Image

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

1.3.0

Cloud or Self Hosted

Cloud

Steps to reproduce

  1. Use the structured output feature to generate a JSON schema.
  2. Employ the following prompt with the Google Gemini 2.0 Flash model.
Design a JSON schema for user profile, including the following fields:

- id
- username
- gender
- nickname

✔️ Expected Behavior

Expected output:

a valid JSON string

{"output": "\n{\n  \"type\": \"object\",\n  \"properties\": {\n    \"id\": {\n      \"type\": \"string\"\n    },\n    \"username\": {\n      \"type\": \"string\"\n    },\n    \"gender\": {\n      \"type\": \"string\",\n      \"enum\": [\"male\", \"female\", \"other\"]\n    },\n    \"nickname\": {\n      \"type\": \"string\"\n    }\n  },\n  \"required\": [\"id\", \"username\", \"gender\", \"nickname\"]\n}\n", "error": ""}

Actual Behavior

an invalid JSON string with backtiles:

{"output": "```json\n{\n  \"type\": \"object\",\n  \"properties\": {\n    \"id\": {\n      \"type\": \"string\"\n    },\n    \"username\": {\n      \"type\": \"string\"\n    },\n    \"gender\": {\n      \"type\": \"string\",\n      \"enum\": [\"male\", \"female\", \"other\"]\n    },\n    \"nickname\": {\n      \"type\": \"string\"\n    }\n  },\n  \"required\": [\"id\", \"username\", \"gender\", \"nickname\"]\n}\n```", "error": ""}
Originally created by @QuantumGhost on GitHub (Apr 27, 2025). Although [we have explicitly configured](https://github.com/langgenius/dify/blob/2266001/api/core/llm_generator/prompts.py#L243) the models to avoid generating markdown code blocks when outputting JSON schemas, certain models (such as Google Gemini 2.0 Flash) still produce JSON with leading backticks. This issue prevents the client from parsing the output JSON correctly, resulting in a broken UI. <img width="1214" alt="Image" src="https://github.com/user-attachments/assets/03dbb78a-7aba-4cf3-bc64-88fe09e44721" /> ### Self Checks - [x] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [x] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [x] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [x] Please do not modify this template :) and fill in all the required fields. ### Dify version 1.3.0 ### Cloud or Self Hosted Cloud ### Steps to reproduce 1. Use the structured output feature to generate a JSON schema. 2. Employ the following prompt with the Google Gemini 2.0 Flash model. ```text Design a JSON schema for user profile, including the following fields: - id - username - gender - nickname ``` ### ✔️ Expected Behavior Expected output: a valid JSON string ```text {"output": "\n{\n \"type\": \"object\",\n \"properties\": {\n \"id\": {\n \"type\": \"string\"\n },\n \"username\": {\n \"type\": \"string\"\n },\n \"gender\": {\n \"type\": \"string\",\n \"enum\": [\"male\", \"female\", \"other\"]\n },\n \"nickname\": {\n \"type\": \"string\"\n }\n },\n \"required\": [\"id\", \"username\", \"gender\", \"nickname\"]\n}\n", "error": ""} ``` ### ❌ Actual Behavior an invalid JSON string with backtiles: ```text {"output": "```json\n{\n \"type\": \"object\",\n \"properties\": {\n \"id\": {\n \"type\": \"string\"\n },\n \"username\": {\n \"type\": \"string\"\n },\n \"gender\": {\n \"type\": \"string\",\n \"enum\": [\"male\", \"female\", \"other\"]\n },\n \"nickname\": {\n \"type\": \"string\"\n }\n },\n \"required\": [\"id\", \"username\", \"gender\", \"nickname\"]\n}\n```", "error": ""}
yindo added the 🐞 buggood first issuecloud labels 2026-02-21 19:12:09 -05:00
yindo closed this issue 2026-02-21 19:12:09 -05:00
Author
Owner

@QuantumGhost commented on GitHub (Apr 27, 2025):

Proposed Mitigation:

  1. Parse the JSON string returned by the models before sending it to the client.
  2. If the JSON is invalid, attempt to fix it using json_repair, then retry parsing.
  3. If the repair is successful and the repaired JSON can be parsed, return the repaired JSON to the client.
  4. If parsing still fails after the repair—indicating the JSON is severely corrupted—return an error to the client with a more meaningful error message to inform the user appropriately.
@QuantumGhost commented on GitHub (Apr 27, 2025): Proposed Mitigation: 1. Parse the JSON string returned by the models before sending it to the client. 2. If the JSON is invalid, attempt to fix it using `json_repair`, then retry parsing. 3. If the repair is successful and the repaired JSON can be parsed, return the repaired JSON to the client. 4. If parsing still fails after the repair—indicating the JSON is severely corrupted—return an error to the client with a more meaningful error message to inform the user appropriately.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#13437