> ## Documentation Index
> Fetch the complete documentation index at: https://ladygraph.io.vn/llms.txt
> Use this file to discover all available pages before exploring further.

# Translation and verification

> Translate with positions, escalate to a stronger model, and check the result.

A tagger works best in one language, but ladyGraph's rule is that evidence must be a verbatim span of the original text. The `translation` module lets you translate first and still point back. It cuts the original into lines with their positions, translates them as numbered lines, and keeps the numbering, so line 2 of the translation is line 2 of the original.

Three more parts make that safe and cheap: escalation (a cheap model first, a stronger one only when a check fails), verification (a second model reports errors), and term hints from a [glossary](https://ladygraph.io.vn/guides/prompt-and-glossary.md).

No part imports a model SDK. A model is a function `ask(prompt) -> text`, or an `LlmClient`. The wording is data.

| Module | What it does | Where it plugs in |
| - | - | - |
| `translation/aligned.py` | Cuts a passage into segments with offsets, translates them as numbered lines in one call, and refills what is missing. | Anywhere. It takes a function. |
| `translation/escalation.py` | A cheap step first, a check, and stronger steps only for what fails. A later answer replaces the current one only if it has no more problems. | Anywhere. A `before` hook per step can add a term search. |
| `translation/verify.py` | A second model reports errors in (source, translation) pairs. A verdict that does not echo its source is not trusted. | An escalation check (`verifier_check`), or called directly. |
| `extractors/translate_ner.py` | Tags the translation and maps each find back to a verbatim quote of the original. | `extractor.kind: translate_ner` |

## Aligned translation

```python theme={null}
from ladygraph.translation import AlignedConfig, AlignedTranslator

config = AlignedConfig(source_language="Spanish", target_language="English", domain="policy text")
result = AlignedTranslator(ask, config).translate(passage)

result.translations     # one English line per original line
result.segments         # each line's start and length in the original
result.numbers_ok       # per line: do the numbers match the original?
result.first_try_ok     # was the numbered reply complete on the first try?
result.refilled         # how many lines were asked again, one by one
```

If the model forgets a line, that line is translated again on its own. A small model may not follow the numbered format. The missing lines are translated one by one, and that is counted in `extractor.stats`.

The offsets index the passage as ladyGraph reads it, so an original span found through line `n` can become evidence. Lesson 7 (`hello_aligned.py`) runs this on a toy translator that forgets a line.

> [!WARNING]
>
> Passing the number check does not make a translation right. It catches only the most expensive slip.

## Escalation

An escalation is a list of steps and a check. A later step's answer replaces the current one only if it has no more problems, so escalating can never make a result worse by the check's own count.

```python theme={null}
from ladygraph.translation import Step, escalate, make_check

check = make_check()        # an empty answer, or numbers that differ from the source's
steps = [Step("local", lambda src, hint: small(src + hint)),
         Step("big",   lambda src, hint: large(src + hint))]
outcome = escalate(source_line, steps, check)
# outcome.text, outcome.route ("first" | "escalated" | "first_kept"), outcome.attempts
```

* `make_check(glossary=None, extra=())` is the basic check, plus the glossary gate when you pass a `Glossary`, plus any checks of your own. The gate was written for one language pair, so pass a glossary only when it fits yours.
* A `Step` can have a `before` hook that returns a hint, for example a term-search result for a name the small model got wrong. The search itself stays outside the runtime, as the term-list builder does.
* Lesson 7 (`hello_escalate.py`) shows four cases: escalate, do not escalate, a stronger step that does no better is not kept, and a step that adds a search result to the hint.

## Verification

A second model reports errors in (id, source, translation) pairs.

```python theme={null}
from ladygraph.translation import LlmVerifier, verifier_check

checker = LlmVerifier(llm, source_language="Spanish", target_language="English", domain="policy text")
verdicts = checker.verify([("c1", "El límite es 5 por persona.", "The limit is 6 per person.")])
```

Each `Verdict` has an `id`, a `verdict` (`ok`, `minor`, `major` or `unchecked`), and a list of errors. An error has one of these types: `untranslated`, `name_mistranslated`, `wrong_term`, `number_dropped_or_changed`, `meaning_changed`, `added_text` or `missing_text`.

`verifier_check(verifier, fail_on=("major",))` turns a verdict into an escalation check.

> [!NOTE]
>
> A checker is a report. A verdict never edits a translation and never marks a concept verified. "Claude as the verifier" means a Claude model served by a gateway you run. ladyGraph does not call Claude directly, and it does not run Claude Code for this.

A verdict that does not echo the first characters of its source is not trusted, so a confused model that attaches a verdict to the wrong pair is marked `unchecked`.

## Tag a translation, ground in the original

`extractor.kind: translate_ner` translates the passage, tags the translation, and maps each find back to a verbatim quote of the original. See [Extractors](https://ladygraph.io.vn/guides/extractors.md) for the config.

* **The evidence rule does not change.** The concept's word is in the tagger's language, but its evidence is a quote found in the original. The back-quote is accepted only if it is in the segment and occurs once in the passage. In the measured document, 65% of exact spans repeated within a passage, and a repeated quote could point at the wrong copy. Otherwise the whole segment is the evidence.
* **Plain text for a local model.** The numbered translation and the back-quote use `complete_text`, with no JSON wrapper. Wrapped in a schema, the E2B model answered `""` for every back-quote, and one numbered answer was cut before its closing brace.

> [accordion] What was measured, and where it still fails
>
> Measured once, on 3 real chunks with the real local models (Gemma E2B translates, quotes and places, and GLiNER tags; 9 segments):
>
> * The numbered translation was valid on all 3 first tries, with no line re-translated.
> * 16 concepts were found. Every evidence span matched its fingerprint, and the contribution validated.
> * 14 of 16 quotes were exact words of the original. 2 were refused as ambiguous, so the whole segment was used.
> * It took 172 s and 35 model calls, almost all of it the local model and not the tagger.
>
> Correctness was not judged beyond a read-through. Some quotes were the right segment but partly the wrong words: a concept "Board of Directors" quoted the words for the Executive Board. The small tagger produced generic concepts such as "plan" and "business". Treat it as a working path with measured plumbing, not as measured quality.
>
> It can still fail when the quote is the wrong words in the right segment, when a translation passes the number check and is still wrong, or when a small model does not follow the numbered format.

## Compare a local model with Claude

Lesson 7 has an experiment, `hello_compare.py`, that translates the same passage with a local model and with a model behind a gateway, and shows the result line by line with each line's number check. Both sides use the same `AlignedTranslator`, prompts and passage, so a difference comes from the model.

It runs on two stand-ins by default. To use a real local model and Claude, you need a LiteLLM gateway and the variables below. Claude goes through the gateway, never directly.

```bash theme={null}
# terminal 1: the gateway (needs litellm[proxy], the claude CLI for the claude-cli alias)
bash ladygraph/tutorial/07_translation/liteLLMExample/run_litellm.sh

# terminal 2, from ladyGraph/
CLAUDE_GATEWAY_URL=http://localhost:4000/v1 CLAUDE_MODEL=claude-cli \
[LITERT_LLM_MODEL=/path/gemma.litertlm LITERT_LLM_DEVICE=cpu] \
PYTHONPATH=. python ladygraph/tutorial/07_translation/hello_compare.py
```

* `CLAUDE_MODEL` is the alias the gateway serves, such as `claude-cli` in the example, not an Anthropic model id.
* The gateway key is `LITELLM_MASTER_KEY`. The proxy script and `hello_compare.py` both read it, and both default to `sk-anything`.
* The `claude-cli` alias starts a new `claude -p` process for every call, so it is slow, and each call is capped at \$1.
* The number check only catches changed numbers. Read lines that differ against the original yourself.

See [Local models](https://ladygraph.io.vn/guides/local-models.md) for the device setting.

> [!NOTE]
>
> Lesson 7 has its own dependency table in its `BRIEF.md`. A real run needs `google-adk`, `litellm[proxy]` and `litert-lm-api`, which `docs/requirements.txt` installs.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.