Skip to content

Python: Bug: Python: AzureRealtimeWebsocket works only with hardcoded api_key in the script. #11639

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

Closed
mkcod opened this issue Apr 18, 2025 · 3 comments
Assignees
Labels
ai connector Anything related to AI connectors bug Something isn't working python Pull requests for the Python Semantic Kernel

Comments

@mkcod
Copy link

mkcod commented Apr 18, 2025

Describe the bug
A clear and concise description of what the bug is.

Looks like websocket connection is not using the same authentication method/variables same as azure ai settings. When I pass the value of api_key as hardcoded value in the script, then AzureRealtimeWebsocket works file. else, if i pass the api_key using environment variables, it is error out with websocket connection reject.

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. https://github.com/microsoft/semantic-kernel/blob/main/python/samples/concepts/realtime/realtime_agent_with_function_calling_websocket.py
  3. Click on '....'
  4. Find this code: realtime_agent = AzureRealtimeWebsocket()
  5. Replace with: realtime_agent = AzureRealtimeWebsocket(
    api_version="2024-10-01-preview",
    api_key=key
    )
    where key is defined as below:
    load_dotenv()
    key=os.getenv("AZURE_OPENAI_API_KEY")

This gives the below error:
websockets.exceptions.InvalidStatus: server rejected WebSocket connection: HTTP 401

and as per logs:
DEBUG:websockets.client:> api-key: 9dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx3j

where as the right key is:
5MxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxFk

when i pass the above key like below:

realtime_agent = AzureRealtimeWebsocket(
api_version="2024-10-01-preview",
api_key="5MxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxFk"
)

everything works fine,

this is not as per the api_key best practices as it exposes the api_key in the code.

  1. Scroll down to '....'
  2. See errorExpected behaviorA clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Platform

Additional context
Add any other context about the problem here.
Covered above everything.

@mkcod mkcod added the bug Something isn't working label Apr 18, 2025
@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code python Pull requests for the Python Semantic Kernel triage labels Apr 18, 2025
@github-actions github-actions bot changed the title Bug: Python: AzureRealtimeWebsocket works only with hardcoded api_key in the script. .Net: Bug: Python: AzureRealtimeWebsocket works only with hardcoded api_key in the script. Apr 18, 2025
@github-actions github-actions bot changed the title .Net: Bug: Python: AzureRealtimeWebsocket works only with hardcoded api_key in the script. Python: Bug: Python: AzureRealtimeWebsocket works only with hardcoded api_key in the script. Apr 18, 2025
@moonbox3
Copy link
Contributor

Hi @mkcod, I am having a hard time repro'ing this issue. Do you have multiple env vars/.env files configured with different API keys? I have one .env file that VSCode turns into env vars on my behalf given that it's located at the root of my project. When we don't pass in any override kwargs to the AzureRealtimeWebsocket constructor, we're allowing Pydantic settings to grab the env vars/.env config as part of object creation.

The fact that it's finding a key, and that it is incorrect, could mean you have multiple keys defined? Per load_dotenv(): it looks for the first .env file by calling find_dotenv, starting in the current working directory and walking up the parent directories until it finds one. Only the first file found is read. If you have several .env files, only the upper‑most one is loaded unless you call load_dotenv again with an explicit path.

I'd test with something like:

import os, dotenv
dotenv.load_dotenv(override=True)
assert os.environ["AZURE_OPENAI_API_KEY"].startswith("5M"), "Wrong key in env"

@moonbox3 moonbox3 added ai connector Anything related to AI connectors and removed triage labels Apr 22, 2025
@mkcod
Copy link
Author

mkcod commented Apr 23, 2025

@moonbox3 - you were right, it was picking up wrong API key from the parent directory. apologies for the confusion.

@moonbox3
Copy link
Contributor

@mkcod not a problem. Glad you got it figured out.

@moonbox3 moonbox3 assigned moonbox3 and unassigned eavanvalkenburg Apr 23, 2025
@moonbox3 moonbox3 removed the .NET Issue or Pull requests regarding .NET code label Apr 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ai connector Anything related to AI connectors bug Something isn't working python Pull requests for the Python Semantic Kernel
Projects
Status: No status
Development

No branches or pull requests

4 participants