# Translate Text

> Translate text between languages using LLM-powered translation with domain awareness and glossary support.

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

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

Translate text between languages using LLM-powered translation with domain awareness and glossary support.

Supports formal, neutral, and informal registers across general, legal, medical, technical, business, and academic domains. Enforce term consistency with custom glossaries and preserve original formatting.

**Operations:**
- **Translation:** Accurate cross-language translation with tone and domain context.
- **Auto-detection:** Automatically detect source language when not specified.
- **Glossary enforcement:** Apply mandatory term mappings for consistency.
- **Formality control:** Adjust output register (formal, neutral, informal).

## 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.
- `source_language` (string, optional): Leave empty for auto-detection.
- `target_language` (string, required): Target language code or name (e.g., "es", "French").
- `formality` (string, optional): Register level for the translated output. Default: `neutral`. Allowed values: `formal`, `neutral`, `informal`.
- `domain` (string, optional): Subject domain for terminology accuracy. Default: `general`. Allowed values: `general`, `legal`, `medical`, `technical`, `business`, `academic`.
- `glossary` (array<object>, optional): Term mappings to enforce during translation.
- `preserve_formatting` (boolean, optional): Keep original paragraphs, lists, and line breaks. 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): Translation completed successfully.
- `error`: Translation failed.

## Input contract

- `text` (string, required): Text to translate.

## Output contract

- `translated_text` (string, optional): The translated content.
- `source_language` (string, optional): Source language (provided or detected).
- `target_language` (string, optional): Target language.
- `detected_source_language` (string, optional): Detected source language when auto-detection was used.

## In expressions

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

- `$("Translate Text").item.json.translated_text` (string) = `"Tu pedido saldrá el 3 de diciembre."`: The translated content.
- `$("Translate Text").item.json.source_language` (string) = `"en"`: Source language (provided or detected).
- `$("Translate Text").item.json.target_language` (string) = `"es"`: Target language.
- `$("Translate Text").item.json.detected_source_language` (string) = `"en"`: Detected source language when auto-detection was used.


## How it works

Translates `text` into `target_language` with the LLM, honoring formality, subject domain, a glossary and the original formatting. Without `source_language` the model detects it.

1. Resolves the node config: expressions in config keys are evaluated against the run; the incoming item is used as-is.
2. Merges the incoming item with the resolved config (config keys win) and reads the inputs from the result.
3. Requires `text` and `target_language`.
4. Builds the prompt with `domain` (when not `general`), `formality` (default `neutral`), the glossary entries that have both `source` and `target`, and the formatting rule (`preserve_formatting`, default `true`).
5. Calls the LLM through the backend with the `llm_provider_id` record. `invocation_mode: "sync"` (default) waits for the full answer; `"stream"` reads it over SSE and joins the chunks.
6. Parses the JSON answer (temperature 0.2); `translated_text` must be non-empty. Adds `detected_source_language` when no `source_language` was given. Routes to `success`.

## In a workflow

### Node

```json
{
  "node_id": "node-translate",
  "label": "Translate to Spanish",
  "operator_slug": "ai.translate",
  "operator_version": "1.0.0",
  "config": {
    "llm_provider_id": "<llm_provider_id>",
    "source_language": "",
    "target_language": "es",
    "formality": "neutral",
    "domain": "business",
    "glossary": [
      {
        "source": "order",
        "target": "pedido"
      }
    ],
    "preserve_formatting": true
  }
}
```

### Envelope

```json
{
  "ok": true,
  "port": "success",
  "data": {
    "translated_text": "Tu pedido saldrá el 3 de diciembre.",
    "source_language": "en",
    "target_language": "es",
    "detected_source_language": "en"
  }
}
```

## Output ports (behaviour in the code)

### `success`

The model returned a non-empty `translated_text`. `detected_source_language` is present only when `source_language` was empty.

```json
{
  "translated_text": "Tu pedido saldrá el 3 de diciembre.",
  "source_language": "en",
  "target_language": "es",
  "detected_source_language": "en"
}
```

### `error`

Validation, the LLM call or parsing failed. `data` is `{ error }` (plus `response` when the backend returned a body).

```json
{
  "error": "Missing target_language configuration."
}
```

## Behaviour in the code

- `llm_provider_id`: Required at runtime although the schema does not mark it as required. It is the id of an LLM provider record; the backend resolves endpoint, deployment and credentials from it.
- `target_language / source_language`: Written into the prompt as given: a code (`es`) or a name (`French`) both work. They are not validated.
- `glossary`: Entries without `source` or `target` are dropped. The glossary is a prompt instruction; the output is not checked against it.
- `timeout_ms`: `timeout_ms` only takes effect in `stream` mode, as the idle time allowed between chunks (default 600000). In `sync` mode it is logged but the backend request has no timeout.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `Missing input text for translation.` | `error` | The incoming item has no `text`. |
| `Missing target_language configuration.` | `error` | `target_language` is empty. |
| `Model did not return valid translation output.` | `error` | The answer had no JSON object with a non-empty `translated_text`. |
| `[AiTranslate] Missing 'llm_provider_id' in node config (LlmProviderModel.id, e.g. 'wf-llm-provider-*'). …` | `error` | The node has no `llm_provider_id`. Pick an LLM provider in the node config; the legacy `model_id` key is not accepted. |
| `LLM Invocation Error: <backend message>` | `error` | The backend rejected the call (unknown or inactive provider record, provider error, credentials). Check the provider record. |
| `LLM Invocation Error: empty response from backend (<reason>).` | `error` | The backend could not be reached or returned nothing in `sync` mode. |
| `RemoteApiService.stream: idle timeout after <timeout_ms>ms` | `error` | In `stream` mode no chunk arrived within `timeout_ms`. |

## Notes and limits

- The model answer is parsed as JSON after removing Markdown code fences; if that fails, the text between the first `{` and the last `}` is parsed.
- Input fields are read from the incoming item (`$json`). A key with the same name in the node config, which may be an expression, takes precedence over the input.
- The worker also returns the provider response as `raw` outside `data`; the engine does not keep it.
- On success the worker returns `port` and `data` without an `ok` field; the engine routes on `port` only.
- `timeout_ms` only takes effect in `stream` mode, as the idle time allowed between chunks (default 600000). In `sync` mode it is logged but the backend request has no timeout.
- In `stream` mode the temperature this node sets is not forwarded to the provider, and `raw` is `null`.
- The engine does not apply `execution.retryOnFail`, `maxTries`, `waitBetweenTries` or `continueOnFail` to this node. Handle failures by wiring the `error` port.


## Contracts (JSON Schema)

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

```json
{
  "slug": "ai.translate",
  "name": "Translate Text",
  "category": "ai",
  "version": "1.0.0",
  "description": "Translate text between languages using LLM-powered translation with domain awareness and glossary support.\n\n  Supports formal, neutral, and informal registers across general, legal, medical, technical, business, and academic domains. Enforce term consistency with custom glossaries and preserve original formatting.\n\n  **Operations:**\n  - **Translation:** Accurate cross-language translation with tone and domain context.\n  - **Auto-detection:** Automatically detect source language when not specified.\n  - **Glossary enforcement:** Apply mandatory term mappings for consistency.\n  - **Formality control:** Adjust output register (formal, neutral, informal).",
  "capabilities": [
    "ai",
    "translation",
    "nlp",
    "localization"
  ],
  "config_schema": {
    "type": "object",
    "required": [
      "target_language"
    ],
    "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
      },
      "source_language": {
        "type": "string",
        "title": "Source Language",
        "default": "",
        "description": "Leave empty for auto-detection."
      },
      "target_language": {
        "type": "string",
        "title": "Target Language",
        "description": "Target language code or name (e.g., \"es\", \"French\")."
      },
      "formality": {
        "type": "string",
        "title": "Formality",
        "enum": [
          "formal",
          "neutral",
          "informal"
        ],
        "default": "neutral",
        "description": "Register level for the translated output."
      },
      "domain": {
        "type": "string",
        "title": "Domain",
        "enum": [
          "general",
          "legal",
          "medical",
          "technical",
          "business",
          "academic"
        ],
        "default": "general",
        "description": "Subject domain for terminology accuracy."
      },
      "glossary": {
        "type": "array",
        "title": "Glossary",
        "description": "Term mappings to enforce during translation.",
        "items": {
          "type": "object",
          "properties": {
            "source": {
              "type": "string",
              "description": "Source term"
            },
            "target": {
              "type": "string",
              "description": "Required translation"
            }
          }
        }
      },
      "preserve_formatting": {
        "type": "boolean",
        "title": "Preserve Formatting",
        "default": true,
        "description": "Keep original paragraphs, lists, and line breaks."
      },
      "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": {
      "text": {
        "type": "string",
        "description": "Text to translate."
      }
    },
    "required": [
      "text"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "translated_text": {
        "type": "string",
        "description": "The translated content."
      },
      "source_language": {
        "type": "string",
        "description": "Source language (provided or detected)."
      },
      "target_language": {
        "type": "string",
        "description": "Target language."
      },
      "detected_source_language": {
        "type": "string",
        "description": "Detected source language when auto-detection was used."
      }
    }
  },
  "ports": [
    {
      "port_id": "success",
      "label": "Translated",
      "description": "Translation completed successfully.",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Translation failed.",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "tags": [
    "ai",
    "translation",
    "nlp",
    "localization",
    "i18n"
  ],
  "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": [
    "target_language"
  ],
  "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
    },
    "source_language": {
      "type": "string",
      "title": "Source Language",
      "default": "",
      "description": "Leave empty for auto-detection."
    },
    "target_language": {
      "type": "string",
      "title": "Target Language",
      "description": "Target language code or name (e.g., \"es\", \"French\")."
    },
    "formality": {
      "type": "string",
      "title": "Formality",
      "enum": [
        "formal",
        "neutral",
        "informal"
      ],
      "default": "neutral",
      "description": "Register level for the translated output."
    },
    "domain": {
      "type": "string",
      "title": "Domain",
      "enum": [
        "general",
        "legal",
        "medical",
        "technical",
        "business",
        "academic"
      ],
      "default": "general",
      "description": "Subject domain for terminology accuracy."
    },
    "glossary": {
      "type": "array",
      "title": "Glossary",
      "description": "Term mappings to enforce during translation.",
      "items": {
        "type": "object",
        "properties": {
          "source": {
            "type": "string",
            "description": "Source term"
          },
          "target": {
            "type": "string",
            "description": "Required translation"
          }
        }
      }
    },
    "preserve_formatting": {
      "type": "boolean",
      "title": "Preserve Formatting",
      "default": true,
      "description": "Keep original paragraphs, lists, and line breaks."
    },
    "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": {
    "text": {
      "type": "string",
      "description": "Text to translate."
    }
  },
  "required": [
    "text"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "translated_text": {
      "type": "string",
      "description": "The translated content."
    },
    "source_language": {
      "type": "string",
      "description": "Source language (provided or detected)."
    },
    "target_language": {
      "type": "string",
      "description": "Target language."
    },
    "detected_source_language": {
      "type": "string",
      "description": "Detected source language when auto-detection was used."
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "success",
    "label": "Translated",
    "description": "Translation completed successfully.",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Translation failed.",
    "is_default": false,
    "supports_condition": false
  }
]
```
