# HTML Text Extraction

> Extracts the main readable content from HTML or XHTML documents using trafilatura with content-boundary detection.

Source: https://docs.sommatic.ai/en/nodes/ai.html.extract.text/

- Slug: `ai.html.extract.text`
- Family: AI
- Version: 1.0.0
- Status: Available
- Execution: async

Extracts the main readable content from HTML or XHTML documents using trafilatura with content-boundary detection.

Strips navigation chrome, scripts, style blocks, advertising containers, cookie banners, headers, footers, and comment threads while preserving the substantive article body.

**Features:**
- **Boilerplate removal:** Detects and removes page chrome based on trafilatura's trained extractors, tuned for modern CMS and blog layouts.
- **Structural preservation:** Retains headings, paragraphs, ordered and unordered lists, and blockquotes in document order.
- **Precision/recall tradeoff:** `favor_precision` toggles between aggressive cleanup (default, fewer false positives) and permissive extraction (recovers content on unconventional layouts).
- **Metadata extraction:** Pulls title, canonical URL, author, publish date, site name, and language when surfaced by the page's meta tags, Open Graph, or JSON-LD.
- **Inputs:** Accepts raw HTML as a string, base64-encoded HTML, or a local file path.

## Configuration

- `favor_precision` (boolean, optional): Trade recall for precision. Turn off if content is getting truncated. Default: `true`.

Its configuration shows the shared execution settings, but this node does not apply retries (`retryOnFail`, `maxTries`) yet. `continueOnFail` is not applied by any node.

## Output ports

- `extracted` (default): Extracted
- `empty`: Empty
- `error`: Error

## Input contract

- `content` (string, optional): Raw HTML string.
- `content_base64` (string, optional)
- `file_path` (string, optional)

## Output contract

- `text` (string, optional)
- `chars` (number, optional)
- `empty` (boolean, optional)
- `method` (string, optional)
- `metadata` (object, optional)

## In expressions

What a later node can read from this node's output with `$("HTML Text Extraction").item.json.<path>` (write your node's label instead of `HTML Text Extraction`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `extracted`, from the output shown in Ports.

- `$("HTML Text Extraction").item.json.text` (string) = `"Release notes\nThe new version adds export t…"`
- `$("HTML Text Extraction").item.json.chars` (number) = `48`
- `$("HTML Text Extraction").item.json.empty` (boolean) = `false`
- `$("HTML Text Extraction").item.json.method` (string) = `"trafilatura"`
- `$("HTML Text Extraction").item.json.metadata` (object) = `{…}`
- `$("HTML Text Extraction").item.json.metadata.input_chars` (number) = `18342` (Not suggested by the editor)


## How it works

Extracts the main readable text of an HTML document with trafilatura, dropping navigation, scripts, comments and other boilerplate; tables are kept.

1. Reads `content` (raw HTML string), `content_base64` and `file_path` from the node config, falling back to the upstream payload; fails on `error` when all three are missing.
2. Uses the first source present in this order: `content`, `content_base64` (decoded as UTF-8), `file_path`.
3. Runs trafilatura with comments excluded, tables included, no formatting and `favor_precision` from the config (default `true`).
4. Routes to `empty` when no text was extracted, otherwise to `extracted`.

## In a workflow

### Node

```json
{
  "node_id": "node-clean-page",
  "label": "Clean page",
  "operator_slug": "ai.html.extract.text",
  "operator_version": "1.0.0",
  "config": {
    "content": "{{ $json.body }}",
    "favor_precision": true,
    "execution": {
      "timeout_ms": 60000
    }
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "text": "Release notes\nThe new version adds export to CSV.",
    "chars": 48,
    "empty": false,
    "method": "trafilatura",
    "metadata": {
      "input_chars": 18342
    }
  },
  "port": "extracted"
}
```

## Output ports (behaviour in the code)

### `extracted`

Main text was found.

```json
{
  "text": "Release notes\nThe new version adds export to CSV.",
  "chars": 48,
  "empty": false,
  "method": "trafilatura",
  "metadata": {
    "input_chars": 18342
  }
}
```

### `empty`

trafilatura found no main content (very short pages, pages that are mostly navigation).

```json
{
  "text": "",
  "chars": 0,
  "empty": true,
  "method": "trafilatura",
  "metadata": {
    "input_chars": 412
  }
}
```

### `error`

No input was given or the script failed.

```json
{
  "error": "content, content_base64, or file_path is required.",
  "stderr": ""
}
```

## Behaviour in the code

- `content, content_base64, file_path`: Not declared in the config schema, but read from the node config first and then from the upstream payload. Usually you map them from the previous node with expressions.
- `favor_precision`: Only `false` turns it off; any other value, including a missing key, keeps precision mode. Turn it off when real content is being cut.
- `execution`: Only `execution.timeout_ms` is read (fallback 60000 ms). `retryOnFail`, `maxTries`, `waitBetweenTries`, `continueOnFail` and `alwaysOutputData` are ignored.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `content, content_base64, or file_path is required.` | `error` | None of the three sources was found in the config or the upstream payload. |
| `Either content, content_base64, or file_path is required` | `error` | Raised by the script when only `file_path` was given and the file does not exist on the host. |
| `Python timed out after <timeout_ms>ms` | `error` | The script exceeded the node timeout. Raise `execution.timeout_ms` or send a smaller file. |
| `Python stdout exceeded MAX_STDOUT_BYTES (52428800 bytes)` | `error` | The result (text or base64 files) is larger than the 50 MB cap. Split the input upstream. |
| `Python runtime not available (ENOENT)` | `error` | The host has no Python interpreter. A failed dependency install shows `PythonRunner failed: <message>` instead. Both are host problems, not workflow problems. |

