Skip to content

Passing headers with the Azure OpenAI client in 1.1.1 #740

@dkhundley

Description

@dkhundley

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

  • This is an issue with the Python library

Describe the bug

To be clear, I’m not certain this is a bug, but I’m uncertain how to do this now.

For extra security layers, I am exposing Azure OpenAI via the Azure API Management service. When I make a call this way, I pass a bearer JWT token and Apim subscription key as headers. I had no problem doing this on lower versions like 0.28.1, but now I don’t know how to do this.

To Reproduce

Attempting to call Azure OpenAI with 1.1.1. The call does not successfully connect. After glancing through the source Python code, I thought maybe sticking the headers as default_headers in the AzureOpenAI client would work, but it does not.

# Setting the auth headers
auth_headers = {
    ‘Ocp-Apim-Subscription-Key’: my_apim_sub_key,
    ‘Authorization’: fBearer {my_jwt_token}’
}

# Setting the Azure OpenAI client
azure_openai_client = AzureOpenAI(
   api_key =null’,
   api_version =2023-07-01-preview’,
   azure_endpoint = my_special_url,
   default_headers = auth_headers
)

# Calling Azure OpenAI
openai_response = azure_openai_client.chat.completions.create(
    model =gpt-35-turbo-16k’,
    messages = my_messages
)

Here’s the way I’m doing it on 0.28.1, and this still works just fine. As you can see, I’m passing the auth headers at the time of the actual API call here.

import openai

# Setting the auth headers
auth_headers = {
    ‘Ocp-Apim-Subscription-Key’: my_apim_sub_key,
    ‘Authorization’: fBearer {my_jwt_token}’
}

# Setting config information on OpenAI
openai.api_url = my_special_url
openai.api_version =2023-07-01-previewopenai.api_key =null# Calling Azure OpenAI
openai_response = openai.ChatCompletion.create(
    engine =gpt-35-turbo-16k’,
    messages = my_messages,
    Headers = auth_headers
)

What might I be doing wrong?

Code snippets

No response

OS

macOS

Python version

Python v3.10

Library version

1.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions