Skip to main content

Language detection and translation middleware for AI agent frameworks

Project description

🌐 AzureAICommunity - Agent - Language Middleware

Automatic language detection, translation, and response back-translation middleware for AI agent applications built on the Agent Framework.

PyPI Version Python Versions PyPI Downloads License PyPI Status

Your agent always reasons in its best language — your users always receive replies in theirs.

Getting Started · Configuration · Usage · Contributing


Overview

azureaicommunity-agent-language-middleware is a plug-and-play translation layer for AI agent pipelines built on agent-framework. It automatically detects the user's language, translates incoming messages to the agent's target language, and back-translates the response — using Azure Translator with an optional LLM agent as fallback.

How It Works

✨ Features

Feature
🔍 Language detection — automatically detect the user's language with configurable confidence threshold
🔄 Round-trip translation — translate incoming messages and back-translate agent responses
☁️ Azure Translator — production-grade speed and accuracy with 100+ languages
🤖 LLM fallback — silently falls back to LLM translation when Azure is unavailable
🔧 Builder pattern — fluent create() API to compose and customize middleware
🔌 Framework integration — drops directly into agent-framework middleware pipelines

📦 Installation

pip install azureaicommunity-agent-language-middleware

🚀 Quick Start

import asyncio
from agent_framework import Agent
from agent_framework.ollama import OllamaChatClient
from language_middleware import LanguageTranslationMiddleware

async def main():
    client = OllamaChatClient(model="gemma3:4b")
    agent = Agent(client)

    middleware = LanguageTranslationMiddleware.create(
        llm_agent=agent,
        target_language="en",
    )

    try:
        result = await agent.run("Wie heißt die Hauptstadt von Frankreich?", middleware=middleware)
        print(result.text)  # "Die Hauptstadt von Frankreich ist Paris."
    finally:
        await middleware.aclose()

asyncio.run(main())

⚙️ Configuration

Parameter Type Default Description
target_language str "en" ISO 639-1 code of the language the agent should reason in
min_confidence float 0.8 Minimum detection confidence to skip translation
azure_config AzureTranslatorConfig None Azure Translator credentials (optional)
llm_agent Agent None LLM agent used as translation fallback or primary service

🧑‍💻 Usage

Option 1 — Azure Translator + LLM Fallback (Recommended)

import asyncio
import os
from agent_framework import Agent
from agent_framework.ollama import OllamaChatClient
from language_middleware import LanguageTranslationMiddleware, AzureTranslatorConfig

azure_config = AzureTranslatorConfig(
    key=os.environ.get("AZURE_TRANSLATOR_KEY", ""),
    region=os.environ.get("AZURE_TRANSLATOR_REGION", ""),
    endpoint=os.environ.get("AZURE_TRANSLATOR_ENDPOINT", ""),
)

async def main():
    client = OllamaChatClient(model="gemma3:4b")
    agent = Agent(client)

    middleware = LanguageTranslationMiddleware.create(
        azure_config=azure_config,
        target_language="en",
        min_confidence=0.8,
        llm_agent=agent,
    )

    try:
        tamil_query = "தஞ்சாவூரின் வரலாற்று சிறப்பு என்ன?"
        tamil_result = await agent.run(tamil_query, middleware=middleware)
        print("Tamil response:", tamil_result.text)

        german_query = "Was ist die historische Bedeutung von Thanjavur?"
        german_result = await agent.run(german_query, middleware=middleware)
        print("German response:", german_result.text)
    finally:
        await middleware.aclose()

asyncio.run(main())

Option 2 — LLM-Only (No Azure Subscription Required)

import asyncio
from agent_framework import Agent
from agent_framework.ollama import OllamaChatClient
from language_middleware import LanguageTranslationMiddleware

async def main():
    client = OllamaChatClient(model="gemma3:4b")
    agent = Agent(client)

    middleware = LanguageTranslationMiddleware.create(
        llm_agent=agent,
        target_language="en",
    )

    try:
        result = await agent.run("Wie heißt die Hauptstadt von Frankreich?", middleware=middleware)
        print(result.text)  # "Die Hauptstadt von Frankreich ist Paris."
    finally:
        await middleware.aclose()

asyncio.run(main())

Option 3 — Azure Only (No LLM Fallback)

middleware = LanguageTranslationMiddleware.create(
    azure_config=azure_config,
    target_language="en",
)

⚙️ How It Works

1. Intercept      →  middleware captures the last user message
2. Detect         →  language is detected with a confidence score
3. Translate      →  message is translated to the target language (if needed)
4. Agent runs     →  the primary LLM processes the translated message
5. Back-translate →  agent response is translated back to the user's original language
6. Return         →  user receives the response in their native language

Translation providers (with automatic fallback):

Priority Provider Capability
1st Azure Translator Detect language + translate (fast, accurate, 100+ languages)
2nd LLM Agent (fallback) Detect language + translate when Azure is unavailable

🤝 Contributing

Contributions are welcome! Please open an issue to discuss what you'd like to change before submitting a pull request.

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/my-feature)
  3. Commit your changes (git commit -m 'Add my feature')
  4. Push to the branch (git push origin feature/my-feature)
  5. Open a Pull Request

📄 License

MIT — see LICENSE for details.

Project details


Download files

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

Source Distribution

Built Distribution

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

File details

Details for the file azureaicommunity_agent_language_middleware-0.1.0.tar.gz.

File metadata

File hashes

Hashes for azureaicommunity_agent_language_middleware-0.1.0.tar.gz
Algorithm Hash digest
SHA256 de60a6e3f3544a5a6d3e837d31eda664f553f3495c3244cfd2461c21c16cf1e0
MD5 c978bcfcc7019a85328d0a37021357bb
BLAKE2b-256 50cfd889acc137fbd26a9e6f2a50062f3151fb92047db6e3bbb9fa0f439107ef

See more details on using hashes here.

File details

Details for the file azureaicommunity_agent_language_middleware-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for azureaicommunity_agent_language_middleware-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2a3698f751bdd08667024e88e393697c9dd5ba53e767e16692c7fdafb004ce3f
MD5 ca6888a942a26bfae04dc5d0434545cf
BLAKE2b-256 6890038d18638371c0f1cedbc14a0f3f3c203f9d86e66aae946a55b5a7bc1271

See more details on using hashes here.

Supported by

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