API error message mixes up object and string

I’m using the Python SDK. I’m first doing an audio translation, whose result (Translation) I was mistakenly passing to the method completions.create instead of the string Translation.text. This resulted in the following error:

openai.BadRequestError: Error code: 400 - {‘error’: {‘message’: “Invalid type for ‘messages[1].content[0]’: expected an object, but got a string instead.”, ‘type’: ‘invalid_request_error’, ‘param’: ‘messages[1].content[0]’, ‘code’: ‘invalid_type’}}

Completions requires the messages to have the content key, with a value that is either a string literal or an Iterable of ChatCompletionContentPartParam. When I used Translation.text for the content of a message instead of the Translation object, the error was resolved.

This leads me to believe that this part of the error message has swapped the words ‘object’ and ‘string’:

expected an object, but got a string instead.

It could also be that something weirder is going on, but the message is very confusing nonetheless.

1 Like

You are looking too deep into the API library. The types you are examining are only for validating the return from the API.

Let’s look at the error:


In a list of messages, the second message. In a list of contents, the first “content” must be an object.

This is correct. You must either send a single string as content, not a list, or if using a list (array) to send multi-part content (such as images), each shall be a JSON object with the type.

Since you only want to translate text, your messages would look like:

messages = [
{"role": "system", "content": "You are a language expert"},
{"role": "user", "content": f"Improve quality:\n{my_text}"}

You need to get a string out of the return object from the first API call.

In your example, you’re sending strings under content, but the error message says

expected an object

Please read my post again. I think you’re missing what the problem is.

1 Like

It seems the second message gets this form:

{‘role’: ‘user’, ‘content’: [‘text’]}

This makes more sense. I don’t know if the error message can be made more descriptive. Inspecting the actual request by setting the logging level to DEBUG revealed the culprit.

The problem is likely that you are not fully extracting a string from the prior return.

Here’s an example of using the transcriptions endpoint (translation is similar, it just always produces English):

# Open the audio file
with open(input_file_path, "rb") as audio_file:
    # Create a transcription using OpenAI API
        transcription = client.audio.transcriptions.create(
            file=audio_file, # a flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, webm
            language="en",   # ISO code
            prompt="Welcome to our radio show.",  # lead-up to input audio
            response_format="json",  # also text, srt, verbose_json, or vtt
    except Exception as e:
        print(f"An API error occurred: {e}")

# get just the transcribed text out of the response
transcribed_text = transcription.text

Yep. I said this in the initial post:

When I used Translation.text for the content of a message instead of the Translation object, the error was resolved.

I was confused by the error message, that’s all.