Python – How to Fix OpenAI API Error 403: Country, Region, or Territory Not Supported

openai-apipython

I have tried to run openai API in python with this code:

client = OpenAI(api_key='sk-...')


def response(question):
    response = client.chat.completions.create(
      model="gpt-4-turbo-preview",
      messages=[
        {
           "role": "user",
           "content": question 
        }
      ]
)


if __name__ == '__main__':
  response(question)

But I get this error:

openai.PermissionDeniedError: Error code: 403 - {'error': {'code': 'unsupported_country_region_territory', 'message': 'Country, region, or territory not supported', 'param': None, 'type': 'request_forbidden'}}

It's weird that when I run by Jupyter notebook. They run just fine the error just happen when I use python script.

Best Answer

Just read the error message. You run code form country, unsupported by openAI. Solution is to run your code from other country (like Jupyter does)