OpenAI ChatCompletion example doesn't work

Looked at related question here, but that does not work.

This example is from the docs

assert openai.__version__ == '0.27.10'    # latest as of this question

completion = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",    # engine too does not work (and docs has model)
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Hello!"}

InvalidRequestError: Must provide an 'engine' or 'deployment_id' parameter to create a


This is my working python code

response = openai.ChatCompletion.create(
        temperature= 0.5,
        messages=[{"role": "user", "content": prompt}],

I don’t have any assert line in mine just a import openai also I think you’re using an old openai library mine is

openai --version
openai 0.27.8

be worth doing an update on it.

Thanks. Tried with this version too (0.27.8) - still doesn’t work.

PS: Please note that the version in my original post (0.27.10) is newer than yours (0.27.8)

Fair point on the 2.10 and 2.8.

The issue you are facing is one from some time ago, when the API was managed via engines, is there any more to your code? i.e. the setup code.

I’m using this setup code

openai.api_key = KEY
openai.api_base = ENDPOINT # https://...
openai.api_type = 'azure'
openai.api_version = '2023-05-15' # is this OK?

I’m using the version from this readme.

ok, let me give you my entire demo code an see if this runs for you, you can then utilise that as a jumping off point, note that I make use of environmental variable set with the export command, if you wish the API key to be stored in your enviroment across reboots and session you can use echo 'export OPENAI_API_KEY=your_api_key' >> ~/.bashrc

import os
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.ChatCompletion.create(
      "role": "user",
      "content": "what is 5+5?"
1 Like

Thanks for your help.
Turned out to be an issue with the deployment_id.

1 Like

For anyone else that finds this: Azure has a slightly different API format.

Pro Tip: The Azure Playground has a “View Code” link that will give you the correct code in several languages:

#Note: The openai-python library support for Azure OpenAI is in preview.
import os
import openai
openai.api_type = "azure"
openai.api_base = "https://<your-compnay>"
openai.api_version = "2023-07-01-preview"
openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.ChatCompletion.create(
  messages = [{"role":"user","content":"Hello!"}])

Ahhhh, that makes a lot of sense now, thank you very much, Nova.

Protip 2: only specifying the latest API versions will get you function calling, the latest is 2023-08-01-preview

as of 2023-07-01-preview, only GPT-4 could use functions.

You can also explore 2023-08-01-preview, and see a extensionsChatCompletionsRequest with things like "dataSources": alongside messages, and the citations output format demonstrated in Bing AI chat.

1 Like