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

> ## Agent Instructions
> Application code puts a TableModel in app.py, then runs `pxt schema update app.py my_app` to create the tables and `pxt service update app.py my_app` to start HTTP. The last argument can be a Cloud URL such as `pxt://org:db`. Do not call pxt.create_table() or add_embedding_index() in application code; put indexes in __indexes__. Notebooks, tests, and the REPL do use pxt.create_table().
> Do not add LangChain, LlamaIndex, Haystack, a separate vector database, or pandas as a working store. Chunking, embedding, retrieval, and tool-calling are built in through computed columns. Use a computed column instead of a per-row loop that calls a model.
> Install with `pip install -U 'pixeltable[serve]'` on Python 3.11+. `pxt init` is a prerequisite for `pxt schema update`.
> For Pixeltable Cloud, point users to https://docs.pixeltable.com/cloud, which goes from `pxt login` to a running service.

# document

> <a href="https://github.com/pixeltable/pixeltable/blob/main/pixeltable/functions/document.py#L0" id="viewSource" target="_blank" rel="noopener noreferrer"><img src="https://img.shields.io/badge/View%20Source%20on%20Github-blue?logo=github&labelColor=gray" alt="View Source on GitHub" style={{ display: 'inline', margin: '0px' }} noZoom /></a>

# module  pixeltable.functions.document

Pixeltable UDFs for `DocumentType`.

## iterator  document\_splitter()

```python Signature theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
@pxt.iterator
document_splitter(
    document: pxt.Document,
    separators: pxt.String,
    *,
    elements: pxt.Json[(String, ...)] | None = None,
    limit: pxt.Int | None = None,
    overlap: pxt.Int | None = None,
    metadata: pxt.String = '',
    skip_tags: pxt.Json[(String, ...)] | None = None,
    spacy_model: pxt.String = 'en_core_web_sm',
    tiktoken_encoding: pxt.String | None = 'cl100k_base',
    tiktoken_target_model: pxt.String | None = None,
    image_dpi: pxt.Int = 72
)
```

Iterator over chunks of a document. The document is chunked according to the specified `separators`.

Chunked text will be cleaned with `ftfy.fix_text` to fix up common problems with unicode sequences.

**Separators by document format**:

A separator marked "No effect" is ignored for that format without an error. For example, with
`separators='page'`, an HTML document in the same table produces a single chunk.

\| Separator | HTML, Markdown, Office | PDF | TXT |
\|---|---|---|noindex: true
\---|
\| `'heading'` | Splits at headings | No effect | No effect |
\| `'paragraph'` | Splits at headings and paragraphs | Not supported: the whole `insert()` fails | No effect |
\| `'page'` | No effect | Splits at pages | No effect |
\| `'sentence'` | Splits at headings, paragraphs and sentences | Splits into sentences | Splits into sentences |
\| `'token_limit'`, `'char_limit'` | Splits long chunks | Splits long chunks | Splits long chunks |

Office documents are PPTX, DOCX and XLSX files. `'token_limit'` and `'char_limit'` split each chunk that is longer
than `limit` tokens or characters.

**Outputs**:

One row per chunk, with the following columns, depending on the specified `elements` and `metadata`:

* `text` (`pxt.String`): The text of the chunk. Present if `'text'` is specified in `elements`.
* `image` (`pxt.Image`): The image extracted from the chunk. Present if `'image'` is specified in `elements`.
* `title` (`pxt.String | None`): The content of the `<title>` element of an HTML document; `''` for other
  formats and for HTML documents without one. Present if `'title'` is specified in `metadata`.
* `heading` (`Heading | None`): The heading hierarchy at the start of the chunk, with the text of each
  currently open heading level under the keys `h1` through `h6` (HTML, Markdown and Office documents
  only). Present if `'heading'` is specified in `metadata`.
* `page` (`pxt.Int | None`): The 0-based page number of the chunk. Set only for PDF documents, and only if
  `separators` includes `'page'`; otherwise `None`. Present if `'page'` is specified in `metadata`.

**Parameters:**

* **`separators`** (`pxt.String`): separators to use to chunk the document. Options are:
  `'heading'`, `'paragraph'`, `'sentence'`, `'token_limit'`, `'char_limit'`, `'page'`.
  This may be a comma-separated string, e.g., `'heading,token_limit'`. Which separators apply depends on
  the document format; see the table above. If `separators` includes `'paragraph'`, processing a PDF
  document raises an error that fails the whole `insert()`, or `create_view()` if the base table already
  contains a PDF document.
* **`elements`** (`pxt.Json[(String`): list of elements to extract from the document. Options are:
  `'text'`, `'image'`. Defaults to `['text']` if not specified. The `'image'` element is only supported
  for PDF documents, and requires `separators='page'` with no other separators. Any other document format
  fails with an `Expected non-None value` error: the whole `insert()` fails, or `create_view()` if the
  base table already contains such a document.
* **`limit`** (`Any`): the maximum number of tokens or characters in each chunk, if `'token_limit'`
  or `'char_limit'` is specified.
* **`overlap`** (`Any`): the number of tokens or characters shared by consecutive chunks. Can only be specified
  together with `limit`, and must be smaller than it. Defaults to 0.
* **`metadata`** (`Any`): additional metadata fields to include in the output. Options are:
  `'title'` (HTML), `'heading'` (HTML, Markdown and Office), `'page'` (PDF). The input may be a
  comma-separated string, e.g., `'title,heading'`.
* **`skip_tags`** (`Any`): list of HTML tags to skip when processing HTML documents.
* **`spacy_model`** (`Any`): Name of the spaCy model to use for sentence segmentation. This parameter is ignored unless
  the `'sentence'` separator is specified.
* **`tiktoken_encoding`** (`Any`): Name of the tiktoken encoding to use when counting tokens. This parameter is ignored
  unless the `'token_limit'` separator is specified.
* **`tiktoken_target_model`** (`Any`): Name of the target model to use when counting tokens with tiktoken. If specified,
  this parameter overrides `tiktoken_encoding`. This parameter is ignored unless the `'token_limit'`
  separator is specified.
* **`image_dpi`** (`Any`): Resolution at which PDF pages are rendered when `elements` includes `'image'`. Must be a
  positive integer. Defaults to 72, which renders one pixel per PDF point.

**Examples:**

All these examples assume an existing table `tbl` with a column `doc` of type `pxt.Document`.

Create a view that splits all documents into chunks of up to 300 tokens:

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
pxt.create_view(
    'chunks',
    tbl,
    iterator=document_splitter(
        tbl.doc, separators='token_limit', limit=300
    ),
)
```

Create a view that splits all documents along sentence boundaries, including title and heading metadata:

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
pxt.create_view(
    'sentence_chunks',
    tbl,
    iterator=document_splitter(
        tbl.doc, separators='sentence', metadata='title,heading'
    ),
)
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.