How to translate product listings with AI
Quick answer
Use a large language model like
gpt-4o to translate product listings by sending the original text as input and requesting the target language output. Automate this with the OpenAI Python SDK to batch process listings for multilingual ecommerce sites.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the openai Python package and set your API key as an environment variable for secure access.
pip install openai>=1.0 output
Collecting openai Downloading openai-1.x.x-py3-none-any.whl Installing collected packages: openai Successfully installed openai-1.x.x
Step by step
This example shows how to translate a product listing from English to Spanish using gpt-4o. It sends the original product description as a prompt and requests the translation.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
product_listing = "This stylish leather wallet features multiple card slots and a secure zipper compartment."
messages = [
{"role": "user", "content": f"Translate the following product listing to Spanish:\n{product_listing}"}
]
response = client.chat.completions.create(
model="gpt-4o",
messages=messages
)
translation = response.choices[0].message.content
print("Translated product listing:", translation) output
Translated product listing: Esta elegante cartera de cuero cuenta con múltiples ranuras para tarjetas y un compartimento seguro con cremallera.
Common variations
You can translate listings to other languages by changing the prompt language. For batch processing, loop over multiple listings. Use gpt-4o-mini for cost-effective translations or claude-3-5-sonnet-20241022 from Anthropic as an alternative.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
listings = [
"Elegant stainless steel watch with water resistance up to 50 meters.",
"Comfortable cotton t-shirt available in multiple colors."
]
target_language = "French"
for listing in listings:
messages = [
{"role": "user", "content": f"Translate this product listing to {target_language}:\n{listing}"}
]
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages
)
print(f"Original: {listing}")
print(f"Translated: {response.choices[0].message.content}\n") output
Original: Elegant stainless steel watch with water resistance up to 50 meters. Translated: Montre élégante en acier inoxydable avec une résistance à l'eau jusqu'à 50 mètres. Original: Comfortable cotton t-shirt available in multiple colors. Translated: T-shirt en coton confortable disponible en plusieurs couleurs.
Troubleshooting
- If translations are incomplete or cut off, increase
max_tokensin the API call. - If you get rate limit errors, implement exponential backoff retries.
- For inconsistent translations, add system instructions to specify style or tone.
Key Takeaways
- Use
gpt-4oorgpt-4o-minifor accurate and cost-effective product listing translations. - Batch translate multiple listings by looping API calls with dynamic prompts.
- Set
max_tokensappropriately to avoid truncated translations. - Add system-level instructions to control translation style and tone.
- Handle API rate limits with retry logic for robust production use.