Bizarre issue preventing response from gpt-4o-mini (‘The model produces invalid content’)

I’m having an extremely odd issue that I’ve been troubleshooting. I returned to my AI chatbot project recently, and it’s been a while. Last time, it used gpt-3.5-turbo, but I switched to gpt-4o-mini. I am getting a 500 error in specific circumstances.

openai.InternalServerError: Error code: 500 - {‘error’: {‘message’: ‘The model produced invalid content. Consider modifying your prompt if you are seeing this error persistently.’, ‘type’: ‘model_error’, ‘param’: None, ‘code’: None}}

Below is my testing code. I managed to narrow it down a bit but still have no idea what causes this issue.

Here’s what I can change to make the error not occur (any of these works):

  1. Remove tools and tool_choice args.
  2. Slightly reduce the length of the prompt, even by a couple of words.
  3. Add at least a second user message or an assistant message.
  4. Change the name or remove the name parameter (but that didn’t always work, depending on the prompt). Some names work, others don’t. ‘Gregory’ worked, for example.
  5. Change the message. Some work and some don’t. ‘Hello.’ didn’t work, but ‘Hello, friend.’ worked.
  6. Change to gpt-4o or gpt-3.5-turbo.

Changing max_tokens did not seem to have an effect.

With the code below, it errors without fail. It’s not the only circumstance in which it errors. As described above, making certain seemingly random changes causes it not to error. I initially discovered this issue when I cleared the message history of my bot so that the only messages were the system prompt and a user message.

I’m confused as to what is causing this. Considering gpt-4o-mini is very new, perhaps this is some strange edge-case bug? If anyone could enlighten me on what the issue could possibly be, I’m all ears!

import os
import openai
from datetime import datetime

# Initialize the OpenAI client
client = openai.OpenAI(api_key=os.environ['OAI_KEY'])

# Define the payload as a dictionary
request_payload = {
    'messages': [
        {
            'role': 'system',
            'content': (
                'You are Rachel, 30, female, from Texas. Likes photography, passionate about music. Appreciates humour, values staying close to family. Openly happy and does not like explicit topics/profanity. Respond in casual tone that is pleasing. Mirror user grammar, spelling.')
        },
        {
            'role': 'user',
            'name': 'Falkry',
            'content': 'Hello.'
        }
    ],
    'model': 'gpt-4o-mini',
    'max_tokens': 500,
    'temperature': 1.0,
    'frequency_penalty': 0.3,
    'presence_penalty': 0.1,
    'tools': [
        {
            'type': 'function',
            'function': {
                'name': 'example_function',
                'description': 'This is a description of the function.',
                'parameters': {
                    'type': 'object',
                    'properties': {
                        'arg1': {'type': 'string', 'description': 'The first argument.'},
                        'arg2': {'type': 'string', 'description': 'The second argument.'}
                    },
                    'required': ['arg1', 'arg2']
                }
            }
        }
    ],
    'tool_choice': 'auto'
}

# Send the request to the OpenAI API
completion = client.chat.completions.create(
    model=request_payload['model'],
    messages=request_payload['messages'],
    max_tokens=request_payload['max_tokens'],
    temperature=request_payload['temperature'],
    frequency_penalty=request_payload['frequency_penalty'],
    presence_penalty=request_payload['presence_penalty'],
    tools=request_payload['tools'],
    tool_choice=request_payload['tool_choice']
)

# Extract and print the completion message
m = completion.choices[0].message.content
print(m)
3 Likes

Having the same issues here, prompts / context work fine with 3.5-turbo and then 4o-mini keeps failing to produce the correct output

2 Likes

May be a temporary situation.
You can try using simple prompt then gradually increase complexity then “Error Handling” .
But probably may be a issue that will be " fixed " soon

2 Likes

I have no idea if it’s of any help, but the only time I’ve encountered this error was when my temperature setting was too high, which caused the AI to generate an invalid sequence of token.

Does this keep happening if you set the temperature to 0.5 or 0?

1 Like

yeah, also used gpt4-o-mini model for 2 days and it was half of the time giving 500 server error, switching back to gpt4-o fixed issue. maybe the model is yet unstable.

PS. we used 0.5 Temperatur and this was happening as well

There are multiple causes for this message, but in most cases, the problem is in the code itself, not on OpenAI’s side. I use LangGraph (LangSmith helps a lot in understanding these errors), and when I received such a response from OpenAI, it was usually due to:

  • Exceeding the token limit in the call
  • A corner case where the LLM incorrectly tries to call a tool
  • Incorrectly defined request payload
  • Internal error in the tool
  • A corner case where the tool returns a response that the LLM doesn’t understand
  • A corner case where the tool returns a incorrectly formatted response
1 Like

Is there any solution for this issue? I keep running into this problem every now and then.

openai.InternalServerError: Error code: 500 - {'error': {'message': 'The model produced invalid content. Consider modifying your prompt if you are seeing this error persistently. ', 'type': 'model_error', 'param': None, 'code': None}}

In my experience, every time I encounter this it was due to bug (or inappropriate way of doing something) in my code. The best way is to debug the code, messages, tool calls, arguments, prompts, etc.