# Extract Entities

> Extract named entities from text using LLM-powered recognition.

Source: https://docs.sommatic.ai/en/nodes/ai.entity.extract/

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

Extract named entities from text using LLM-powered recognition.

Identify people, organizations, locations, dates, monetary amounts, and custom entity types from any unstructured text. Configure entity types or provide your own definitions via Knowledge Resources.

**Operations:**
- **Standard:** Extract common entity types (PERSON, ORG, LOCATION, DATE, MONEY, EMAIL, PHONE, URL).
- **Custom:** Define your own entity types with descriptions and examples.
- **All:** Combine standard and custom entity extraction.

## 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.
- `extraction_mode` (string, required): Standard extracts common types. Custom uses your definitions. All combines both. Default: `standard`. Allowed values: `standard`, `custom`, `all`.
- `entity_types` (array<string>, optional): Filter which standard types to extract. Leave empty for all. Shown when: extraction_mode = `standard` | `all`.
- `custom_entities` (array<object>, optional): Define custom entity types with name, description, and examples. Shown when: extraction_mode = `custom` | `all`.
- `knowledge_resource_id` (string, optional): Optional: load custom entity definitions from a Knowledge Resource.
- `language` (string, optional): Language of the input text. Default: `en`.
- `deduplicate` (boolean, optional): Merge duplicate entity mentions into unique entries. 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

- `success` (default): At least one entity was extracted.
- `none_found`: No entities detected in the text.
- `error`: Extraction failed.

## Input contract

- `text` (string, required): Text to extract entities from.
- `context` (string, optional): Optional context to improve extraction accuracy.

## Output contract

- `entities` (array, optional): Extracted entities with type, text, and confidence.
- `entity_counts` (object, optional): Count per entity type.
- `unique_entities` (object, optional): Deduplicated entities grouped by type.

## In expressions

What a later node can read from this node's output with `$("Extract Entities").item.json.<path>` (write your node's label instead of `Extract Entities`). **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.

- `$("Extract Entities").item.json.entities` (array) = `[…] (3)`: Extracted entities with type, text, and confidence.
- `$("Extract Entities").item.json.entity_counts` (object) = `{…}`: Count per entity type.
- `$("Extract Entities").item.json.unique_entities` (object) = `{…}`: Deduplicated entities grouped by type.
- `$("Extract Entities").item.json.entity_counts.PERSON` (number) = `2` (Not suggested by the editor)
- `$("Extract Entities").item.json.entity_counts.EMAIL` (number) = `1` (Not suggested by the editor)
- `$("Extract Entities").item.json.unique_entities.PERSON` (array) = `[…] (1)` (Not suggested by the editor)
- `$("Extract Entities").item.json.unique_entities.EMAIL` (array) = `[…] (1)` (Not suggested by the editor)
- `$("Extract Entities").item.json.total` (number) = `3` (Not suggested by the editor)
- `$("Extract Entities").item.json.unique_total` (number) = `2` (Not suggested by the editor)


## How it works

Extracts named entities of standard and/or custom types from `text` in one model call, filters unknown types and groups duplicate mentions.

1. Merges the upstream payload with the node config (config wins), reads `text` (falling back to `input`) and `context`, and defaults `invocation_mode` to `stream` when it is not set.
2. Builds the type list by `extraction_mode`: `standard` uses PERSON, ORG, LOCATION, DATE, MONEY, EMAIL, PHONE, URL and ID (filtered by `entity_types`); `custom` uses `custom_entities` plus `structured_data.entity_types` of `knowledge_resource_id`; `all` uses both. Fails when the list is empty.
3. Sends a system prompt listing the types and a strict JSON format; the user message is `text`, prefixed by `Context:` when set; `temperature: 0`.
4. Parses the answer as JSON; `entities` must be an array. Drops entities whose type is not in the list (case-insensitive).
5. With `deduplicate` (default true), groups mentions by type and normalised text, counting `mentions` and keeping the highest confidence. Routes to `success` when at least one entity remains, else `none_found`.

## In a workflow

### Node

