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

# Message enhancement

> How Lola Send transforms AI model responses into structured, channel-optimized messages using Gemini.

Lola Send uses a Gemini-powered pipeline to transform raw AI text responses into structured, channel-appropriate messages. Agents produce text; the enhancer formats that text into interactive WhatsApp components — buttons, selection lists, links — and applies emoji context for the target channel.

## Pipeline stages

The message enhancer runs a two-stage pipeline on every complete agent response:

<Steps>
  <Step title="Language detection">
    Gemini identifies the conversation language from the raw response text, returning an ISO 2-letter code (e.g., `es`, `en`, `pt`). The detected language is used to enforce linguistic consistency in the formatted output.

    If language detection fails, the enhancer defaults to Spanish (`es`).
  </Step>

  <Step title="Structured formatting">
    The raw text is converted into a structured JSON object matching one of four component types. Gemini evaluates the content and selects the appropriate format:

    | Type      | When used                                                       | Key fields                                   |
    | --------- | --------------------------------------------------------------- | -------------------------------------------- |
    | `text`    | Standard messages, phone number requests                        | `content`                                    |
    | `buttons` | 1–3 discrete choices                                            | `content`, `options`                         |
    | `select`  | 4–10 options (lists up to 10 in options; >10 listed in content) | `content`, `options`, `list_title`, `button` |
    | `link`    | Messages containing URLs for external actions                   | `content`, `links` (array of `text` + `url`) |

    The enhancer enforces the detected language across all output fields — `content`, `options`, `list_title`, `button`, and link `text`.
  </Step>
</Steps>

## Output constraints

The enhancer enforces WhatsApp Business API field limits:

| Field            | Maximum length   |
| ---------------- | ---------------- |
| `content`        | 1,024 characters |
| `list_title`     | 24 characters    |
| `options` (each) | 20 characters    |
| `button`         | 20 characters    |
| Link `text`      | 20 characters    |

If any field exceeds its limit, Gemini summarizes the content to fit within the constraint. Options and button labels do not include emojis.

## WhatsApp-specific formatting

The enhancer applies contextual emoji hints for financial operations:

| Context      | Emoji |
| ------------ | ----- |
| Cash pickup  | 💸    |
| Bank account | 🏦    |
| Card payment | 💳    |

Bold text is converted from markdown double-asterisk (`**`) to WhatsApp single-asterisk (`*`) format. Newlines are stripped from the structured output.

## Model configuration

| Setting            | Value                  |
| ------------------ | ---------------------- |
| Model              | Gemini 2.5 Flash Lite  |
| Provider           | Google Cloud Vertex AI |
| Temperature        | 1                      |
| Top-p              | 0                      |
| Streaming          | Disabled               |
| Only full messages | `True`                 |

The message enhancer runs on **Gemini 2.5 Flash Lite** via **Google Cloud Vertex AI**. Vertex AI supports **VPC Service Controls**, which allows all inference traffic to remain within the bank's GCP perimeter — conversation data sent for enhancement never leaves the controlled network boundary.

The `only_full_messages=True` setting ensures enhancement runs only on complete responses, not on streaming fragments. This guarantees the enhancer has the full message context before selecting a component type.

<Info>
  Gemini on Vertex AI operates under Google Cloud's enterprise data processing terms: zero data retention, no model training on customer data, and full compliance with VPC Service Controls for network isolation.
</Info>

## Fallback behavior

If the enhancer fails for any reason — model error, invalid JSON output, parsing failure — the response falls back to a plain `OutgoingTextMessage` with the original text. No message is lost.

```python theme={null}
except Exception as e:
    return OutgoingTextMessage(
        lead=lead,
        content=text
    )
```

<Tip>
  The message enhancer operates independently of agent logic. Agents produce text responses; the enhancer formats them for the target channel. Changes to formatting do not require modifications to agent prompts or tool functions.
</Tip>

## Security considerations

* **No data generation**: The enhancer reformats existing text — it does not generate new content, invoke tool functions, or access backend services.
* **Language enforcement**: The two-stage pipeline detects and enforces the conversation language, preventing mixed-language responses that could confuse senders.
* **Deterministic output**: Temperature and top-p settings are configured for consistent, structured output rather than creative variation.

## Configuration and control

| Control           | Description                                                          |
| ----------------- | -------------------------------------------------------------------- |
| Model selection   | The Gemini model can be changed without affecting agent logic        |
| Output schema     | The structured JSON schema defines available component types         |
| Full message mode | `only_full_messages` controls whether partial messages are enhanced  |
| Fallback          | Plain text fallback ensures no messages are lost on enhancer failure |
