JSON prompting dominates APIs, but many production stacks—especially Claude-style prompting and document pipelines—prefer XML prompting: explicit open/close tags that delimit instructions, context, and outputs. Tags reduce ambiguity when multiple sections share one message.
XML sits between raw prose and JSON: more structured than Markdown headings alone, and often clearer than nested JSON when humans edit prompt templates.
Learning Objectives
By the end of this lesson, students should be able to:
- Use XML-like tags to separate instructions, context, and examples.
- Request model outputs wrapped in named tags for easy extraction.
- Contrast XML vs. JSON for machine parsing and human editing.
- Avoid malformed nesting and unescaped special characters.
- Combine tagged inputs with system and user roles.
- Parse tagged regions with regex or a lightweight XML parser.
XML prompting is the use of XML-style (or XML-like) tags in prompts and responses to delimit logical sections—such as <instructions>, <context>, <examples>, and <answer>—improving clarity for both the model and downstream extractors.
Why Tags Help Models
Name each section.
Keep policy vs. data apart.
Pull the answer tag.
Check required tags exist.
| Tag role | Example | Purpose |
|---|---|---|
| Policy | <instructions> | Stable rules |
| Evidence | <document> | Retrieved or user text |
| Demo | <example> | Few-shot pairs |
| Output | <result> | Parse target |
Prefer XML when
- Multi-section prompts
- Humans edit templates
- Long mixed context
Prefer JSON when
- Typed fields for code
- API / agent tools
- Schema validation
Hybrid
- XML input sections
- JSON inside
<result> - Best of both
Practical Tagged Prompt
Strengths
- Clear section boundaries
- Readable prompt templates
- Simple tag extraction
Tradeoffs
- Not a full XML schema by itself
- User text may contain
<chars - Weaker type checking than JSON Schema
“XML prompting requires a validating XML parser and DTD.” In LLM practice, tags are often XML-like delimiters, not a formally validated document. Still escape or wrap untrusted user content carefully so injected </document> cannot close your sections early—a guardrail concern overlapping prompt injection.
Knowledge Check
- Short Answer: What is XML prompting? Answer: Using XML-style tags to delimit prompt/response sections.
- True/False: Tags help separate instructions from documents. Answer: True.
- Multiple Choice: A good output tag purpose is: (a) easy extraction, (b) GPU scheduling, (c) tokenization training. Answer: (a).
- Short Answer: Name one risk of raw user text inside tags. Answer: Injected closing tags / prompt injection.
- True/False: JSON is usually better for typed API fields. Answer: True.
- Multiple Choice: Hybrid pattern often puts: (a) JSON inside a result tag, (b) CNNs inside softmax, (c) CSS inside CUDA. Answer: (a).
- Short Answer: Why label
<instructions>separately? Answer: Keeps policy stable and distinct from variable evidence. - Short Answer: How might you extract
<summary>? Answer: Regex or an XML/HTML parser on the completion. - Multiple Choice: Prefer XML tags when: (a) multi-section human-edited prompts, (b) only binary blobs, (c) matrix multiply. Answer: (a).
- True/False: Sibling Markdown formatting is often enough for human-facing answers. Answer: True.
Key Takeaways
- XML-like tags structure complex prompts and outputs.
- Isolate instructions, documents, and answer regions.
- Watch for tag injection; consider JSON hybrids for types.
- Next: Markdown Formatting.
Hands-on: Convert a flat prompt into tagged sections; measure answer-tag hit rate.
Discussion: Should untrusted documents be base64-wrapped or CDATA-like escaped inside tags?
Recap: XML prompting labels boundaries so models and parsers agree. Continue with Markdown Formatting.