## Notes and limits

- Runs a Python script in a child process. The first run on a worker host creates a virtual environment and installs the script's pinned dependencies (it can take minutes and is not counted in the node timeout); later runs reuse it.
- `file_path` must point to a file on the local disk of the orchestrator host that runs the node; it is not a URL and is not fetched from storage.
- The script is killed after `execution.timeout_ms`; when that key is not set the code uses 60000 ms, not the schema default of 300000.
- The node does not fetch URLs; give it HTML that an upstream node already downloaded.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "ai.html.extract.text",
  "name": "HTML Text Extraction",
  "category": "ai",
  "version": "1.0.0",
  "description": "Extracts the main readable content from HTML or XHTML documents using trafilatura with content-boundary detection.\n\n  Strips navigation chrome, scripts, style blocks, advertising containers, cookie banners, headers, footers, and comment threads while preserving the substantive article body.\n\n  **Features:**\n  - **Boilerplate removal:** Detects and removes page chrome based on trafilatura's trained extractors, tuned for modern CMS and blog layouts.\n  - **Structural preservation:** Retains headings, paragraphs, ordered and unordered lists, and blockquotes in document order.\n  - **Precision/recall tradeoff:** `favor_precision` toggles between aggressive cleanup (default, fewer false positives) and permissive extraction (recovers content on unconventional layouts).\n  - **Metadata extraction:** Pulls title, canonical URL, author, publish date, site name, and language when surfaced by the page's meta tags, Open Graph, or JSON-LD.\n  - **Inputs:** Accepts raw HTML as a string, base64-encoded HTML, or a local file path.",
  "capabilities": [
    "html",
    "xhtml",
    "markup",
    "text-extraction"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "favor_precision": {
        "type": "boolean",
        "title": "Favor Precision",
        "default": true,
        "description": "Trade recall for precision. Turn off if content is getting truncated."
      },
      "execution": {
        "type": "object",
        "title": "Execution Settings",
        "properties": {
          "alwaysOutputData": {
            "type": "boolean",
            "title": "Always Output Data",
            "description": "Always output data, even on error (if configured).",
            "default": false
          },
          "retryOnFail": {
            "type": "boolean",
            "title": "Retry On Fail",
            "description": "Retry the execution if it fails.",
            "default": false
          },
          "maxTries": {
            "type": "integer",
            "title": "Max Retries",
            "description": "Number of retries.",
            "default": 3,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "waitBetweenTries": {
            "type": "integer",
            "title": "Wait Between Retries (ms)",
            "default": 1000,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "continueOnFail": {
            "type": "boolean",
            "title": "Continue On Fail",
            "description": "Do not crash workflow on error.",
            "default": false
          },
          "timeout_ms": {
            "type": "integer",
            "title": "Timeout (ms)",
            "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
            "default": 300000
          }
        }
      }
    }
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "content": {
        "type": "string",
        "description": "Raw HTML string."
      },
      "content_base64": {
        "type": "string"
      },
      "file_path": {
        "type": "string"
      }
    }
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "text": {
        "type": "string"
      },
      "chars": {
        "type": "number"
      },
      "empty": {
        "type": "boolean"
      },
      "method": {
        "type": "string"
      },
      "metadata": {
        "type": "object"
      }
    }
  },
  "ports": [
    {
      "port_id": "extracted",
      "label": "Extracted",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "empty",
      "label": "Empty",
      "is_default": false,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "tags": [
    "ai",
    "html",
    "text",
    "markup"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_sommatic_ai",
    "profile": {
      "name": "Sommatic AI",
      "verified": true,
      "url": "https://me.veripass.com/sommatic",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "favor_precision": {
      "type": "boolean",
      "title": "Favor Precision",
      "default": true,
      "description": "Trade recall for precision. Turn off if content is getting truncated."
    },
    "execution": {
      "type": "object",
      "title": "Execution Settings",
      "properties": {
        "alwaysOutputData": {
          "type": "boolean",
          "title": "Always Output Data",
          "description": "Always output data, even on error (if configured).",
          "default": false
        },
        "retryOnFail": {
          "type": "boolean",
          "title": "Retry On Fail",
          "description": "Retry the execution if it fails.",
          "default": false
        },
        "maxTries": {
          "type": "integer",
          "title": "Max Retries",
          "description": "Number of retries.",
          "default": 3,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "waitBetweenTries": {
          "type": "integer",
          "title": "Wait Between Retries (ms)",
          "default": 1000,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "continueOnFail": {
          "type": "boolean",
          "title": "Continue On Fail",
          "description": "Do not crash workflow on error.",
          "default": false
        },
        "timeout_ms": {
          "type": "integer",
          "title": "Timeout (ms)",
          "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
          "default": 300000
        }
      }
    }
  }
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "content": {
      "type": "string",
      "description": "Raw HTML string."
    },
    "content_base64": {
      "type": "string"
    },
    "file_path": {
      "type": "string"
    }
  }
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "text": {
      "type": "string"
    },
    "chars": {
      "type": "number"
    },
    "empty": {
      "type": "boolean"
    },
    "method": {
      "type": "string"
    },
    "metadata": {
      "type": "object"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "extracted",
    "label": "Extracted",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "empty",
    "label": "Empty",
    "is_default": false,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "is_default": false,
    "supports_condition": false
  }
]
```
