Skip to main content

fabricatio-translate

MIT License Python Versions PyPI Version PyPI Downloads

LLM-powered text translation capability for Fabricatio agents. Supports single and batch translation with chunked processing for large texts.

Installation

pip install fabricatio[translate]
# or
uv pip install fabricatio[translate]

Architecture

The package provides a single capability mixin (Translate) that layers into a Fabricatio Role:

Layer Class Purpose
Capability Translate Mixin that adds translate and translate_chunked methods
Config TranslateConfig Template selection (loaded from fabricatio.toml under [translate])

Usage

Mix Translate into a Role to get translation methods:

import asyncio
from fabricatio import Role
from fabricatio_translate.capabilities.translate import Translate

class TranslatorRole(Role, Translate):
    """Role with translation capability."""

async def main() -> None:
    role = TranslatorRole(name="translator")

    # Single text
    result = await role.translate("Hello, world.", target_language="fr")
    print(result)  # "Bonjour, le monde."

    # Batch translation
    results = await role.translate(["Hello", "Goodbye"], target_language="de")
    print(results)  # ["Hallo", "Auf Wiedersehen"]

    # Chunked translation for long text (splits by word count)
    long_text = "A very long paragraph..." * 1000
    result = await role.translate_chunked(long_text, target_language="ja", chunk_size=4000)

    # With custom specification
    result = await role.translate(
        "The quick brown fox",
        target_language="zh",
        specification="Use formal register. Preserve animal names.",
    )

asyncio.run(main())

Fallback behavior

If an individual translation fails, the source text is preserved in the output list rather than raising an exception. The utility fill_empty handles this gap-filling:

from fabricatio_translate.capabilities.translate import fill_empty

result = fill_empty(["a", "b", "c"], ["translated_a", None, None])
# result == ["translated_a", "b", "c"]

API Reference

Translate capability

Method Returns Description
translate(text, target_language, specification="", **kwargs) str | list[str] Translate a single text or list of texts
translate_chunked(text, target_language, chunk_size=6000, specification="", **kwargs) str | list[str] Split long text into word-count-based chunks, translate each, and reassemble

Both methods accept a specification string for style, tone, or terminology instructions passed to the LLM template.

Configuration

All options below are read through the fabricatio configuration chain (see the Configuration Guide). Set them under the [ext.translate] table in fabricatio.toml, equivalently under [tool.fabricatio.ext.translate] in pyproject.toml, or via FABRICATIO_EXT__TRANSLATE__<FIELD_UPPER> environment variables.

[ext.translate]
translate_template = "built-in/translate"
Option Type Default Description
translate_template str "built-in/translate" The template to use for translation.

Access at runtime: from fabricatio_translate.config import translate_config.

Kwargs types

Class Fields
TranslateKwargs target_language: str, specification: str
TranslateChunkedKwargs extends TranslateKwargs with chunk_size: int

License

MIT — see the LICENSE file.

Metadata

Release files for fabricatio-translate 0.1.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for fabricatio-translate 0.1.9
File Interpreter ABI Platform
fabricatio_translate-0.1.9-py3-none-any.whl Python 3 none any Details

Release files / fabricatio_translate-0.1.9-py3-none-any.whl

Download URL fabricatio_translate-0.1.9-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dffece718df9b973f4c393f9bb4ff9c3a7567fb9bd59c7c5f55114e2558a4600
BLAKE2b-256 checksum
How to use checksums
177c9bd1e5474328b0e8bd73a60eff910ef333eb800f1e56a852d4d967fced1c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page