473 từ
2 phút đọc
Structured Prompting 2026: XML, JSON Schema, Output Types và Chain-of-Thought

Unstructured prompts sinh unstructured output. Structured prompts sinh output predictable, parseable, testable.

Trong production systems, bạn không thể parse free-text responses. Bạn cần guaranteed fields, typed values và machine-readable output. Structured prompting giải quyết việc này — và năm 2026 có 4 patterns chính.


Bốn Patterns#

PatternInput StyleOutput StyleBest For
XML Tags<tag>content</tag>Structured textSystem prompts, tool calls
JSON SchemasJSON definitionJSON outputAPI integration, validation
Output TypesPydantic/Zod modelTyped objectType-safe applications
Chain-of-ThoughtReasoning tracesStep-by-step + answerComplex reasoning

1. XML Tags#

<task>Viết PostgreSQL query tìm users chưa login 90 ngày</task>
<schema>users table: id (UUID), email (text), last_login (timestamp)</schema>
<constraints>
- Dùng parameterized queries
- Return only user_id và email
</constraints>
<output_format>Return ONLY SQL query. Không giải thích.</output_format>

Best Practices#

  • Tag names ngắn, descriptive: <task>, <context>, <output>
  • Nest sparingly — deep nesting confuses models
  • Consistent casing — <tag> every time
  • Đóng mọi tag
  • Để blank lines giữa các tags

2. JSON Schemas#

schema = {
"type": "object",
"properties": {
"severity": {
"type": "string",
"enum": ["critical", "high", "medium", "low"]
},
"root_cause": {"type": "string"},
"action_items": {
"type": "array",
"items": {"type": "string"}
}
},
"required": ["severity", "root_cause", "action_items"]
}

Tại sao JSON schemas thắng trong production:#

  • Validatablejsonschema hoặc pydantic validate output
  • Parseablejson.loads() là xong
  • Type-safe — Boolean là boolean, number là number
  • Auto-retry — Output invalid JSON → frameworks retry với error message
  • API-ready — Pass output trực tiếp vào downstream systems

3. Output Types#

# Pydantic
class CodeReview(BaseModel):
file_path: str
issues: list[dict]
score: int = Field(ge=1, le=10)
verdict: Literal["approve", "changes_requested", "blocked"]
// Zod
const CodeReviewSchema = z.object({
filePath: z.string(),
score: z.number().min(1).max(10),
verdict: z.enum(["approve", "changes_requested", "blocked"]),
});

Output types thắng unstructured nhờ: parse errors ít, type safety, self-documenting, IDE support, testability.


4. Chain-of-Thought#

CoT prompting buộc model reasoning step-by-step trước khi trả lời:

prompt = """Solve step by step, then output JSON.
Problem: Payment $156.78, fee 2.9% + $0.30. Net amount?
Step by step:
1. Percentage fee: $156.78 × 0.029 = $4.55
2. Fixed fee: $0.30
3. Total fee: $4.85
4. Net: $156.78 - $4.85 = $151.93
JSON output:
{
"gross_amount": 156.78,
"net_amount": 151.93,
"total_fee": 4.85
}
"""

Kết Hợp Cả Bốn Patterns#

prompt = """
<task>Phân tích incident deployment dưới đây</task>
<context>Service: payment-api, Time: 02:15 UTC, Error: Connection pool exhaustion</context>
<reasoning>Suy luận step-by-step trước khi trả lời</reasoning>
<output_format>
Return JSON matching schema: { "root_cause": "string", "severity": "critical|high|medium|low", "prevention": ["string"] }
</output_format>
"""
  1. XML tags — Tách context khỏi instructions
  2. Chain-of-thought — Buộc reasoning trước output
  3. JSON schema — Constrain output structure
  4. Pydantic validation — Đảm bảo response dùng được programmatically

Anti-Patterns#

  1. Mix formats — JSON trong XML trong YAML → Pick one format
  2. Over-specifying — 20+ fields → Giữ schema dưới 8 fields
  3. No fallback for parse failures — Luôn có try/except + regex fallback

Production Checklist#

  • Output format specified BEFORE user input
  • JSON schemas include field descriptions
  • Schemas validated with Pydantic/Zod pre-deployment
  • Parse failures có fallback strategy
  • CoT tách biệt khỏi final output
  • XML tags consistent (case, nesting, closing)
  • Schema complexity matches task complexity

Tiếp Theo#

BàiChủ đề
1System Prompts vs Steering Files vs Agent Instructions
2MCP Tools as Prompts
3Structured Prompting (bài này)
4Prompt Testing & Evaluation
5Production Patterns & Anti-Patterns

Series: Prompt Engineering 2026 — Production Patterns. Bài 3: Structured Prompting.

Advertisement

Structured Prompting 2026: XML, JSON Schema, Output Types và Chain-of-Thought
https://minixium.com/vi/posts/structured-prompting-2026-xml-json-schemas-output-types-chain-of-thought-vi/
Tác giả
Minixium
Đăng vào lúc
2026-05-30
Giấy phép bản quyền
CC BY-NC-SA 4.0

Advertisement