> ## Documentation Index
> Fetch the complete documentation index at: https://docs.vlm.run/llms.txt
> Use this file to discover all available pages before exploring further.

# Layout Detection

> Identify and analyze document structure with visual result previews showing highlighted extractions and field overlays

<Frame caption="Layout Detection Example on the Qwen-2.5 VL Tech Report.">
  <div style={{ display: 'flex', flexWrap: 'wrap', gap: '4px' }}>
    <div style={{ flex: '1 1 calc((100% - 8px) / 3)' }}>
      <img src="https://mintcdn.com/autonomiai/n0UzFHKSRrWx1Yji/agents/assets/layout-1.jpg?fit=max&auto=format&n=n0UzFHKSRrWx1Yji&q=85&s=897ec8eb91caa9a5aa34291a4468dc65" style={{ width: '100%', border: '1px solid #ddd', borderRadius: '4px' }} width="1241" height="1754" data-path="agents/assets/layout-1.jpg" />
    </div>

    <div style={{ flex: '1 1 calc((100% - 8px) / 3)' }}>
      <img src="https://mintcdn.com/autonomiai/n0UzFHKSRrWx1Yji/agents/assets/layout-2.jpg?fit=max&auto=format&n=n0UzFHKSRrWx1Yji&q=85&s=3d214d394bd52bb52e2c213e0e30f3c7" style={{ width: '100%', border: '1px solid #ddd', borderRadius: '4px' }} width="1241" height="1754" data-path="agents/assets/layout-2.jpg" />
    </div>

    <div style={{ flex: '1 1 calc((100% - 8px) / 3)' }}>
      <img src="https://mintcdn.com/autonomiai/n0UzFHKSRrWx1Yji/agents/assets/layout-3.jpg?fit=max&auto=format&n=n0UzFHKSRrWx1Yji&q=85&s=37b25be0819cc46505cd683994049fd2" style={{ width: '100%', border: '1px solid #ddd', borderRadius: '4px' }} width="1241" height="1754" data-path="agents/assets/layout-3.jpg" />
    </div>

    <div style={{ flex: '1 1 calc((100% - 8px) / 3)' }}>
      <img src="https://mintcdn.com/autonomiai/n0UzFHKSRrWx1Yji/agents/assets/layout-4.jpg?fit=max&auto=format&n=n0UzFHKSRrWx1Yji&q=85&s=6eda133fe1638dfb152fffd3c086a4c1" style={{ width: '100%', border: '1px solid #ddd', borderRadius: '4px' }} width="1241" height="1754" data-path="agents/assets/layout-4.jpg" />
    </div>

    <div style={{ flex: '1 1 calc((100% - 8px) / 3)' }}>
      <img src="https://mintcdn.com/autonomiai/n0UzFHKSRrWx1Yji/agents/assets/layout-5.jpg?fit=max&auto=format&n=n0UzFHKSRrWx1Yji&q=85&s=39da0b0189ced080da83c76cbef4ffe5" style={{ width: '100%', border: '1px solid #ddd', borderRadius: '4px' }} width="1241" height="1754" data-path="agents/assets/layout-5.jpg" />
    </div>

    <div style={{ flex: '1 1 calc((100% - 8px) / 3)' }}>
      <img src="https://mintcdn.com/autonomiai/n0UzFHKSRrWx1Yji/agents/assets/layout-6.jpg?fit=max&auto=format&n=n0UzFHKSRrWx1Yji&q=85&s=55012b4dd4fa13f6890b109082367d50" style={{ width: '100%', border: '1px solid #ddd', borderRadius: '4px' }} width="1241" height="1754" data-path="agents/assets/layout-6.jpg" />
    </div>
  </div>
</Frame>

## Usage Example

<Tip>
  For best results, we recommend using the [Structured Outputs API](/agents/structured-responses) to get responses in a structured and validated data format.
</Tip>

<Info title="Document Layout Elements">
  The following examples can detect headers, paragraphs, tables, lists, figures, and other document elements. The response schema includes bounding boxes, reading order and more.
</Info>

