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

# Text

> The components that shape text: text.chunk splits it into pieces, text.render_template fills a template.

The text family has two components that work on text in the flow, with no provider and no outside service. `text.chunk`
cuts a long text into pieces, and `text.render_template` writes a message from a template and the values wired to it.

## `text.chunk`

Splits the text of a message into chunks of a fixed number of characters. Consecutive chunks overlap, so the text at one
edge appears in both chunks. The chunks are the input a `database.vector.ingest` takes.

| Route | Category | Value | Notes |
| :- | :- | :- | :- |
| `text` | Data input | `message` | Required. The text of the message is split. A message with no `text` block fails the step. |
| `method` | Data input | `choice`: `fixed_char_length`, default `fixed_char_length` | Required. The one method there is. |
| `char_length` | Data input | `integer`, default `1024` | Required. The most characters in a chunk. |
| `overlap_chars` | Data input | `integer`, default `32` | Required. How many characters a chunk shares with the one before it. |
| `chunks` | Data output | `messages` | Required. The chunks, in order. |
| `success` | Flow output | — | The text was split. |
| `failure` | Flow output | — | The text could not be read or split ([Edge cases](#edge-cases)). |

A chunk starts `char_length - overlap_chars` characters after the one before it, so with the defaults the first chunk
holds characters 0 to 1023 and the second starts at character 992. Characters are counted, not words or tokens, and a
chunk can start or end in the middle of a word. Each chunk is a new message with the role of the input message, one
`text` block and a new `attributes.id`. The step posts a `component.output` event, `Initial text (<n> char long) was
split into <m> chunks`.

### Edge cases

* A text of at most `char_length - overlap_chars` characters is one chunk, and an empty text is no chunks.
* A longer text can end with a chunk that lies wholly inside the one before it: with the defaults, a text of 1000
  characters gives two chunks, the second the last 8 characters, which the first already holds.
* An `overlap_chars` equal to `char_length` makes the step fail, with no event.
* An `overlap_chars` above `char_length` splits nothing: the step takes `success` with an empty `chunks`.

## `text.render_template`

Renders a [Jinja](https://jinja.palletsprojects.com/) template into one message. The template is a message, and every
other data input the component holds is a variable the template reads. The inputs are not fixed: add as many as the
template needs, each a data input with its own route, the `custom` route in the table.

| Route | Category | Value | Notes |
| :- | :- | :- | :- |
| `template` | Data input | `message`, default a `jinja` message holding the comment `{# Jinja template goes here #}` | Required. The first content block of the message is the template and must be a `text` block, or the step fails with no event. Its role and `metadata` carry over to the result. |
| `input_mode` | Data input | `choice`: `value`, `data`, default `value` | What a variable holds ([Input modes](#input-modes)). |
| `custom` | Data input | any | Any other route. Each one is a variable of the template, named by the route. |
| `content` | Data output | `message` | The rendered message. |
| `success` | Flow output | — | The template rendered. |
| `failure` | Flow output | — | The template could not be rendered. |

### Input modes

A variable is always one of the component's data inputs other than `template` and `input_mode`. An input holding no
value is not a variable. What the template sees depends on `input_mode`:

| `input_mode` | A variable holds | A `text` input named `company` is read as |
| :- | :- | :- |
| `value` | The port's value as stored, with its `type` beside the data. | `{{ company.text }}` |
| `data` | The data itself: the string, the number, the message, or for `messages` the list of messages. | `{{ company }}` |

In `data` mode a `message` is an object with `role`, `content` and `attributes`, so `{{ question.content[0].text }}`
reads its first block, and a `messages` input is a list a `{% for %}` loop walks. A `text` input in `value` mode is
`{"type": "text", "text": "..."}`, which is why `.text` is needed there.

Beside the inputs, the template always sees five variables: `blob`, `session`, `definition`, `execution` and
`attributes`, the last holding `current_time` and a fresh `id`. They are the same ones a
[`logic.code`](https://docs.blobhub.io/blob-types/workflow/workflows/component-code) program is given, and an input with one of those names is
replaced by the variable.

### When a render fails

The step takes `failure` in each case, and no `content` is written:

* An `input_mode` that is not `value` or `data` posts `"input_mode" is not supported`.
* A template that uses an attribute the renderer blocks posts `text.render_template "<name>": the template used a
  blocked attribute and was not rendered.` The message does not repeat the template.
* Any other error in the template, posts the error's trace.

### Example: a shipment notice for Corvel Freight

Corvel Freight sends a notice for each booking. A `text.render_template` in `data` mode has a `customer` input that
holds `Northwind Foods` and a `lines` input wired from a `data.messages`. The template is:

```jinja theme={null}
Booking for {{ customer }} on {{ attributes.current_time }}:
{% for line in lines %}- {{ line.content[0].text }}
{% endfor %}
```

The `content` output is a message with the template's role holding the finished text, ready for a `genai.chat` or a
thread.

### Example: chunk a handbook

A `text.chunk` reads the handbook from a `data.message` and writes the chunks to a `data.messages`, with a chunk length
of 800 characters and an overlap of 80. The connections follow the shape of a data connection: `text` reads the source
and `chunks` writes the target.

```json theme={null}
{
  "components": [
    {"id": "handbook", "category": "data", "type": "data.message", "name": "Handbook",
     "position": {"x": 0, "y": 200},
     "ports": [
       {"category": "data_input", "route": "value",
        "value": {"type": "message", "message": {"role": "user", "content": [
          {"type": "text", "text": "Dock rules. A driver may wait at most 45 minutes at a dock before ..."}]}}}
     ]},
    {"id": "split", "category": "processor", "type": "text.chunk", "name": "Split handbook",
     "position": {"x": 200, "y": 0},
     "ports": [
       {"category": "data_input", "route": "method", "value": {"type": "choice", "id": "fixed_char_length"}},
       {"category": "data_input", "route": "char_length", "value": {"type": "integer", "integer": 800}},
       {"category": "data_input", "route": "overlap_chars", "value": {"type": "integer", "integer": 80}}
     ]},
    {"id": "handbook_chunks", "category": "data", "type": "data.messages", "name": "Handbook chunks",
     "position": {"x": 400, "y": 200},
     "ports": [
       {"category": "data_input", "route": "persistence", "value": {"type": "choice", "id": "execution"}}
     ]}
  ],
  "connections": [
    {"id": "c1", "source_id": "split", "target_id": "handbook", "route": "text",
     "source_port": "data_input", "target_port": "data_reference"},
    {"id": "c2", "source_id": "split", "target_id": "handbook_chunks", "route": "chunks",
     "source_port": "data_output", "target_port": "data_reference"}
  ]
}
```

The flow connections that start `Split handbook` and follow its `success` route are left out.


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