> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.instabase.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.instabase.com/_mcp/server.

# Cleaning results

> Refine extracted data using a variety of cleaning options.

Cleaning transforms extracted data into standardized formats for downstream systems.

> **Agent mode**
>
> Agent mode typically handles basic formatting directly in field prompts, reducing the need for separate cleaning steps. Use cleaning for complex transformations or when standardizing data for specific downstream requirements.

> **Legacy mode**
>
> Legacy mode often requires cleaning to achieve consistent formatting and structure. Legacy mode applies only to projects that haven't been updated to agent mode. All new projects and apps are created in agent mode by default.

## Quick clean

Quick clean options include:

* Changing character casing to all uppercase, all lowercase, or sentence case.

* Removing characters by specifying characters to remove, with no comma separator.

* Reformatting date by selecting from available formatting options.

  > **Tip**
  >
  > In numeric-only dates like 06/01/2024, use the **Input** field as needed to specify whether the original value lists the month or the day first.

Quick clean options use Python functions to reformat data. There is no model processing and no
unit cost.

## Cleaning prompt

Cleaning prompts refine field results using natural language instructions.

Follow these best practices to write effective cleaning prompts.

* **Include before and after examples** for varied input formats.

  *Standardize address format to street, city, state ZIP. For example:*\
  *123 Burnside St, Portland OR 97201 → 123 Burnside St, Portland, OR 97201*\
  *456 Elm Avenue, Charlotte, North Carolina 28105 → 456 Elm Ave, Charlotte, NC 28105*

* **Handle missing or invalid data**.

  *If the field contains a valid dollar amount, return it in the format \$X.XX. If no amount is found or the value is invalid, return "No amount specified."*

* **Normalize reference fields as text**.

  *Convert each list item to the format: Item: \[Item], Price: \[Price], Qty: \[Quantity] and separate each item with a semicolon*.

## Cleaning function

For advanced cleaning requirements involving complex transformations, external data, or mission-critical results, write a custom cleaning function in Python.

> **Tip**
>
> When working with JSON data, use `json.loads(previous_line)` to parse it.

For example, you might use a cleaning function to standardize phone numbers from various formats:

```python
def clean_phone_number(previous_line, context):
    """
    Standardizes phone numbers to (XXX) XXX-XXXX format.
    A simple example of a custom cleaning function that handles
    formatting beyond what quick clean options provide.
    """
    # Skip processing if input is empty or None
    if not previous_line:
        return previous_line

    # Extract only digits from the input
    digits = ''.join(char for char in previous_line if char.isdigit())

    # Format 10-digit numbers as (XXX) XXX-XXXX
    if len(digits) == 10:
        return f"({digits[0:3]}) {digits[3:6]}-{digits[6:10]}"

    # Return original for non-standard numbers
    return previous_line
```

Cleaning functions accept these parameters:

| Parameter                  | Required? | Description                                                                                                                                                                                                                                                                                                                                                                                                |
| -------------------------- | --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `previous_line`            | Required  | Represents the value of the preceding cleaning line, or the extraction value if the custom function is the first cleaning line.                                                                                                                                                                                                                                                                            |
| `context`                  | Required  | Stores metadata about the document.                                                                                                                                                                                                                                                                                                                                                                        |
| `context['document_text']` | Optional  | Retrieves the entire text of the document.                                                                                                                                                                                                                                                                                                                                                                 |
| `context['file_path']`     | Optional  | Retrieves the path to the uploaded file.                                                                                                                                                                                                                                                                                                                                                                   |
| `keys`                     | Optional  | Access custom variables and [organization secrets](/admin/secret-management). Use `keys['custom']['<key-name>']` for custom keys and `keys['secret']['<key-name>']` for secret keys.                                                                                                                                                                                                                       |
| `<additional-field-name>`  | Optional  | When writing custom functions in automation projects, click **Add argument** to select additional fields in the class to use in the function. Because fields are extracted sequentially, referenced parameters must precede the current field in the editing panel.  > **Tip**
>
> If necessary, reorder fields using the up and down arrows that display in the field editor when you hover over a field. |

Cleaning functions can return any value. The value is converted to a string when it's passed to subsequent refinement lines or validation rules. If the cleaning function encounters issues, it must raise an exception.

For additional guidance about custom functions, see [Writing custom functions](/automate/custom-functions).