Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extra_headers while using as proxy #1975

Open
1 task done
csgulati09 opened this issue Dec 26, 2024 · 0 comments · May be fixed by #1976
Open
1 task done

extra_headers while using as proxy #1975

csgulati09 opened this issue Dec 26, 2024 · 0 comments · May be fixed by #1976
Labels
bug Something isn't working

Comments

@csgulati09
Copy link

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

@csgulati09 csgulati09 added the bug Something isn't working label Dec 26, 2024
@csgulati09 csgulati09 linked a pull request Dec 26, 2024 that will close this issue
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant