← Master Index
Vol. 13 Module 13.1 Lecture

XML Prompting

Prompting Techniques

How This Lesson Fits the Module & Volume

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.
Definition

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

Label

Name each section.

Isolate

Keep policy vs. data apart.

Extract

Pull the answer tag.

Validate

Check required tags exist.

Tag roleExamplePurpose
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

prompt = f""" Summarize the document in 3 bullets. Cite only facts present in . Put the final summary inside tags. Do not include preamble outside those tags. {retrieved_text} """ # Extraction after the completion: import re m = re.search(r"(.*?)", completion, flags=re.S | re.I) if not m: raise ValueError("missing tag") summary = m.group(1).strip() # Optional hybrid: ask for JSON inside the tag # {"bullets": ["...", "...", "..."]}

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
Common Misconception

“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

  1. Short Answer: What is XML prompting? Answer: Using XML-style tags to delimit prompt/response sections.
  2. True/False: Tags help separate instructions from documents. Answer: True.
  3. Multiple Choice: A good output tag purpose is: (a) easy extraction, (b) GPU scheduling, (c) tokenization training. Answer: (a).
  4. Short Answer: Name one risk of raw user text inside tags. Answer: Injected closing tags / prompt injection.
  5. True/False: JSON is usually better for typed API fields. Answer: True.
  6. Multiple Choice: Hybrid pattern often puts: (a) JSON inside a result tag, (b) CNNs inside softmax, (c) CSS inside CUDA. Answer: (a).
  7. Short Answer: Why label <instructions> separately? Answer: Keeps policy stable and distinct from variable evidence.
  8. Short Answer: How might you extract <summary>? Answer: Regex or an XML/HTML parser on the completion.
  9. Multiple Choice: Prefer XML tags when: (a) multi-section human-edited prompts, (b) only binary blobs, (c) matrix multiply. Answer: (a).
  10. 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.
Trainer’s Guide

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.