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):
- Remove
tools
andtool_choice
args. - Slightly reduce the length of the prompt, even by a couple of words.
- Add at least a second user message or an assistant message.
- 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. - Change the message. Some work and some don’t. ‘Hello.’ didn’t work, but ‘Hello, friend.’ worked.
- Change to
gpt-4o
orgpt-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)