ImportError: Unable to Import AzureOpenAI from OpenAI

Error

ImportError: cannot import name 'AzureOpenAI' from 'openai'
(/home/incorta/lib/python3.8/site-packages/openai/__init__.py)

Reason

The application is trying to import the AzureOpenAI client:

from openai import AzureOpenAI

However, the installed openai Python package does not contain the AzureOpenAI class. This typically happens when an older version of the OpenAI SDK (for example, 0.28.x) is installed, while the application code is written for the newer 1.x SDK.

Resolution

  • Verify the installed OpenAI SDK version:

    pip show openai
    
  • If the version is older than 1.x, uninstall it and install a compatible version (for example, 1.68.2):

    pip uninstall openai
    
    pip install openai==1.68.2
    
  • Ensure there are no conflicting OpenAI package versions in the Python environment.

  • Restart the application after upgrading the package so that the updated library is picked up.