Vision api does not work with project

Including a project header when curling leads to the error:

The server had an error processing your request. Sorry about that! You can retry your request, or contact us through our help center at help.openai.com if you keep seeing this error.

Example that fails:

curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "OpenAI-Project: <>" \
  -H "Authorization: Bearer <>" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "What’s in this image?"
          },
          {
            "type": "image_url",
            "image_url": {
              "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
            }
          }
        ]
      }
    ],
    "max_tokens": 300
  }'

The same curl request works when you remove the project header

i am not sure i understand

Welcome @talal1,

Are you using a project API key or a legacy API key?

The project header is only to be used when you want usage to be billed against a project using a legacy API key. Additionally, if it’s the default project, there’s no need for the header.

More info in docs

1 Like

unfortunately, it fails even when i use a legacy api key.
If i remove the vision portion, it works e.g., the following works:

curl https://api.openai.com/v1/chat/completions  \
 -H "Content-Type: application/json"  \
 -H "OpenAI-Project:<>"   -H "Authorization: Bearer sk-<>"   -d '{
    "model": "gpt-4o",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "What is 1 + 2?"
          }
        ]
      }
    ],
    "max_tokens": 300                                                                                                      
}'

For users who belong to multiple organizations or are accessing their projects through their legacy user API key, you can pass a header to specify which organization and project is used for an API request.

Project key + matching OpenAI-Project header = undefined, indeterminate state

I can confirm that with gpt-4o, image gives 500 status with project key + project id, while removing the image from the content list gets a response.

Old API key + project, as documented, is also 500

The solution

  • When using project_ID, the “OpenAI-Organization” header is also required.

Python gpt-4o vision usage:

import httpx as requests; import os, json
messages = [...]
body = {
    "model": "gpt-4o", "max_tokens": 100, "top_p": 1e-7, "temperature": 1e-7,
    "messages": messages, # provide messages python list
}
my_org = os.environ.get('OPENAI_ORG_ID')  # SDK default
project_id = os.environ.get('OPENAI_PROJECT_ID')  # SDK default
apikey = os.environ.get('OPENAI_API_KEY')  # SDK default
project_key = os.environ.get('OPENAI_API_KEY_PROJECT') or ""  # custom

headers = {
    "Authorization": f"Bearer {apikey}",
    "OpenAI-Organization": f"{my_org}",
    "OpenAI-Project": f"{project_id}",
    "OpenAI-Beta": "assistants=v2",
}
url = "https://api.openai.com/v1/chat/completions"
response = requests.post(url, headers=headers, json=body)
if response.status_code != 200:
    print(f"HTTP error {response.status_code}: {response.text}")
else:
    print(json.dumps(response.json(), indent=3))
1 Like