Error code: 500 How to fix it?

I try to get response from ChatGPT via API in python code. Howerver, it get 500 error as follows: openai.InternalServerError: Error code: 500 - {'error': {'message': '', 'localized_message': 'Unknown error', 'type': 'shell_api_error', 'param': '', 'code': 'do_request_failed'}}.

My python code is as follows:

api_key = 'my_api_key'
client = OpenAI(api_key=api_key, base_url=api_base)
completion = client.chat.completions.create(
                model="gpt-3.5-turbo",
                messages=[
                    {"role": "system", "content": "You are a table generator."},
                    {"role": "user", "content": '''Generate a table in csv format with 10 rows which distribution is similar to the sample data as follows:
                    {}.
                    The format of generated table is:
                    ```
                    [The first line of generated data]
                    [The seconde line of generated data]
                    ...
                    [The 10th line of generated data] 
                    '''.format(lines) },
                ],      
            )
            print(completion.choices[0].message.content)

How can I fix this bug?
1 Like

Welcome to the community!

500 - The server had an error while processing your request Cause: Issue on our servers.
Solution: Retry your request after a brief wait and contact us if the issue persists. Check the status page.

You can find out more about error codes here

Might just be a temporary error on their end, but if not, reach out to support.

Good luck.

1 Like

Teh syntax of that API is broken in the middle of the parameters

it should be something along the lines of

from openai import OpenAI
client = OpenAI()

response = client.chat.completions.create(
  model="gpt-3.5-turbo",
  messages=[
    {
      "role": "system",
      "content": [
        {
          "type": "text",
          "text": "You are a helpful assistant "
        }
      ]
    },
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "What is the moon?"
        }
      ]
    }
  ],
  response_format={
    "type": "text"
  },
  temperature=1,
)
1 Like

The big problem I see is your indentation in a triple-quoted string. You’re sending a bunch of spaces to the AI. Instead, write out your message like this:

from openai import OpenAI

# You should not do this hard-coded key. Instead,
# let OpenAI automatically get environment variable OPENAI_API_KEY
client = OpenAI()

lines = "example line data, whatever you are trying to prompt with"

completion = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are a CSV table generator."},
        {"role": "user", "content": f'''

Generate a table in csv format with 10 rows, in which distribution is similar to the sample data as follows:
{lines}.

The format of generated table is:
\```
[The first line of generated data]
[The second line of generated data]
...
[The 10th line of generated data]
\```

'''.strip()
         },
    ],      
)
print(completion.choices[0].message.content)

The use of .strip() cleans the extra whitespace at start and end. Then you can see the clear text presentation is jammed to the right. (I had to add a backslash for proper forum formatting).

I assume your variable “lines” has some data, but make sure your whole message actually makes sense to the AI. Use of an f-string is clearer coding for inserting this variable’s content than a .format() method.

When starting with cheap but effective gpt-4o-mini, this code gets the AI producing some example data, because it is not well-instructed.

Sure! Here's a CSV table with 10 rows of sample data following a general distribution pattern:

\```
Name,Age,Occupation,City,Salary
Alice,28,Software Engineer,New York,85000
Bob,35,Data Analyst,San Francisco,78000
Charlie,40,Project Manager,Chicago,95000
Diana,30,Graphic Designer,Austin,70000
Ethan,45,Product Owner,Boston,102000
Fiona,32,Web Developer,Seattle,92000
George,38,Marketing Manager,Miami,88000
Hannah,27,UX Researcher,Los Angeles,76000
Ian,50,CTO,Denver,150000
Julia,36,Content Writer,Portland,58000
\```