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

extra_headers while using as proxy

Open csgulati09 opened this issue 11 months ago • 0 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

If someone intents to use OpenAI client as a proxy for their calls. Specially for the realtime calls, it will throw 401. There is a chance that the authentication details might be there in the default headers. Since default headers are not being considered in the extra_headers. Hence the call returns a 401.

I use portkey (portkey.ai) for my project. There I am using default_headers where all the authentication takes place.

To Reproduce

  1. Initialize the client:
async def main():
    client = AsyncOpenAI(
        api_key="OPENAI_API_KEY",
        base_url=PORTKEY_GATEWAY_URL,
        default_headers=createHeaders(
            virtual_key="PORTKEY_PROVIDER_VIRTUAL_KEY",
        ),
    )
  1. Trying using realtime example for the openai README.

Error: websockets.exceptions.InvalidStatus: server rejected WebSocket connection: HTTP 401

Code snippets

import asyncio
from openai import AsyncOpenAI
from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL


async def main():
    client = AsyncOpenAI(
        api_key="OPENAI_API_KEY",
        base_url=PORTKEY_GATEWAY_URL,
        default_headers=createHeaders(
            virtual_key="PORTKEY_PROVIDER_VIRTUAL_KEY",
        ),
    )

    async with client.beta.realtime.connect(
        model="gpt-4o-realtime-preview-2024-10-01"
    ) as connection:
        await connection.session.update(session={"modalities": ["text"]})

        await connection.conversation.item.create(
            item={
                "type": "message",
                "role": "user",
                "content": [{"type": "input_text", "text": "Say hello!"}],
            }
        )
        await connection.response.create()

        async for event in connection:
            if event.type == "response.text.delta":
                print(event.delta, flush=True, end="")

            elif event.type == "response.text.done":
                print()

            elif event.type == "response.done":
                break


asyncio.run(main())

OS

macOS 15.1.1

Python version

Python 3.9.6

Library version

v1.58.1

csgulati09 avatar Dec 26 '24 09:12 csgulati09