This prompt is designed for a specific job: converting a flat text representation of a document's outline into a structured, tree-based JSON object. The ideal user is a document intelligence engineer or a legal/technical documentation specialist who already has extracted text from a PDF, DOCX, or similar format and needs to reconstruct the logical section hierarchy. The core value is turning a linear sequence of headings like '1. Introduction', '1.1 Scope', '2. Methods' into a navigable parent-child structure that preserves numbering schemes, heading levels, and cross-reference targets. This structured output is essential for building search indexes, document viewers with collapsible navigation, or compliance review systems where the relationship between a clause and its sub-clauses carries legal or technical meaning.
Prompt
Nested Hierarchy Extraction Prompt for Document Outlines

When to Use This Prompt
Determining the right job for nested hierarchy extraction and when a simpler approach or full document layout analysis is required.
You should use this prompt when the input text contains a clear, distinguishable heading structure through numbering (e.g., '3.2.1'), consistent indentation, or explicit markup like Markdown headers (#, ##). The prompt assumes that heading levels are inferable from these textual cues alone; it does not perform layout analysis on a rendered page. A concrete implementation would involve feeding the raw text of a contract's table of contents or a technical specification's outline into the prompt, with a constraint that the output must conform to a specific JSON schema where each node has id, title, level, number, and children fields. The prompt is not suitable for documents where headings are only visually distinct (e.g., bold and centered text without numbering) unless that visual formatting has been converted into explicit text markers during the extraction phase.
Avoid using this prompt when the primary challenge is page layout analysis, such as detecting columns, associating captions with figures, or extracting tables. For those tasks, use a dedicated layout extraction or table extraction prompt. Similarly, if you only need a flat list of section titles without parent-child relationships, a simpler extraction prompt will be more cost-effective and less prone to hallucinating a hierarchy where none exists. Before deploying this prompt, you must implement a validation harness that checks for common structural failures: broken numbering sequences (e.g., jumping from '2.1' to '2.3'), orphaned subheadings that lack a parent, and circular references in the output tree. For high-stakes legal or compliance workflows, always include a human review step to verify the extracted hierarchy against the original document before it is used in a production system.
Use Case Fit
Where the Nested Hierarchy Extraction Prompt works well and where it introduces unacceptable risk. Use these cards to decide whether this prompt fits your document pipeline before you invest in eval harnesses.
Good Fit: Legal and Regulatory Documents
Use when: documents have explicit numbering schemes (e.g., 1.1, (a), (i)) and strict parent-child section relationships. Contracts, statutes, and compliance filings are ideal. Guardrail: validate that every extracted child node has a parent reference and that numbering sequences are contiguous.
Good Fit: Technical Documentation with Heading Hierarchies
Use when: source material uses consistent heading levels (H1, H2, H3) with predictable nesting. API specs, architecture docs, and engineering manuals benefit. Guardrail: cross-check extracted hierarchy depth against the document's actual heading styles to catch level-skipping errors.
Bad Fit: Visually Implied Structure Without Markup
Avoid when: hierarchy is conveyed only through font size, indentation, or whitespace without semantic markup or numbering. PDFs with purely visual formatting will produce unreliable trees. Guardrail: require explicit numbering or tagged heading styles; escalate visually-structured documents to layout-aware extraction pipelines.
Bad Fit: Flat or Single-Level Content
Avoid when: the document has no meaningful hierarchy—single-level lists, continuous prose, or flat FAQ pages. The prompt will hallucinate structure or return trivial single-node trees. Guardrail: pre-check document structure depth before invoking the prompt; skip if fewer than two heading levels are detected.
Required Inputs: Cleaned Text with Numbering Preserved
Risk: OCR noise, stripped numbering, or merged paragraphs destroy the extraction signal. Guardrail: preprocess documents to preserve original numbering strings, remove header/footer artifacts, and verify that section numbers survived text extraction before calling the prompt.
Operational Risk: Broken Numbering Sequences
Risk: missing or misread section numbers cause orphaned subheadings, incorrect parent assignments, and corrupted tree structures that break downstream navigation. Guardrail: run post-extraction validation that checks for gaps in numbering sequences, orphaned nodes, and depth inconsistencies before ingestion.
Copy-Ready Prompt Template
A copy-ready prompt template for extracting nested document hierarchies into tree-structured JSON with numbering preservation and cross-reference detection.
This prompt template is designed to extract the full section hierarchy from a document outline, preserving the original numbering scheme, heading levels, and parent-child relationships. It produces a tree-structured JSON object where each node represents a section with its heading, level, numbering, children, and any cross-references detected within the section body. The template is built for legal and technical documentation teams who need reliable structure extraction before downstream processing, indexing, or navigation tree generation.
textYou are a document structure extraction system. Your task is to extract the nested section hierarchy from the provided document text and return a tree-structured JSON object. ## INPUT [INPUT] ## OUTPUT SCHEMA Return a JSON object with the following structure: { "document_title": "string or null", "sections": [ { "id": "unique-section-id", "heading": "Section heading text exactly as written", "level": integer (1 for top-level, 2 for sub-section, etc.), "numbering": "original numbering string, e.g., '1.2.3' or 'Article IV'", "page_reference": "page or location indicator if present, else null", "body_summary": "brief summary of section content if body text is present, else null", "cross_references": [ { "target": "referenced section number or identifier", "reference_type": "see|refers_to|amends|subject_to|notwithstanding|other", "context": "surrounding sentence fragment containing the reference" } ], "children": [] } ], "warnings": [] } ## CONSTRAINTS [CONSTRAINTS] ## INSTRUCTIONS 1. Preserve the original numbering scheme exactly as it appears in the text. Do not renumber or normalize. 2. Determine heading levels based on numbering depth, indentation, and formatting cues in the text. If a section has no children, its children array must be empty. 3. Detect cross-references within section body text, including phrases like "see Section X", "as defined in Y", "subject to Z", "notwithstanding A", and similar reference language. 4. If a section number appears to skip a level (e.g., jumping from 1.1 to 1.3 without 1.2), flag it in the warnings array at the root level with the specific sections involved. 5. If a subheading appears without a parent section that matches its numbering prefix, flag it as an orphaned subheading in the warnings array. 6. Do not invent sections that are not present in the text. Only extract what is explicitly provided. 7. If the document has no discernible hierarchy, return a single section with the full text as the heading and an empty children array. ## EXAMPLES [EXAMPLES] ## OUTPUT Return only the JSON object. No markdown fences, no commentary.
To adapt this template, replace the square-bracket placeholders with your specific requirements. The [INPUT] placeholder should contain the full document text or outline to be processed. The [CONSTRAINTS] placeholder allows you to add domain-specific rules, such as maximum nesting depth, specific numbering patterns to recognize (e.g., Roman numerals for legal documents), or custom cross-reference patterns relevant to your document type. The [EXAMPLES] placeholder should include one or two few-shot examples showing the expected output for sample inputs, which significantly improves accuracy for unusual numbering schemes or deeply nested structures.
Before deploying this prompt into a production pipeline, implement validation checks on the output JSON. Verify that every child section's numbering prefix matches its parent's numbering, that no sections are duplicated, and that the warnings array captures real anomalies rather than false positives. For high-stakes document processing—such as legal contracts or regulatory filings—route outputs with non-empty warnings arrays to human review. Test the prompt against documents with known broken numbering sequences, orphaned subheadings, and cross-reference chains to ensure the extraction logic handles edge cases gracefully rather than silently producing incorrect hierarchies.
Prompt Variables
Required and optional inputs for the Nested Hierarchy Extraction Prompt. Each placeholder must be populated before the prompt is sent to the model. Validation notes describe how to check that the input is well-formed before execution.
| Placeholder | Purpose | Example | Validation Notes |
|---|---|---|---|
[DOCUMENT_TEXT] | Full text of the document from which the outline hierarchy will be extracted |
| Must be non-empty string. Strip binary artifacts and control characters. Truncate to model context window minus 2000 tokens for prompt overhead. |
[HEADING_PATTERNS] | Regex or string patterns that identify heading lines in the document | ^(\d+.?)+\s+[A-Z]|^[IVX]+.\s+|^Article\s+\d+|^Section\s+\d+ | Must be valid regex or explicit string list. Test against sample document lines before prompt assembly. Empty array means model must infer patterns. |
[NUMBERING_SCHEME] | Description of the numbering convention used in the document | Decimal-dotted: 1, 1.1, 1.1.1\nRoman: I, II, III\nAlphanumeric: (a), (b), (c) | Must be one of enumerated values or a clear natural-language description. Null allowed if scheme is unknown and model should detect it. |
[MAX_DEPTH] | Maximum nesting depth to extract | 6 | Integer between 1 and 10. Depths beyond 6 increase hallucination risk. Default to 6 if not specified. |
[OUTPUT_SCHEMA] | JSON schema or structure description for the output tree | {"type":"object","properties":{"sections":{"type":"array","items":{"$ref":"#/definitions/section"}}}} | Must be valid JSON Schema or explicit field list. Validate with schema parser before prompt assembly. Required for downstream ingestion. |
[INCLUDE_CROSS_REFERENCES] | Whether to extract internal cross-references between sections | Boolean. When true, output must include refs array per section. Increases token usage. Set false for simple outline extraction only. | |
[PAGE_BOUNDARIES] | Optional page break markers to anchor sections to page ranges | [{"page":1,"offset":0},{"page":2,"offset":2450}] | Array of page boundary objects or null. When provided, sections must include start_page and end_page fields. Validate array length matches document page count. |
[ORPHAN_THRESHOLD] | Minimum content length in characters for a section to be considered valid | 50 | Integer. Sections with body text shorter than this threshold are flagged as potential orphans. Set to 0 to disable orphan detection. Default 50. |
Implementation Harness Notes
How to wire the hierarchy extraction prompt into a production document processing pipeline with validation, retries, and human review.
The hierarchy extraction prompt is designed to be called after text extraction and before downstream indexing or navigation systems. In a production pipeline, the raw text from a PDF parser or OCR engine is fed into the prompt, and the resulting JSON tree must be validated before it can be trusted by a table of contents generator, a document viewer sidebar, or a search index. The prompt alone is not enough—you need a harness that catches malformed JSON, schema violations, and logically inconsistent hierarchies before they corrupt downstream systems.
Wrap the model call in a validation layer that checks the output JSON against the expected schema. The schema should enforce the presence of required fields like sections, heading_level, numbering, and children, and it should verify that heading_level values are integers that increment or decrement by no more than one level at a time. For production deployments, implement a retry loop with a maximum of 3 attempts if the output fails JSON parsing or schema validation. On the second and third retries, append the specific validation error to the prompt as additional context—for example, 'Previous output failed validation: heading_level jumped from 2 to 4 without a level-3 parent.' This gives the model a concrete signal to self-correct rather than guessing. Log every extraction attempt with the document identifier, model version, prompt version, and validation result for audit trails. If the warnings array in the output contains broken numbering sequences or orphaned subheadings, route the document to a human review queue rather than silently ingesting potentially corrupted hierarchy data.
For documents exceeding the model context window, split the document at major section boundaries—ideally at heading level 1 or 2 transitions—process each part independently, and merge the resulting trees by matching section numbering at the split points. This merge step requires a reconciliation function that aligns overlapping sections and resolves numbering conflicts. Store the extracted hierarchy alongside the source document with a timestamp and model version for traceability. When choosing a model, prefer one with strong JSON mode and long-context support; smaller models may struggle with deeply nested legal or technical outlines. Avoid deploying this prompt without a human review queue for documents flagged with structural warnings—silent ingestion of broken hierarchies will degrade search relevance and navigation accuracy over time.
Expected Output Contract
Fields, format, and validation rules for the extracted hierarchy JSON. Use this contract to build a parser or validator before integrating the prompt into a production pipeline.
| Field or Element | Type or Format | Required | Validation Rule |
|---|---|---|---|
hierarchy | Array of objects | Root array must contain at least one node. Schema check: type is array, length > 0. | |
hierarchy[].id | String | Must be unique within the document. Parse check: no duplicate IDs across all nodes. | |
hierarchy[].title | String | Must not be empty or whitespace-only. Length > 0 after trimming. | |
hierarchy[].level | Integer | Must be a positive integer >= 1. Heading level must not skip more than one level from its parent (e.g., H1 to H3 is a failure signal). | |
hierarchy[].numbering | String or null | If present, must match a valid numbering pattern (e.g., '1.2.3', 'A.1', 'IV'). Null allowed when no numbering is detected. Regex check against [DOCUMENT_NUMBERING_PATTERN]. | |
hierarchy[].children | Array of objects | Must be an array. Empty array allowed for leaf nodes. Schema check: type is array. | |
hierarchy[].page_start | Integer or null | If present, must be a positive integer. Null allowed when page information is unavailable. Value must not exceed [DOCUMENT_PAGE_COUNT]. | |
hierarchy[].cross_references | Array of strings or null | If present, each string must match a valid section ID or numbering pattern within the document. Null allowed. Parse check: no broken internal references. |
Common Failure Modes
What breaks first when extracting nested hierarchies from documents and how to guard against it.
Broken Numbering Sequences
What to watch: The model outputs a hierarchy where section 3.1 is followed by 3.3 with no 3.2, or numbering restarts mid-document. This happens when the model skips over visually ambiguous markers or misreads OCR'd digits. Guardrail: Add a post-extraction validator that checks for gaps in sequential numbering and flags any parent whose children don't form a contiguous sequence. Re-prompt with the specific gap location when detected.
Orphaned Subheadings
What to watch: A subheading is extracted but assigned to the wrong parent section, or worse, assigned no parent at all and left floating at the root level. This is common when heading indentation is ambiguous or when a document uses inconsistent formatting for the same heading level. Guardrail: Validate that every extracted node has exactly one parent (except the root). Reject outputs with orphaned nodes and re-prompt with explicit parent-child linking instructions and the orphaned heading text.
Heading Level Misclassification
What to watch: An H2 is extracted as an H3 because the model relies on font size alone, or a bold body-text line is promoted to a heading. This flattens or distorts the intended document structure. Guardrail: Provide explicit multi-signal criteria in the prompt (font size, numbering pattern, bold, indentation, and context). Add a schema check that rejects trees where a single level contains wildly inconsistent numbering styles.
Cross-Reference Target Drift
What to watch: The model extracts a cross-reference like "see Section 4.2" but links it to Section 4.3 or fails to resolve the target entirely. This breaks navigability for downstream systems. Guardrail: Post-process the output to verify that every extracted cross-reference target actually exists in the hierarchy. For unresolved references, add a target_not_found flag rather than guessing. Re-prompt for resolution only when confidence is high.
Page-Boundary Heading Splitting
What to watch: A heading that spans a page break is extracted as two separate headings, or the heading text is concatenated with body text from the previous page. This creates phantom sections and corrupts the tree. Guardrail: Include page-boundary awareness in the prompt instructions. Validate that no heading text appears truncated mid-word. When a heading sits at the top of a page, check the previous page's last line for continuation context.
Silent Section Collapse
What to watch: The model merges two adjacent sections with similar-sounding titles into one, or collapses a parent section that has no body text between it and its first child. This loses structural information that matters for navigation and search. Guardrail: Compare the count of extracted headings against a simple regex-based heading count from the source text. If the counts diverge by more than a threshold, flag the output for human review or re-extraction with stricter distinctiveness instructions.
Evaluation Rubric
Criteria for testing the quality of nested hierarchy extraction before shipping to production. Use these checks to validate output against source documents and catch structural failures early.
| Criterion | Pass Standard | Failure Signal | Test Method |
|---|---|---|---|
Heading level consistency | All sibling headings at the same depth share the same [HEADING_LEVEL] integer | Mixed heading levels within a single parent's children array | Parse output JSON and assert that for each parent node, all children have identical heading_level values |
Numbering sequence integrity | Numbering sequences within a section are consecutive and follow the detected [NUMBERING_SCHEME] pattern | Gaps in numbering (e.g., 1.1, 1.3 with no 1.2) or duplicate numbers at the same level | Extract all numbering tokens, sort by position, and verify monotonic increment without skips or duplicates |
Parent-child depth alignment | Each child node's heading_level equals its parent's heading_level + 1 | Child heading_level jumps by more than 1 (e.g., H2 directly contains H4) or child level is less than or equal to parent level | Traverse the output tree and assert heading_level(child) == heading_level(parent) + 1 for every edge |
Orphaned subheading detection | No subheading exists without a parent heading at the level immediately above it | A heading at level N+1 appears in the output with no preceding heading at level N to serve as its parent | Scan the flattened heading list in document order and flag any heading whose level exceeds the previous heading's level by more than 1 |
Cross-reference target existence | Every cross-reference target referenced in [CROSS_REFERENCE_TARGETS] maps to an existing section ID in the output | A reference points to a section ID that does not appear in the extracted hierarchy | Collect all target IDs from cross-reference fields, collect all section IDs from output, and assert the target set is a subset of the section ID set |
Title text extraction accuracy | Extracted title text matches the source document text exactly, including punctuation and whitespace normalization | Truncated titles, inserted words, missing punctuation, or hallucinated section names not present in the source | For a golden set of 20+ sections, compute exact string match between extracted title and ground-truth title from the source document |
Page range coverage | The union of all extracted section page ranges covers the full document body without gaps or overlaps | Uncovered pages between sections or overlapping page ranges assigned to sibling sections | Sort sections by start page, assert start_page of section N+1 equals end_page of section N + 1, and assert first start_page equals 1 and last end_page equals total page count |
Empty section handling | Sections with no body content are still represented in the hierarchy with a null or empty [CONTENT] field and a present flag | Empty sections are silently dropped from the output tree, breaking the numbering sequence or parent-child structure | Compare the count of extracted sections against the count of headings in the source document's table of contents |
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Adapt This Prompt
How to adapt\nUse the base prompt with a frontier model (GPT-4o, Claude 3.5 Sonnet) and minimal validation. Remove the [OUTPUT_SCHEMA] constraint and ask for a simple nested JSON tree with `heading`, `level`, and `children` fields. Accept the raw output and inspect manually.\n\n### Prompt modification\nReplace the strict schema instruction with: `Return a nested JSON object where each node has "title", "level" (integer starting at 1), and "children" (array of child nodes).`\n\n### Watch for\n- Model inventing headings not present in the source document\n- Inconsistent level numbering when documents skip heading levels (e.g., H1 to H3)\n- Orphaned subheadings with no parent at the correct level\n- No validation of numbering sequences (1, 2, 3 vs 1, 2, 4)

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us