Rules for configuring DeepSeek to produce structured JSON output reliably, with schema validation and error handling patterns.
## Structured JSON Output Rules for DeepSeek
### System Configuration
When you need DeepSeek to output structured data, follow these rules:
### Prompt Structure
1. Define the exact JSON schema in the prompt
2. Include a minimal example of the expected output
3. State explicitly: "Output ONLY valid JSON. No markdown, no explanation, no code blocks."
4. For DeepSeek V3: Use the JSON mode API parameter when available
### Schema Definition Template
```
Respond with a JSON object matching this exact schema:
{
"field_name": "string - description of this field",
"numeric_field": 0,
"array_field": ["item description"],
"nested": {
"sub_field": "description"
}
}
```
### Error Prevention
- Always include field descriptions to reduce hallucination
- Use enum values where possible: "status": "one of: active, inactive, pending"
- Specify number formats: "price": "number, two decimal places, USD"
- For arrays, specify min/max length: "tags": "array of 3-5 strings"
- Include "required" vs "optional" for each field
### Validation Pattern
After receiving output:
1. Parse with try/catch
2. Validate against schema
3. Check for null/undefined required fields
4. Verify enum values are within allowed set
5. Retry with error message if validation fails
### Common Pitfalls
- R1 may wrap JSON in thinking tags — strip <think>...</think> before parsing
- Long outputs may get truncated — set max_tokens appropriately
- Nested objects beyond 3 levels tend to have more errorsWorkflows from the Neura Market marketplace related to this DeepSeek resource