```json
{
  "node_id": "node-extract-contacts",
  "label": "Extract contacts",
  "operator_slug": "ai.entity.extract",
  "operator_version": "1.0.0",
  "config": {
    "text": "{{ $json.body }}",
    "extraction_mode": "standard",
    "entity_types": [
      "PERSON",
      "EMAIL",
      "PHONE"
    ],
    "deduplicate": true,
    "language": "en",
    "llm_provider_id": "wf-llm-provider-xxxx",
    "invocation_mode": "stream",
    "timeout_ms": 120000
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "entities": [
      {
        "text": "Ana Ruiz",
        "type": "PERSON",
        "normalized": "Ana Ruiz",
        "confidence": 0.98
      },
      {
        "text": "ana.ruiz@example.com",
        "type": "EMAIL",
        "normalized": "ana.ruiz@example.com",
        "confidence": 0.99
      },
      {
        "text": "Ms. Ruiz",
        "type": "PERSON",
        "normalized": "Ana Ruiz",
        "confidence": 0.9
      }
    ],
    "entity_counts": {
      "PERSON": 2,
      "EMAIL": 1
    },
    "unique_entities": {
      "PERSON": [
        {
          "text": "Ana Ruiz",
          "type": "PERSON",
          "normalized": "Ana Ruiz",
          "confidence": 0.98,
          "mentions": 2
        }
      ],
      "EMAIL": [
        {
          "text": "ana.ruiz@example.com",
          "type": "EMAIL",
          "normalized": "ana.ruiz@example.com",
          "confidence": 0.99,
          "mentions": 1
        }
      ]
    },
    "total": 3,
    "unique_total": 2
  },
  "port": "success"
}
```

## Output ports (behaviour in the code)

### `success`

At least one entity of a configured type was found.

```json
{
  "entities": [
    {
      "text": "Ana Ruiz",
      "type": "PERSON",
      "normalized": "Ana Ruiz",
      "confidence": 0.98
    },
    {
      "text": "ana.ruiz@example.com",
      "type": "EMAIL",
      "normalized": "ana.ruiz@example.com",
      "confidence": 0.99
    },
    {
      "text": "Ms. Ruiz",
      "type": "PERSON",
      "normalized": "Ana Ruiz",
      "confidence": 0.9
    }
  ],
  "entity_counts": {
    "PERSON": 2,
    "EMAIL": 1
  },
  "unique_entities": {
    "PERSON": [
      {
        "text": "Ana Ruiz",
        "type": "PERSON",
        "normalized": "Ana Ruiz",
        "confidence": 0.98,
        "mentions": 2
      }
    ],
    "EMAIL": [
      {
        "text": "ana.ruiz@example.com",
        "type": "EMAIL",
        "normalized": "ana.ruiz@example.com",
        "confidence": 0.99,
        "mentions": 1
      }
    ]
  },
  "total": 3,
  "unique_total": 2
}
```

### `none_found`

The model found no entity of a configured type.

```json
{
  "entities": [],
  "entity_counts": {},
  "unique_entities": {},
  "total": 0,
  "unique_total": 0
}
```

### `error`

No entity types are configured, the model call failed or the answer had no `entities` array.

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

## Behaviour in the code

- `invocation_mode`: Defaults to `stream` in code, not `sync` as the schema says, so long texts do not hit the sync request timeout. Set `sync` explicitly to opt out.
- `knowledge_resource_id`: Read only when `extraction_mode` is `custom` or `all`; a failed fetch is only logged.
- `deduplicate`: With `false`, `unique_entities` is `null` and `unique_total` is 0; `entities` always keeps every mention.
- `text, context`: Not in the config schema (it is in the input contract), but read from the config first and then from the upstream payload.
- `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 |
| --- | --- | --- |
| `No entity types defined. Configure standard types, custom entities, or a Knowledge Resource.` | `error` | For example `extraction_mode: "custom"` without `custom_entities` and without a resource, or an `entity_types` filter that matches no standard type. |
| `Model did not return valid entity extraction output.` | `error` | The answer was not JSON or `entities` was not an array. |
| `[AiEntityExtract] 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

