How to optimize JSON schema for consistent structured output in DeepSeek-V3
The biggest mistake I see is using generic field names like data or info and expecting the model to infer the format from the prompt. DeepSeek-V3 tends to hallucinate nested arrays or wrap objects in unnecessary parent keys if the schema isn't airtight.
To get 100% consistency, you have to move away from "suggestive" schemas and move toward "restrictive" ones. Here is the benchmark of what actually works:
The "Loose" Approach (High failure rate)
If you just provide a basic JSON object description in the system prompt, V3 occasionally adds markdown formatting (
...) even when you explicitly ask for raw JSON, or it might flip a boolean to a string ("true" instead of true).The "Strict" Approach (High consistency)
The key is using an explicit JSON Schema (Draft 7 style) and leveraging the enum property for every single field that has a finite set of possibilities. DeepSeek-V3 performs significantly better when it has a closed list to choose from rather than generating free-form text.
For example, instead of:
{
"sentiment": "string"
}Use this:{
"type": "object",
"properties": {
"sentiment": {
"type": "string",
"enum": ["positive", "negative", "neutral"]
}
},
"required": ["sentiment"]
}Another performance quirk I found: DeepSeek-V3 struggles with deeply nested objects (4+ levels deep). When the nesting gets too complex, the model occasionally forgets to close a bracket or misses a required field in the innermost object. If you're hitting this, flatten your schema. I've found that flattening a 4-level hierarchy into 2 levels increased my successful parse rate from 88% to 99.4% across 1,000 test samples.
If you are using the API, avoid putting the schema in the user prompt. Put the schema and the "output only JSON" instruction in the system prompt, and use the user prompt solely for the raw data.
Here is the prompt structure that gave me the most stable results:
System: You are a data extraction engine. Output valid JSON that adheres strictly to this schema: [Insert JSON Schema]. Do not include any preamble or markdown formatting.
User: Extract the following: [Insert Text]Comparing this to Claude 3.5, Claude is more "intelligent" about fixing its own schema errors on the fly, but DeepSeek-V3 is more predictable once the schema is optimized. The trade-off is that V3 requires more upfront engineering of the schema, whereas Claude can often "guess" what you want from a messy schema.
Performance Summary:
GPT-4o: Best "out of the box" reliability, handles messy schemas well, most expensive.
Claude 3.5 Sonnet: Great at complex nesting, very high reasoning, mid-price.
DeepSeek-V3: Extreme efficiency and speed, requires strict enum and flat schemas for 100% reliability, lowest cost.
All Replies (0)
No replies yet — be the first!
