openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

NotFoundError when specifying azure_deployment in AzureOpenAI client

Open IANTHEREAL opened this issue 5 months ago • 2 comments

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • [X] This is an issue with the Python library

Describe the bug

When initializing the AzureOpenAI client with the azure_deployment parameter specified, a NotFoundError with error code 404 is raised upon calling client.beta.assistants.list(). However, omitting the azure_deployment parameter results in the expected behavior with no errors.

(autogen) ➜  autogen git:(main) ✗ python test.py
Traceback (most recent call last):
  File "/Users/ianz/Work/autogen/test.py", line 11, in <module>
    print(client.beta.assistants.list())
  File "/Users/ianz/Work/miniconda3/envs/autogen/lib/python3.10/site-packages/openai/resources/beta/assistants/assistants.py", line 270, in list
    return self._get_api_list(
  File "/Users/ianz/Work/miniconda3/envs/autogen/lib/python3.10/site-packages/openai/_base_client.py", line 1145, in get_api_list
    return self._request_api_list(model, page, opts)
  File "/Users/ianz/Work/miniconda3/envs/autogen/lib/python3.10/site-packages/openai/_base_client.py", line 990, in _request_api_list
    return self.request(page, options, stream=False)
  File "/Users/ianz/Work/miniconda3/envs/autogen/lib/python3.10/site-packages/openai/_base_client.py", line 856, in request
    return self._request(
  File "/Users/ianz/Work/miniconda3/envs/autogen/lib/python3.10/site-packages/openai/_base_client.py", line 908, in _request
    raise self._make_status_error_from_response(err.response) from None
openai.NotFoundError: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}

To Reproduce

import os
from openai import AzureOpenAI
    
client = AzureOpenAI(
    api_key=os.getenv("AZURE_OPENAI_API_KEY"),  
    api_version="2024-02-15-preview",
    azure_endpoint = os.getenv("AZURE_OPENAI_API_BASE"),
    azure_deployment = "gpt-4-turbo"
)

print(client.beta.assistants.list())

Code snippets

No response

OS

macOS

Python version

Python 3.10.13

Library version

1.3.7

IANTHEREAL avatar Feb 19 '24 01:02 IANTHEREAL

@IANTHEREAL This is a good call out. For Azure assistants APIs, azure_deployment is not supported. To explain, Assistants is a bit different than other deployment-based features, in that it does not require the deployment name in the URL like: {endpoint}/openai/deployments/{azure_deployment}/chat/completions. While the client-level azure_deployment keyword argument is meant as a convenience to add the deployment name to the URL for you, we see in this case that it's not needed and results in 404. For Assistants APIs where you do need to pass the name of your deployment, i.e. assistants.create, you should pass it at the method-level:

my_assistant = client.beta.assistants.create(
    instructions="You are a personal math tutor. When asked a question, write and run Python code to answer the question.",
    name="Math Tutor",
    tools=[{"type": "code_interpreter"}],
    model="deployment-name",
)

@rattrayalex - I think the AzureOpenAI docstring should be updated to mention that azure_deployment is not supported for the assistants APIs. I can open a PR.

kristapratico avatar Feb 22 '24 22:02 kristapratico

Please do, thank you Krista!

cc @RobertCraigie

rattrayalex avatar Feb 23 '24 05:02 rattrayalex