- When `text` is empty, the worker falls back to `input`, which at runtime is always the whole upstream payload object. The missing-text check therefore never fires and the object is sent to the model as the message. Always map `text` explicitly.
- Because stream mode is the default, `temperature: 0` is usually not applied and `raw` is not available.
- 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.
- 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.entity.extract",
  "name": "Extract Entities",
  "category": "ai",
  "version": "1.0.0",
  "description": "Extract named entities from text using LLM-powered recognition.\n\n  Identify people, organizations, locations, dates, monetary amounts, and custom entity types from any unstructured text. Configure entity types or provide your own definitions via Knowledge Resources.\n\n  **Operations:**\n  - **Standard:** Extract common entity types (PERSON, ORG, LOCATION, DATE, MONEY, EMAIL, PHONE, URL).\n  - **Custom:** Define your own entity types with descriptions and examples.\n  - **All:** Combine standard and custom entity extraction.",
  "capabilities": [
    "nlp",
    "ner",
    "extraction",
    "entities"
  ],
  "config_schema": {
    "type": "object",
    "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
      },
      "extraction_mode": {
        "type": "string",
        "title": "Extraction Mode",
        "enum": [
          "standard",
          "custom",
          "all"
        ],
        "default": "standard",
        "description": "Standard extracts common types. Custom uses your definitions. All combines both."
      },
      "entity_types": {
        "type": "array",
        "title": "Entity Types (Standard)",
        "description": "Filter which standard types to extract. Leave empty for all.",
        "items": {
          "type": "string"
        },
        "displayOptions": {
          "show": {
            "extraction_mode": [
              "standard",
              "all"
            ]
          }
        }
      },
      "custom_entities": {
        "type": "array",
        "title": "Custom Entity Definitions",
        "description": "Define custom entity types with name, description, and examples.",
        "items": {
          "type": "object",
          "properties": {
            "type": {
              "type": "string",
              "description": "Entity type name (e.g., PRODUCT, DISEASE)"
            },
            "description": {
              "type": "string",
              "description": "What this entity type represents"
            },
            "examples": {
              "type": "string",
              "description": "Comma-separated examples"
            }
          }
        },
        "displayOptions": {
          "show": {
            "extraction_mode": [
              "custom",
              "all"
            ]
          }
        }
      },
      "knowledge_resource_id": {
        "type": "string",
        "title": "Knowledge Resource",
        "description": "Optional: load custom entity definitions from a Knowledge Resource.",
        "ui_widget": "knowledge_resource_picker"
      },
      "language": {
        "type": "string",
        "title": "Language",
        "default": "en",
        "description": "Language of the input text."
      },
      "deduplicate": {
        "type": "boolean",
        "title": "Deduplicate",
        "default": true,
        "description": "Merge duplicate entity mentions into unique entries."
      },
      "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
          }
        }
      }
    },
    "required": [
      "extraction_mode"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "text": {
        "type": "string",
        "description": "Text to extract entities from."
      },
      "context": {
        "type": "string",
        "description": "Optional context to improve extraction accuracy."
      }
    },
    "required": [
      "text"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "entities": {
        "type": "array",
        "description": "Extracted entities with type, text, and confidence."
      },
      "entity_counts": {
        "type": "object",
        "description": "Count per entity type."
      },
      "unique_entities": {
        "type": "object",
        "description": "Deduplicated entities grouped by type."
      }
    }
  },
  "ports": [
    {
      "port_id": "success",
      "label": "Entities Found",
      "description": "At least one entity was extracted.",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "none_found",
      "label": "No Entities",
      "description": "No entities detected in the text.",
      "is_default": false,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Extraction failed.",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "tags": [
    "ai",
    "nlp",
    "ner",
    "entities",
    "extraction"
  ],
  "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": {
    "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
    },
    "extraction_mode": {
      "type": "string",
      "title": "Extraction Mode",
      "enum": [
        "standard",
        "custom",
        "all"
      ],
      "default": "standard",
      "description": "Standard extracts common types. Custom uses your definitions. All combines both."
    },
    "entity_types": {
      "type": "array",
      "title": "Entity Types (Standard)",
      "description": "Filter which standard types to extract. Leave empty for all.",
      "items": {
        "type": "string"
      },
      "displayOptions": {
        "show": {
          "extraction_mode": [
            "standard",
            "all"
          ]
        }
      }
    },
    "custom_entities": {
      "type": "array",
      "title": "Custom Entity Definitions",
      "description": "Define custom entity types with name, description, and examples.",
      "items": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "description": "Entity type name (e.g., PRODUCT, DISEASE)"
          },
          "description": {
            "type": "string",
            "description": "What this entity type represents"
          },
          "examples": {
            "type": "string",
            "description": "Comma-separated examples"
          }
        }
      },
      "displayOptions": {
        "show": {
          "extraction_mode": [
            "custom",
            "all"
          ]
        }
      }
    },
    "knowledge_resource_id": {
      "type": "string",
      "title": "Knowledge Resource",
      "description": "Optional: load custom entity definitions from a Knowledge Resource.",
      "ui_widget": "knowledge_resource_picker"
    },
    "language": {
      "type": "string",
      "title": "Language",
      "default": "en",
      "description": "Language of the input text."
    },
    "deduplicate": {
      "type": "boolean",
      "title": "Deduplicate",
      "default": true,
      "description": "Merge duplicate entity mentions into unique entries."
    },
    "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
        }
      }
    }
  },
  "required": [
    "extraction_mode"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "text": {
      "type": "string",
      "description": "Text to extract entities from."
    },
    "context": {
      "type": "string",
      "description": "Optional context to improve extraction accuracy."
    }
  },
  "required": [
    "text"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "entities": {
      "type": "array",
      "description": "Extracted entities with type, text, and confidence."
    },
    "entity_counts": {
      "type": "object",
      "description": "Count per entity type."
    },
    "unique_entities": {
      "type": "object",
      "description": "Deduplicated entities grouped by type."
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "success",
    "label": "Entities Found",
    "description": "At least one entity was extracted.",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "none_found",
    "label": "No Entities",
    "description": "No entities detected in the text.",
    "is_default": false,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Extraction failed.",
    "is_default": false,
    "supports_condition": false
  }
]
```
