What am I missing? 'error': {'message': 'you must provide a model parameter'

I’m getting ‘error’: {‘message’: ‘you must provide a model parameter’ } when running this simple completions API. What am I missing?

BASE_URL = “https://api.openai.com/v1/chat/completions

OPENAI_API_KEY = os.getenv(“OPENAI_API_KEY”)

url = BASE_URL

headers = {“Authorization”: "Bearer " + OPENAI_API_KEY}

data = { “model”: “gpt-4”, “messages”: [{“role”: “user”, “content”: “How big is the Earth?”}] }

response = requests.post(url, data=json.dumps(data), headers=headers)

1 Like

Now I’m no fancy word-reading person, but I’m going to take a wild guess and say maybe a model parameter?

Just read the documentation,

https://platform.openai.com/docs/api-reference/chat/create

Here’s what a default cURL request looks like,

curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-3.5-turbo",
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant."
      },
      {
        "role": "user",
        "content": "Hello!"
      }
    ]
  }'

Maybe there’s some hint in there?

I turned out to be a problem with how I’m passing the json data. The correct way to send the payload is:

response = requests.post(url, json=data, headers=headers)

2 Likes

In the future, you may want to consider using the openai Python library.

from openai import OpenAI
client = OpenAI()

completion = client.chat.completions.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Hello!"}
  ]
)

print(completion.choices[0].message)

It somewhat simplifies the process.

1 Like