Skip to content

extra_headers while using as proxy  #1975

Open
@csgulati09

Description

@csgulati09

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions