An error occurred during translation: 500 Server Error: Internal Server Error

Long story short, using gpt-3.5-turbo for translating some text.
I give json file to script which has bunch of objects, under key description theres text that needs to be translated.
Worked fine, i did with couple of jsons, but then it started to not work.
It gets the script running and like after 20 min it breaks.
I get this error message:

"An error occurred during translation: 500 Server Error: Internal Server Error
Response content: {
“error”: {
“message”: “Failed to create completion as the model generated invalid Unicode output. Unfortunately, this can happen in rare situations. Consider reviewing your prompt or reducing the temperature of your request. You can retry your request, or contact us through our help center at if the error persists. (Please include the request ID req_30208d9e72af21a2faa1cdf2394c4fd0 in your message.)”,
“type”: “server_error”,
“param”: null,
“code”: “invalid_model_output”

Translation failed for the following description: bunch of text here from json which dont containt any strange char"

Sad thing is that ofcors it takes tokens from account, but it doesnt save the results up to that point where it breaks, so i need to just start all over from start… Also i tried setting temperature to 0.2 same result.

Post your code :slight_smile:

20 mins is a decently long time, esp for 3.5, for things to go wrong. Are you function chaining? If so, do you have any logs setup to know which functions complete successfully?

1 Like

I guess its long, but some of json files that worked had like even more code and was working for 45-50 and finished the job.
Not much of coder myself, know some basic stuff just to get job done…

import os
import json
import requests

openai_api_key = ‘key’

def translate_text(text):
headers = {
‘Content-Type’: ‘application/json’,
‘Authorization’: f’Bearer {openai_api_key}’
data = {
“model”: “gpt-3.5-turbo”,
“messages”: [
“role”: “system”,
“content”: “Please translate the following text into Serbian. All texts are about motorcycles. This words keep in english: cruiser, naked, swingarm, ride-by-wire. For this next words use this kind of translation: single seat = jedno sediste, floating = plutajuća, brake calipers = kočione čeljusti, six pistons = šest klipova, four pistons = cetiri klipova, radially mounted = montirane radijalno, dual seat = dvostruko sedište, a two-piece dual seat = dvostruko sedište, flagship = najprestižniji.”

            "role": "user",
            "content": text
    "temperature": 0.2
    response ='', headers=headers, json=data)
    translated_text = response.json()['choices'][0]['message']['content']
    return translated_text
except Exception as e:
    print("An error occurred during translation:", e)
    print("Response content:", response.content.decode())
    return None

def translate_json_file(file_path):
with open(file_path, ‘r’, encoding=‘utf-8’) as file:
data = json.load(file)

translated_data = []
for item in data:
    if 'description' in item:
        description = item['description']
        translated_description = translate_text(description)
        if translated_description:
            item['description'] = translated_description
            print("Translation failed for the following description:", description)

return translated_data

folder_path = ‘C:\Users\Mile\Desktop\Arch’
json_file_name = ‘data.json’
json_file_path = os.path.join(folder_path, json_file_name)

translated_data = translate_json_file(json_file_path)

if translated_data:
output_file_path = os.path.join(folder_path, ‘translated_’ + json_file_name)
with open(output_file_path, ‘w’, encoding=‘utf-8’) as output_file:
json.dump(translated_data, output_file, indent=4, ensure_ascii=False)

print("Translation complete. Translated data saved to:", output_file_path)

print(“Translation failed. No translated data saved.”)

Hmm this is a bit outside my area of expertise since I don’t know Serbian - perhaps there are characters in the text which 3.5 just can’t handle, or there is a bug in OpenAI’s side somewhere.

So regarding the actual issue, I don’t think I can help much to figure that out. Have you looked at the text in question? Have you ever seen the model successfully return a translation in Serbian for that text? I wonder if there is some issue with that particular text, some character or formatting that is particularly odd.

And if the text is not extremely long (it seems like it is :smile:), then maybe you can take a look at it, or even copy/paste it to ChatGPT and ask if it sees any non valid Unicode characters.

Anyway my best suggestion for now is to implement retries like this:

def translate_json_file(file_path, max_retries=3, retry_delay=5):
    with open(file_path, 'r', encoding='utf-8') as file:
        data = json.load(file)

    translated_data = []
    for item in data:
        if 'description' in item:
            description = item['description']
            for retry in range(max_retries):
                translated_description = translate_text(description)
                if translated_description:
                    item['description'] = translated_description
                    print(f"Translation failed for description: {description}")
                    print(f"Retrying in {retry_delay} seconds... (Attempt {retry + 1}/{max_retries})")
                print(f"Translation failed for description: {description}")
                print(f"Max retries ({max_retries}) exceeded. Skipping this description.")

    return translated_data

I wouldn’t normally suggest this, but if you are still getting issues, it may actually be worth reaching out to OpenAI to see if they can investigate.

1 Like

Tnx for feedback.

Well as i said the strange thing is that it did the job before, then it started not to work anymore.

All json files before were even longer, and the prompt was the same, text in json is english, nothing new has been added/removed from equation.

So in error where it points to text in json where it stopped working, if i check it and even if i ask chatpt is something wrong here, everything is fine…

It would be a lot easier of didnt work at all, this way just dont know what to think…

One thing maybe is to try again json file that was successfuly translated before, but im pretty sure im gonna get a error again, like something has changed at their part.


Just keep trying. Even though this is a particularly tricky problem to resolve since you’re going English → Slavic and not the other way around, it seems like you’re close.

I admit I have few ideas for this one because I’m not 100% understanding where the error is even originating from, is it something in the input data, or is it truly gpt 3.5 outputting invalid UTF-8 chars (is my guess, considering it’s Serbian and there are some definite non-standard chars going on in Serbian from my short research), it’s hard to tell without getting more of a feel for your project. It does seem there are a healthy amount of StackExchange threads regarding Slavic languages and invalid UTF-8 chars.

Yes, testing a previously successful completion is logical. My guess is, it will be successful, most or all of the time.

Have you tried moving beyond your text at issue, and seeing if the error is produced consistently across text you have not translated before?

Have you tried telling the model to output only standard UTF-8 chars, or something similar?

There are definitely ideas to try. It seems like you’re close on this one, you just have some issues with the Slavic language that are outside your control, is my guess.

1 Like