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

# Chunking

> Split SDK documents into retrieval-ready chunks.

Chunking turns normalized documents into retrieval-sized passages with IDs and metadata. In the full SDK workflow, `rag.ingest()` runs this stage for you. Use `rag.chunk()` directly when you need to inspect or transform chunks before embedding.

```python theme={null}
from ragrails import RagRails

rag = RagRails()
```

## From Extraction Output

```python theme={null}
docs = rag.parse(files="files/refund-policy.pdf")
chunks = rag.chunk(
    markdown=docs.outputs,
    chunk_size=1200,
    chunk_overlap=150,
    min_chunk_length=80,
)
```

## From Direct Markdown

```python theme={null}
chunks = rag.chunk(
    markdown="# Refund policy\n\nCustomers can request a refund within 30 days of purchase.",
    title="Refund policy",
    source="policy.md",
)
```

## Result

```python theme={null}
chunks.inputs
chunks.chunks
chunks.items
chunks.failed
chunks.errors
```

A chunk item is a plain dictionary:

```json theme={null}
{
  "id": "refund-policy-0",
  "text": "Customers can request a refund within 30 days of purchase.",
  "source": "files/refund-policy.pdf",
  "metadata": {"title": "Refund policy", "chunk_index": 0}
}
```

## Input Forms

```python theme={null}
# String
rag.chunk(markdown="# Guide\n\nContent here.")

# List of strings
rag.chunk(markdown=["# Doc 1\n\nContent.", "# Doc 2\n\nMore content."])

# Document dicts from parse(), scrape(), fetch(), or ingest source results
rag.chunk(markdown=extraction_result.outputs)
```

For dict inputs, `text` is required. `source`, `title`, and `metadata` are preserved when present.

## Parameters

| Parameter          | Default  | Description                                                                   |
| ------------------ | -------- | ----------------------------------------------------------------------------- |
| `markdown`         | required | String, list of strings, or list of dicts with non-empty `text`.              |
| `title`            | `""`     | Title used for plain string inputs when dict metadata does not set one.       |
| `source`           | `""`     | Source used for plain string inputs.                                          |
| `chunk_size`       | `2000`   | Target max characters per chunk.                                              |
| `chunk_overlap`    | `200`    | Character overlap between adjacent chunks. Must be smaller than `chunk_size`. |
| `min_chunk_length` | `100`    | Drop chunks shorter than this.                                                |

## Next Stage

```python theme={null}
embedded = rag.embed(chunks=chunks.items)
```

<CardGroup cols={2}>
  <Card title="Extraction" icon="file-input" href="/usage/sdk/extraction">Create document inputs.</Card>
  <Card title="Embedding" icon="binary" href="/usage/sdk/embedding">Embed chunk outputs.</Card>
</CardGroup>
