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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fabricatio_translate-0.1.9-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file fabricatio_translate-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: fabricatio_translate-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for fabricatio_translate-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 dffece718df9b973f4c393f9bb4ff9c3a7567fb9bd59c7c5f55114e2558a4600
MD5 78710edb80bbbeb2c35af501891bceb8
BLAKE2b-256 177c9bd1e5474328b0e8bd73a60eff910ef333eb800f1e56a852d4d967fced1c

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.9 This release

1 file

0.1.8

1 file

0.1.7

1 file

0.1.6

1 file

0.1.5

1 file

0.1.4

1 file

0.1.3

1 file

0.1.2

1 file

0.1.1

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page