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 errorsSystem rules for designing inter-service communication in microservices architectures with DeepSeek Coder, covering sync/async patterns, error handling, and resilience.
System rules for generating content in multiple languages with DeepSeek V3, covering translation quality, cultural adaptation, locale-specific formatting, and quality assurance.
System rules for safe code refactoring with DeepSeek R1, requiring test coverage verification, incremental changes, and behavior preservation checks.
System rules for using DeepSeek V3 to generate clear, maintainable technical documentation including API docs, architecture docs, and onboarding guides.
System rules for DeepSeek Coder to generate optimized database queries, with requirements for EXPLAIN analysis, indexing recommendations, and performance targets.
System rules for using DeepSeek V3 to generate infrastructure code, CI/CD pipelines, and operational runbooks with security and reliability best practices.