# Enrich Data

> Enrich records by adding computed, inferred, or standardized fields using an LLM.

Source: https://docs.sommatic.ai/en/nodes/ai.enrich/

- Slug: `ai.enrich`
- Family: AI
- Version: 1.0.0
- Status: Available
- Execution: async

Enrich records by adding computed, inferred, or standardized fields using an LLM.

Select an enrichment mode and define the output fields you need. The operator reads the input record, applies the chosen strategy, and returns the enriched record with confidence scores per field.

**Operations:**
- **Categorize:** Add category tags to a record based on its content.
- **Standardize:** Normalize names, addresses, dates, and phone numbers to consistent formats.
- **Infer:** Fill missing field values by reasoning from available context.
- **Augment:** Add computed fields such as seniority from job title or industry from company name.

## Configuration

- `llm_provider_id` (string, optional): The `id` of a `LlmProviderModel` record (e.g. `wf-llm-provider-*`). The orchestrator delegates to the backend: `POST /cognitive-infrastructure/cognitive-assets/llm-provider/invoke` for sync mode, or `POST /chat/conversation/execute/stream-ephemeral` (SSE) for stream mode. The backend resolves endpoint + api-key + deployment from the record and routes to the correct adapter.
- `invocation_mode` (string, optional): Transport for the LLM call. `sync` blocks until the full response arrives. `stream` consumes tokens via SSE (required for long generations). Default `sync`. Default: `sync`. Allowed values: `sync`, `stream`.
- `timeout_ms` (integer, optional): Maximum time to wait for the invocation to complete (or idle timeout between chunks in stream mode). Default 30000 for sync, 600000 for stream.
- `enrichment_mode` (string, required): Strategy to apply: categorize, standardize, infer missing values, or augment with computed fields. Default: `categorize`. Allowed values: `categorize`, `standardize`, `infer`, `augment`.
- `input_fields` (array<string>, optional): Which fields from the input record to read.
- `output_fields` (array<object>, required): Fields to add or compute in the enriched record.
- `knowledge_resource_id` (string, optional): Optional: reference data or lookup tables for enrichment.
- `language` (string, optional): Language of the input data. Default: `en`.

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

- `success` (default): All requested fields were added successfully.
- `partial`: Some requested fields could not be computed.
- `error`: Enrichment failed.

## Input contract

- `record` (object, required): The data record to enrich.
- `context` (string, optional): Optional context to improve enrichment accuracy.

## Output contract

- `enriched_record` (object, optional): Original record merged with new fields.
- `added_fields` (array, optional): List of field names successfully added.
- `confidence_per_field` (object, optional): Confidence score (0-1) for each added field.

## In expressions

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

- `$("Enrich Data").item.json.enriched_record` (object) = `{…}`: Original record merged with new fields.
- `$("Enrich Data").item.json.added_fields` (array) = `[…] (2)`: List of field names successfully added.
- `$("Enrich Data").item.json.confidence_per_field` (object) = `{…}`: Confidence score (0-1) for each added field.
- `$("Enrich Data").item.json.enriched_record.company` (string) = `"Northwind Logistics"` (Not suggested by the editor)
- `$("Enrich Data").item.json.enriched_record.title` (string) = `"Head of Operations"` (Not suggested by the editor)
- `$("Enrich Data").item.json.enriched_record.industry` (string) = `"Logistics"` (Not suggested by the editor)
- `$("Enrich Data").item.json.enriched_record.seniority` (string) = `"Director"` (Not suggested by the editor)
- `$("Enrich Data").item.json.confidence_per_field.industry` (number) = `0.93` (Not suggested by the editor)
- `$("Enrich Data").item.json.confidence_per_field.seniority` (number) = `0.81` (Not suggested by the editor)


## How it works

Adds the requested `output_fields` to a JSON `record` with the model (categorize, standardize, infer or augment) and routes to `success` when every field was added, or `partial` otherwise.

1. Merges the upstream payload with the node config (config wins) and reads `record`, which must be an object; `output_fields` must be non-empty.
2. Keeps only the `input_fields` keys of the record when `input_fields` is set; otherwise sends the whole record, pretty-printed, as the user message.
3. With `knowledge_resource_id`, appends the resource `content` as JSON under `Reference data for lookup:`; a failed fetch is only logged.
4. Sends a mode instruction listing each output field as `- name (type): description` and a strict JSON format; `temperature: 0.1`.
5. Parses the answer as JSON; it must contain `enriched_record`. Routes to `success` when every `output_fields` name is in `added_fields`, else to `partial`.

## In a workflow

### Node

```json
{
  "node_id": "node-enrich-lead",
  "label": "Enrich lead",
  "operator_slug": "ai.enrich",
  "operator_version": "1.0.0",
  "config": {
    "record": "{{ $json.lead }}",
    "enrichment_mode": "augment",
    "input_fields": [
      "company",
      "title"
    ],
    "output_fields": [
      {
        "name": "industry",
        "type": "string",
        "description": "Industry of the company."
      },
      {
        "name": "seniority",
        "type": "string",
        "description": "Seniority implied by the job title."
      }
    ],
    "language": "en",
    "llm_provider_id": "wf-llm-provider-xxxx",
    "invocation_mode": "sync"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "enriched_record": {
      "company": "Northwind Logistics",
      "title": "Head of Operations",
      "industry": "Logistics",
      "seniority": "Director"
    },
    "added_fields": [
      "industry",
      "seniority"
    ],
    "confidence_per_field": {
      "industry": 0.93,
      "seniority": 0.81
    }
  },
  "port": "success"
}
```

## Output ports (behaviour in the code)

### `success`

The model reported every requested field in `added_fields`.

```json
{
  "enriched_record": {
    "company": "Northwind Logistics",
    "title": "Head of Operations",
    "industry": "Logistics",
    "seniority": "Director"
  },
  "added_fields": [
    "industry",
    "seniority"
  ],
  "confidence_per_field": {
    "industry": 0.93,
    "seniority": 0.81
  }
}
```

### `partial`

At least one requested field is missing from `added_fields`.

```json
{
  "enriched_record": {
    "company": "Northwind Logistics",
    "title": "Head of Operations",
    "industry": "Logistics"
  },
  "added_fields": [
    "industry"
  ],
  "confidence_per_field": {
    "industry": 0.93
  }
}
```

### `error`

The record or output fields are missing, the model call failed or the answer had no `enriched_record`.

```json
{
  "error": "Model did not return valid enrichment output."
}
```

## Behaviour in the code

- `record`: Not in the config schema (it is in the input contract), but read from the config first and then from the upstream payload.
- `enrichment_mode`: An unknown value uses the `categorize` instruction.
- `input_fields`: Filters what the model sees, but the model is asked to return the original fields in `enriched_record`; fields you filtered out are not merged back by the code.
- `timeout_ms`: Stream mode only: idle timeout between chunks, default 600000. In sync mode it is not applied to the request.
- `execution`: Declared but ignored: neither the worker nor the engine reads `retryOnFail`, `maxTries`, `waitBetweenTries`, `continueOnFail`, `alwaysOutputData` or `timeout_ms` from this block.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `Missing or invalid input record.` | `error` | `record` is absent or not an object. |
| `At least one output field must be defined.` | `error` | `output_fields` is empty. |
| `Model did not return valid enrichment output.` | `error` | The answer was not JSON or had no `enriched_record`. |
| `[AiEnrich] Missing 'llm_provider_id' in node config (LlmProviderModel.id, e.g. 'wf-llm-provider-*'). The legacy 'model_id' alias is no longer supported — update the node config to use 'llm_provider_id'.` | `error` | `llm_provider_id` is empty or not a string. Pick an LLM provider record with chat capability. |
| `LLM Invocation Error: <backend message>` | `error` | The backend rejected the call: unknown or inactive provider record, provider error, quota or content filter. Check the provider record and the backend logs. |
| `LLM Invocation Error: empty response from backend (<transport error>)` | `error` | Sync mode: the HTTP call to the backend failed (network error, non-2xx status or timeout). Long prompts often hit the request timeout; switch to `invocation_mode: "stream"`. |
| `RemoteApiService.stream: idle timeout after <timeout_ms>ms` | `error` | Stream mode: no chunk arrived within `timeout_ms`. A failed stream request shows `RemoteApiService.stream: HTTP <status> — <body>` instead. |

## Notes and limits

