We are building a production-ready translation agent that auto-detects source languages, translates with context-aware prompts, and validates quality through back-translation. This is useful for support teams, documentation pipelines, or any product that needs reliable multilingual output without managing token-based cost surprises.
What you'll need
- Python 3.10 or newer
- An Oxlo.ai API key from https://portal.oxlo.ai
- The OpenAI SDK:
pip install openai
Step 1: Initialize the Oxlo.ai client and sanity check
First, I instantiate the client pointing to Oxlo.ai and verify connectivity with a simple Spanish-to-English translation. I use llama-3.3-70b here because it handles multilingual tasks reliably.
from openai import OpenAI
client = OpenAI(
base_url="https://api.oxlo.ai/v1",
api_key="YOUR_OXLO_API_KEY"
)
response = client.chat.completions.create(
model="llama-3.3-70b",
messages=[
{"role": "user", "content": "Translate to English: Hola, ¿cómo estás?"}
]
)
print(response.choices[0].message.content)
Step 2: Define the system prompt for structured translation
To get consistent, parseable output, I give the model a system prompt that enforces JSON formatting and preserves tone. This prompt is the contract every subsequent request will rely on.
SYSTEM_PROMPT = """You are a professional translation engine.
When given a text and a target language, perform these steps:
1. Detect the source language (ISO 639-1 code).
2. Translate the text to the target language, preserving tone, formatting, and meaning.
3. Return only a JSON object with keys: source_language, target_language, translation.
Example:
Input:
Target: French
Text: Hello world
Output:
{"source_language": "en", "target_language": "fr", "translation": "Bonjour le monde"}"""
Step 3: Build the core translation function
Now I wrap the prompt in a reusable function that accepts any text and target language. I switch to qwen-3-32b for this step because its multilingual reasoning is particularly strong for mixed-language inputs.
import json
def translate(text: str, target_lang: str, model: str = "qwen-3-32b"):
user_msg = f"Target: {target_lang}\nText: {text}"
resp = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_msg},
],
temperature=0.1,
)
raw = resp.choices[0].message.content.strip()
if raw.startswith("
```"):
raw = raw.split("```
")[1].replace("json", "").strip()
return json.loads(raw)
result = translate("The server is temporarily unavailable.", "German")
print(json.dumps(result, indent=2, ensure_ascii=False))
Step 4: Add a back-translation quality gate
To catch hallucinations or meaning drift, I add a validation layer. I translate the output back to the source language and ask the model to flag discrepancies. DeepSeek V3.2 works well for this coding and reasoning task.
def validate_translation(original: str, translated_text: str, source_lang: str) -> dict:
prompt = f"""You are a translation validator.
Given the original text and a back-translated version, judge fidelity.
Return JSON with keys: is_valid (bool), issues (list), confidence_score (int 1-100).
Original ({source_lang}): {original}
Back-translation ({source_lang}): {translated_text}"""
resp = client.chat.completions.create(
model="deepseek-v3.2",
messages=[{"role": "user", "content": prompt}],
temperature=0.1,
)
raw = resp.choices[0].message.content.strip()
if raw.startswith("
```"):
raw = raw.split("```
")[1].replace("json", "").strip()
return json.loads(raw)
def full_pipeline(text: str, target_lang: str):
fwd = translate(text, target_lang)
back = translate(fwd["translation"], fwd["source_language"])
validation = validate_translation(text, back["translation"], fwd["source_language"])
return {
"translation": fwd,
"back_translation": back,
"validation": validation,
}
Step 5: Batch process a dataset
For production use, I process a list of strings. Because Oxlo.ai charges per request rather than per token, long documents and large batches are predictable in cost. I add a small sleep to stay within polite rate limits.
import time
def batch_translate(texts: list[str], target_lang: str, delay: float = 0.5):
results = []
for t in texts:
try:
out = full_pipeline(t, target_lang)
results.append(out)
except Exception as e:
results.append({"error": str(e), "text": t})
time.sleep(delay)
return results
documents = [
"Your invoice is ready for download.",
"Nous avons détecté une activité inhabituelle.",
"El servidor no responde. Por favor, inténtelo más tarde.",
]
outputs = batch_translate(documents, "English")
for o in outputs:
print(json.dumps(o, indent=2, ensure_ascii=False))
Run it
Here is the complete script that ties everything together. When I run it against Oxlo.ai, I get structured translations with validation metadata.
from openai import OpenAI
import json
import time
import os
client = OpenAI(
base_url="https://api.oxlo.ai/v1",
api_key=os.environ["OXLO_API_KEY"]
)
SYSTEM_PROMPT = """You are a professional translation engine.
When given a text and a target language, perform these steps:
1. Detect the source language (ISO 639-1 code).
2. Translate the text to the target language, preserving tone, formatting, and meaning.
3. Return only a JSON object with keys: source_language, target_language, translation.
Example:
Input:
Target: French
Text: Hello world
Output:
{"source_language": "en", "target_language": "fr", "translation": "Bonjour le monde"}"""
def translate(text: str, target_lang: str, model: str = "qwen-3-32b"):
user_msg = f"Target: {target_lang}\nText: {text}"
resp = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_msg},
],
temperature=0.1,
)
raw = resp.choices[0].message.content.strip()
if raw.startswith("
```"):
raw = raw.split("```
")[1].replace("json", "").strip()
return json.loads(raw)
def validate_translation(original: str, translated_text: str, source_lang: str) -> dict:
prompt = f"""You are a translation validator.
Given the original text and a back-translated version, judge fidelity.
Return JSON with keys: is_valid (bool), issues (list), confidence_score (int 1-100).
Original ({source_lang}): {original}
Back-translation ({source_lang}): {translated_text}"""
resp = client.chat.completions.create(
model="deepseek-v3.2",
messages=[{"role": "user", "content": prompt}],
temperature=0.1,
)
raw = resp.choices[0].message.content.strip()
if raw.startswith("
```"):
raw = raw.split("```
")[1].replace("json", "").strip()
return json.loads(raw)
def full_pipeline(text: str, target_lang: str):
fwd = translate(text, target_lang)
back = translate(fwd["translation"], fwd["source_language"])
val = validate_translation(text, back["translation"], fwd["source_language"])
return {"translation": fwd, "back_translation": back, "validation": val}
if __name__ == "__main__":
docs = [
"La factura está lista para su descarga.",
"We noticed unusual activity on your account.",
]
for doc in docs:
result = full_pipeline(doc, "English")
print(json.dumps(result, ensure_ascii=False, indent=2))
Example output:
{
"translation": {
"source_language": "es",
"target_language": "en",
"translation": "The invoice is ready for download."
},
"back_translation": {
"source_language": "en",
"target_language": "es",
"translation": "La factura está lista para descargar."
},
"validation": {
"is_valid": true,
"issues": [],
"confidence_score": 95
}
}
Wrap-up
From here, cache repeated phrases with a local SQLite store to avoid redundant API calls, or wire the pipeline into a FastAPI service for real-time inference. Oxlo.ai's request-based pricing means adding quality gates and processing large batches stays predictable, especially for long-context documents that would spike costs on token-based providers.
Top comments (0)