<CodeGroup>
  ```python Python theme={"theme":{"light":"github-light","dark":"dark-plus"}}
  from vlmrun.client import VLMRun

  # Initialize the VLMRun client
  client = VLMRun(api_key="<VLMRUN_API_KEY>")

  # Analyze document layout
  response = client.agent.completions.create(
      model="vlmrun-orion-1:auto",
      messages=[
          {
            "role": "user",
            "content": [
              {"type": "text", "text": "Analyze the document layout and identify all elements with bounding boxes"},
              {"type": "image_url", "image_url": {"url": "https://storage.googleapis.com/vlm-data-public-prod/hub/examples/document.layout/qwen-25-vl-tech-report.jpg", "detail": "auto"}}
            ]
          }
      ]
  )

  print(response.choices[0].message.content)
  ```

  ```python Python - Structured Outputs theme={"theme":{"light":"github-light","dark":"dark-plus"}}
  from vlmrun.client import VLMRun
  from pydantic import BaseModel, Field

  # Define the response schema
  class LayoutElement(BaseModel):
    type: str = Field(..., description="Type of layout element")
    xywh: tuple[float, float, float, float] = Field(..., description="Bounding box (x, y, w, h)")

  class LayoutResponse(BaseModel):
    elements: list[LayoutElement] = Field(..., description="List of layout elements")

  # Initialize the VLMRun client
  client = VLMRun(api_key="<VLMRUN_API_KEY>")

  # Analyze document layout with structured output
  response = client.agent.completions.create(
      model="vlmrun-orion-1:auto",
      messages=[
          {
            "role": "user",
            "content": [
              {"type": "text", "text": "Analyze the document layout and identify all elements with bounding boxes"},
              {"type": "image_url", "image_url": {"url": "https://storage.googleapis.com/vlm-data-public-prod/hub/examples/document.layout/qwen-25-vl-tech-report.jpg", "detail": "auto"}}
            ]
          }
      ],
      response_format={"type": "json_schema", "schema": LayoutResponse.model_json_schema()},
  )

  # Validate the response
  result = LayoutResponse.model_validate_json(response.choices[0].message.content)
  # >>> LayoutResponse(elements=[LayoutElement(type="caption", xywh=(...)), ...])
  ```

  ```typescript Node.js theme={"theme":{"light":"github-light","dark":"dark-plus"}}
  import { VlmRun } from "vlmrun";

  const client = new VlmRun({
    apiKey: "<VLMRUN_API_KEY>",
    baseURL: "https://api.vlm.run/v1"
  });

  const response = await client.agent.completions.create({
    model: "vlmrun-orion-1:auto",
    messages: [
      {
        role: "user",
        content: [
          { type: "text", text: "Analyze the document layout and identify all elements with bounding boxes" },
          { type: "image_url", image_url: { url: "https://storage.googleapis.com/vlm-data-public-prod/hub/examples/document.layout/qwen-25-vl-tech-report.jpg", detail: "auto" } }
        ]
      }
    ]
  });

  console.log(response.choices[0].message.content);
  ```

  ```typescript Node.js - Structured Outputs [expandable] theme={"theme":{"light":"github-light","dark":"dark-plus"}}
  import { VlmRun } from "vlmrun";
  import { z } from "zod";
  import { zodToJsonSchema } from "zod-to-json-schema";

  // Define the response schema with Zod
  const LayoutResponseSchema = z.object({
    elements: z.array(z.object({
      type: z.string().describe("Type of layout element"),
      xywh: z.array(z.number()).describe("Bounding box (x, y, w, h)")
    })).describe("List of layout elements")
  });

  // Initialize the VLMRun client
  const client = new VlmRun({
    apiKey: "<VLMRUN_API_KEY>",
    baseURL: "https://api.vlm.run/v1"
  });

  // Analyze document layout with structured output
  const response = await client.agent.completions.create({
    model: "vlmrun-orion-1:auto",
    messages: [
      {
        role: "user",
        content: [
          { type: "text", text: "Analyze the document layout and identify all elements with bounding boxes" },
          { type: "image_url", image_url: { url: "https://storage.googleapis.com/vlm-data-public-prod/hub/examples/document.layout/qwen-25-vl-tech-report.jpg", detail: "auto" } }
        ]
      }
    ],
    response_format: {
      type: "json_schema",
      schema: zodToJsonSchema(LayoutResponseSchema)
    }
  });

  const result = LayoutResponseSchema.parse(JSON.parse(response.choices[0].message.content));
  ```
</CodeGroup>

## FAQ

<AccordionGroup>
  <Accordion title="What layout elements are supported?" icon="list">
    * **Headers**: H1-H6 level headers with hierarchical structure
    * **Paragraphs**: Body text blocks with proper text flow
    * **Titles**: Main title of the document
    * **Tables**: Structured data with row/column detection
    * **Figures**: Images, charts, diagrams, and visual elements
    * **Lists**: Bulleted and numbered list structures
    * **Captions**: Figure and table captions with associations
    * **Footnotes**: Footnotes with references and content
    * **Formulas**: Mathematical formulas and equations
    * **Pictures**: Images and visual elements
    * **Section Headers**: Section headers and titles
  </Accordion>

  <Accordion title="What format do the bounding boxes come in?" icon="crop">
    The bounding boxes come in the format of `xywh`, where `x` and `y` are the top-left corner coordinates, and `w` and `h` are the width and height of the bounding box. All values are in pixels relative to the document image.
  </Accordion>

  <Accordion title="What is the reading order?" icon="sort">
    The reading order indicates the sequence in which elements should be read, following the natural document flow from top to bottom and left to right. This is useful for accessibility and content extraction.
  </Accordion>

  <Accordion title="Can it process multi-page documents?" icon="file">
    Yes, the layout detection can process multi-page documents. Each page is analyzed separately, and the results include page-specific bounding boxes and reading orders.
  </Accordion>
</AccordionGroup>