- `enriched_record` is whatever the model returns; the code does not merge it with the input record or check field types. The `context` input is ignored.
- The worker returns `raw` next to `data`; the engine keeps only `data` as the node output, so `raw` is not available downstream.
- The model call goes through the Sommatic AI backend, which resolves the provider record named by `llm_provider_id` (endpoint, credentials, deployment); the orchestrator never holds provider keys. `invocation_mode: "sync"` (default) waits for the full answer; `"stream"` reads it as server-sent events and joins the chunks.
- `timeout_ms` only takes effect in stream mode, as the idle time allowed between chunks (default 600000). In sync mode the value is logged but not applied to the HTTP request, so the platform's default request timeout governs.
- In stream mode the backend endpoint does not apply sampling settings, so the fixed `temperature: 0.1` only takes effect in sync mode.
- The knowledge resource is read from the backend on every run (10 s timeout). A resource whose status is not `active` is used anyway, with a warning in the log.
- No automatic retries: the engine does not honour `execution.retryOnFail`, `maxTries` or `waitBetweenTries` for this node; a failed call goes straight to `error`.


## Contracts (JSON Schema)

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

```json
{
  "slug": "ai.enrich",
  "name": "Enrich Data",
  "category": "ai",
  "version": "1.0.0",
  "description": "Enrich records by adding computed, inferred, or standardized fields using an LLM.\n\n  Select an enrichment mode and define the output fields you need. The operator reads the input record, applies the chosen strategy, and returns the enriched record with confidence scores per field.\n\n  **Operations:**\n  - **Categorize:** Add category tags to a record based on its content.\n  - **Standardize:** Normalize names, addresses, dates, and phone numbers to consistent formats.\n  - **Infer:** Fill missing field values by reasoning from available context.\n  - **Augment:** Add computed fields such as seniority from job title or industry from company name.",
  "capabilities": [
    "ai",
    "enrichment",
    "data-quality",
    "augmentation"
  ],
  "config_schema": {
    "type": "object",
    "required": [
      "enrichment_mode",
      "output_fields"
    ],
    "properties": {
      "llm_provider_id": {
        "type": "string",
        "title": "LLM Provider",
        "description": "The `id` of a `LlmProviderModel` record (e.g. `wf-llm-provider-*`). The orchestrator delegates to the backend: `POST /cognitive-infrastructure/cognitive-assets/llm-provider/invoke` for sync mode, or `POST /chat/conversation/execute/stream-ephemeral` (SSE) for stream mode. The backend resolves endpoint + api-key + deployment from the record and routes to the correct adapter.",
        "x-ui-widget": "llm-provider-picker",
        "x-llm-provider-capabilities": [
          "chat"
        ]
      },
      "invocation_mode": {
        "type": "string",
        "title": "Invocation Mode",
        "description": "Transport for the LLM call. `sync` blocks until the full response arrives. `stream` consumes tokens via SSE (required for long generations). Default `sync`.",
        "enum": [
          "sync",
          "stream"
        ],
        "default": "sync"
      },
      "timeout_ms": {
        "type": "integer",
        "title": "Timeout (ms)",
        "description": "Maximum time to wait for the invocation to complete (or idle timeout between chunks in stream mode). Default 30000 for sync, 600000 for stream.",
        "minimum": 1000
      },
      "enrichment_mode": {
        "type": "string",
        "title": "Enrichment Mode",
        "enum": [
          "categorize",
          "standardize",
          "infer",
          "augment"
        ],
        "default": "categorize",
        "description": "Strategy to apply: categorize, standardize, infer missing values, or augment with computed fields."
      },
      "input_fields": {
        "type": "array",
        "title": "Input Fields",
        "items": {
          "type": "string"
        },
        "description": "Which fields from the input record to read."
      },
      "output_fields": {
        "type": "array",
        "title": "Output Fields",
        "description": "Fields to add or compute in the enriched record.",
        "items": {
          "type": "object",
          "properties": {
            "name": {
              "type": "string",
              "description": "Field name to produce."
            },
            "description": {
              "type": "string",
              "description": "What this field represents."
            },
            "type": {
              "type": "string",
              "description": "Expected data type (string, number, boolean, array)."
            }
          }
        }
      },
      "knowledge_resource_id": {
        "type": "string",
        "title": "Knowledge Resource",
        "description": "Optional: reference data or lookup tables for enrichment.",
        "ui_widget": "knowledge_resource_picker"
      },
      "language": {
        "type": "string",
        "title": "Language",
        "default": "en",
        "description": "Language of the input data."
      },
      "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": {
      "record": {
        "type": "object",
        "description": "The data record to enrich."
      },
      "context": {
        "type": "string",
        "description": "Optional context to improve enrichment accuracy."
      }
    },
    "required": [
      "record"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "enriched_record": {
        "type": "object",
        "description": "Original record merged with new fields."
      },
      "added_fields": {
        "type": "array",
        "description": "List of field names successfully added."
      },
      "confidence_per_field": {
        "type": "object",
        "description": "Confidence score (0-1) for each added field."
      }
    }
  },
  "ports": [
    {
      "port_id": "success",
      "label": "Enriched",
      "description": "All requested fields were added successfully.",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "partial",
      "label": "Partial",
      "description": "Some requested fields could not be computed.",
      "is_default": false,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Enrichment failed.",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "tags": [
    "ai",
    "enrichment",
    "data-quality",
    "augmentation"
  ],
  "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",
  "required": [
    "enrichment_mode",
    "output_fields"
  ],
  "properties": {
    "llm_provider_id": {
      "type": "string",
      "title": "LLM Provider",
      "description": "The `id` of a `LlmProviderModel` record (e.g. `wf-llm-provider-*`). The orchestrator delegates to the backend: `POST /cognitive-infrastructure/cognitive-assets/llm-provider/invoke` for sync mode, or `POST /chat/conversation/execute/stream-ephemeral` (SSE) for stream mode. The backend resolves endpoint + api-key + deployment from the record and routes to the correct adapter.",
      "x-ui-widget": "llm-provider-picker",
      "x-llm-provider-capabilities": [
        "chat"
      ]
    },
    "invocation_mode": {
      "type": "string",
      "title": "Invocation Mode",
      "description": "Transport for the LLM call. `sync` blocks until the full response arrives. `stream` consumes tokens via SSE (required for long generations). Default `sync`.",
      "enum": [
        "sync",
        "stream"
      ],
      "default": "sync"
    },
    "timeout_ms": {
      "type": "integer",
      "title": "Timeout (ms)",
      "description": "Maximum time to wait for the invocation to complete (or idle timeout between chunks in stream mode). Default 30000 for sync, 600000 for stream.",
      "minimum": 1000
    },
    "enrichment_mode": {
      "type": "string",
      "title": "Enrichment Mode",
      "enum": [
        "categorize",
        "standardize",
        "infer",
        "augment"
      ],
      "default": "categorize",
      "description": "Strategy to apply: categorize, standardize, infer missing values, or augment with computed fields."
    },
    "input_fields": {
      "type": "array",
      "title": "Input Fields",
      "items": {
        "type": "string"
      },
      "description": "Which fields from the input record to read."
    },
    "output_fields": {
      "type": "array",
      "title": "Output Fields",
      "description": "Fields to add or compute in the enriched record.",
      "items": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string",
            "description": "Field name to produce."
          },
          "description": {
            "type": "string",
            "description": "What this field represents."
          },
          "type": {
            "type": "string",
            "description": "Expected data type (string, number, boolean, array)."
          }
        }
      }
    },
    "knowledge_resource_id": {
      "type": "string",
      "title": "Knowledge Resource",
      "description": "Optional: reference data or lookup tables for enrichment.",
      "ui_widget": "knowledge_resource_picker"
    },
    "language": {
      "type": "string",
      "title": "Language",
      "default": "en",
      "description": "Language of the input data."
    },
    "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": {
    "record": {
      "type": "object",
      "description": "The data record to enrich."
    },
    "context": {
      "type": "string",
      "description": "Optional context to improve enrichment accuracy."
    }
  },
  "required": [
    "record"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "enriched_record": {
      "type": "object",
      "description": "Original record merged with new fields."
    },
    "added_fields": {
      "type": "array",
      "description": "List of field names successfully added."
    },
    "confidence_per_field": {
      "type": "object",
      "description": "Confidence score (0-1) for each added field."
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "success",
    "label": "Enriched",
    "description": "All requested fields were added successfully.",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "partial",
    "label": "Partial",
    "description": "Some requested fields could not be computed.",
    "is_default": false,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Enrichment failed.",
    "is_default": false,
    "supports_condition": false
  }
]
```
