agents icon indicating copy to clipboard operation
agents copied to clipboard

in _ws_connect raise WSServerHandshakeError( aiohttp.client_exceptions.WSServerHandshakeError: 401

Open sarangmohod opened this issue 2 months ago • 8 comments

Bug Description

when i try to run agent.py file in dev it shows this kind of error

2025-11-07 13:50:16,282 - WARNING livekit.agents - failed to connect to livekit, retrying in 0s Traceback (most recent call last): File "/home/livekit/new_backend/livekit-voice-agent/.venv/lib/python3.10/site-packages/livekit/agents/worker.py", line 695, in _connection_task ws = await self._http_session.ws_connect( File "/home/livekit/new_backend/livekit-voice-agent/.venv/lib/python3.10/site-packages/aiohttp/client.py", line 1093, in _ws_connect raise WSServerHandshakeError( aiohttp.client_exceptions.WSServerHandshakeError: 401, message='Invalid response status', url='wss://demoproject-tdoekpst.livekit.cloud/agent'

Expected Behavior

when we run "uv run agent.py dev" it should connect to livekit cloud server

Reproduction Steps

1.follow steps to create agent using python quickstart 
2.created agent
3.then run using uv run agent.py dev
...
- Sample code snippet -
from dotenv import load_dotenv

from livekit import agents
from livekit.agents import AgentSession, Agent, RoomInputOptions
from livekit.plugins import noise_cancellation, silero
from livekit.plugins.turn_detector.multilingual import MultilingualModel

load_dotenv(".env.local")


class Assistant(Agent):
    def __init__(self) -> None:
        super().__init__(
            instructions="""You are a helpful voice AI assistant.
            You eagerly assist users with their questions by providing information from your extensive knowledge.
            Your responses are concise, to the point, and without any complex formatting or punctuation including emojis, asterisks, or other symbols.
            You are curious, friendly, and have a sense of humor.""",
        )


async def entrypoint(ctx: agents.JobContext):
    session = AgentSession(
        stt="assemblyai/universal-streaming:en",
        llm="openai/gpt-4.1-mini",
        tts="cartesia/sonic-3:9626c31c-bec5-4cca-baa8-f8ba9e84c8bc",
        vad=silero.VAD.load(),
        turn_detection=MultilingualModel(),
    )

    await session.start(
        room=ctx.room,
        agent=Assistant(),
        room_input_options=RoomInputOptions(
            # For telephony applications, use `BVCTelephony` instead for best results
            noise_cancellation=noise_cancellation.BVC(), 
        ),
    )

    await session.generate_reply(
        instructions="Greet the user and offer your assistance."
    )


if __name__ == "__main__":
    agents.cli.run_app(agents.WorkerOptions(entrypoint_fnc=entrypoint))

Operating System

linux

Models Used

No response

Package Versions

livekit                                  1.0.18
livekit-agents                           1.2.18
livekit-api                              1.0.7
livekit-blingfire                        1.0.0
livekit-plugins-assemblyai               1.2.18
livekit-plugins-noise-cancellation       0.2.5
livekit-plugins-silero                   1.2.18
livekit-plugins-turn-detector            1.2.18
livekit-protocol                         1.0.8

Session/Room/Call IDs

No response

Proposed Solution


Additional Context

No response

Screenshots and Recordings

No response

sarangmohod avatar Nov 07 '25 09:11 sarangmohod

Hi, have you configured your credentials including LIVEKIT_API_KEY and LIVEKIT_API_SECRET? A 401 error suggests that authorization failed

tinalenguyen avatar Nov 07 '25 17:11 tinalenguyen

Hi, have you configured your credentials including LIVEKIT_API_KEY and LIVEKIT_API_SECRET? A 401 error suggests that authorization failed

Hi, I’ve cross-checked the credentials multiple times, but it still didn’t work. I even created a new agent, and later a completely new project using another email ID, but faced the same issue. After that, I tried the same process on a different PC following the LiveKit documentation carefully, but it still failed to connect to the LiveKit cloud server.

sarangmohod avatar Nov 07 '25 18:11 sarangmohod

Could you try removing /agent from your LiveKit URL: wss://demoproject-tdoekpst.livekit.cloud and see if that works?

In my .env.local file, I have this line: LIVEKIT_URL="wss://demoproject-tdoekpst.livekit.cloud"

When I run uv run agent.py, it automatically connects to wss://demoproject-tdoekpst.livekit.cloud/agent.

I followed the LiveKit documentation for creating the agent from this page: https://docs.livekit.io/agents/start/voice-ai/

sarangmohod avatar Nov 10 '25 06:11 sarangmohod

Apologies, I just realized that /agent was automatically appended to the URL. I don't think we support custom voices for TTS in our inference yet, so that may be a factor, Could you try manually importing the STT/LLM/TTS components? The setup would look something like:

session = AgentSession(
        stt=assemblyai.STT(),
        llm=openai.LLM(model="gpt-4.1-mini"),
        tts=cartesia.TTS(model"sonic-3", voice="9626c31c-bec5-4cca-baa8-f8ba9e84c8bc"),
        vad=silero.VAD.load(),
        turn_detection=MultilingualModel(),
    )

tinalenguyen avatar Nov 10 '25 06:11 tinalenguyen

Apologies, I just realized that /agent was automatically appended to the URL. I don't think we support custom voices for TTS in our inference yet, so that may be a factor, Could you try manually importing the STT/LLM/TTS components? The setup would look something like:

session = AgentSession(
        stt=assemblyai.STT(),
        llm=openai.LLM(model="gpt-4.1-mini"),
        tts=cartesia.TTS(model"sonic-3", voice="9626c31c-bec5-4cca-baa8-f8ba9e84c8bc"),
        vad=silero.VAD.load(),
        turn_detection=MultilingualModel(),
    )

I have used manual setup as you suggested: from dotenv import load_dotenv from livekit import agents from livekit.agents import AgentSession, Agent, RoomInputOptions from livekit.plugins import noise_cancellation, silero from livekit.plugins.turn_detector.multilingual import MultilingualModel

load_dotenv(".env.local") class Assistant(Agent): def init(self) -> None: super().init( instructions="""You are a helpful voice AI assistant. You eagerly assist users with their questions by providing information from your extensive knowledge. Your responses are concise, to the point, and without any complex formatting or punctuation including emojis, asterisks, or other symbols. You are curious, friendly, and have a sense of humor.""", ) async def entrypoint(ctx: agents.JobContext): session = AgentSession( stt="assemblyai/universal-streaming:en", llm="openai/gpt-4.1-mini", tts="cartesia/sonic-3:9626c31c-bec5-4cca-baa8-f8ba9e84c8bc", vad=silero.VAD.load(), turn_detection=MultilingualModel(), ) await session.start( room=ctx.room, agent=Assistant(), room_input_options=RoomInputOptions( # For telephony applications, use BVCTelephony instead for best results noise_cancellation=noise_cancellation.BVC(),

    ),
)
await session.generate_reply(
    instructions="Greet the user and offer your assistance."
)

if name == "main": agents.cli.run_app(agents.WorkerOptions(entrypoint_fnc=entrypoint))

sarangmohod avatar Nov 10 '25 06:11 sarangmohod

In the code snippet you posted, the setup is still the same (using strings to load in the models via inference). Could you try importing the plugins and using those?

tinalenguyen avatar Nov 10 '25 07:11 tinalenguyen

In the code snippet you posted, the setup is still the same (using strings to load in the models via inference). Could you try importing the plugins and using those?

After changing the code, it runs fine in the console, but in development, it still shows same error.

2025-11-10 12:57:17,506 - WARNING livekit.agents - failed to connect to livekit, retrying in 0s Traceback (most recent call last): File "/home/sarangmohod/livekit/livekit-voice-agent/.venv/lib/python3.10/site-packages/livekit/agents/worker.py", line 695, in _connection_task ws = await self._http_session.ws_connect( File "/home/sarangmohod/livekit/livekit-voice-agent/.venv/lib/python3.10/site-packages/aiohttp/client.py", line 1093, in _ws_connect raise WSServerHandshakeError( aiohttp.client_exceptions.WSServerHandshakeError: 401, message='Invalid response status', url=wss://demoproject-tdoekpst.livekit.cloud/agent.

sarangmohod avatar Nov 10 '25 07:11 sarangmohod

Here is one way to pinpoint the issue:

  1. In your local /home/sarangmohod/livekit/livekit-voice-agent/.venv/lib/python3.10/site-packages/livekit/agents/worker.py, locate the function call around line 695. Add print statements before that to see the actual api key, secret, url that are being loaded
  2. Compare them between the one you get from console and the one from dev. Most likely the ones in dev mode are somehow different.

chenghao-mou avatar Nov 14 '25 14:11 chenghao-mou