I am having this error : aiohttp.client_exceptions...
# ask-ai
w
I am having this error : aiohttp.client_exceptions.WSServerHandshakeError: 401, message='Invalid response status', url='wss://keestudio-2jxcbw0l.livekit.cloud/agent' for my phone calling agent when i try to run it using the command agent.py dev
t
Thanks for reaching out, Keynar. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! That error
aiohttp.client_exceptions.WSServerHandshakeError: 401
usually means the WebSocket handshake didn’t go through because the authentication credentials weren’t quite right. Most often, this happens when the JSON Web Token (JWT) you’re using is either not generated correctly or doesn’t have the permissions it needs. Here’s how you can fix it: 1. Make sure you’re creating a valid JWT with the right grants. For agent connections, your token needs to include the
api.VideoGrants(agent=True)
grant. 2. Try using this example to generate the token with the `livekit-server-sdk`:
Copy code
from livekit import api

# Initialize AccessToken with your API key and secret
at = api.AccessToken(api_key="your-api-key", api_secret="your-api-secret")

# Add video grants for agent access
at.add_grant(api.VideoGrants(agent=True))

# Generate the JWT token
token = at.to_jwt()
1. Double-check that the API key and secret you’re using to create the token are correct and still active. You can always verify or regenerate them in your LiveKit project settings. 2. If your agent relies on environment variables, just confirm that
LIVEKIT_API_KEY
and
LIVEKIT_API_SECRET
are set up properly:
Copy code
LIVEKIT_API_KEY=your_api_key
LIVEKIT_API_SECRET=your_api_secret
Once your JWT is generated properly and in use, connecting to
wss://keestudio-2jxcbw0l.livekit.cloud/agent
should work smoothly. If you hit any bumps along the way, feel free to reach out! Sources: livekit-plugins/livekit-plugins-anam/livekit/plugins/anam/api.py | LiveKit CLI project management | LiveKit Docs | Protocol & WebRTC | livekit/livekit_composite | DeepWiki
w
now what do I do ? Do I run the code you just gave me or the agent itself ?
t
First, you'll want to run the code I shared to generate a valid JWT. This JWT is what will authenticate your agent connection. After you've got your JWT, you can go ahead and run the agent itself (for example, with the command
agent.py dev
). Just make sure the JWT you generated is being used properly for authentication. If your agent is set up to get the JWT from environment variables, don't forget to set it like this before running the agent:
Copy code
export LIVEKIT_JWT=your_generated_jwt
Feel free to reach out if you want me to walk you through any part of this!
w
here's what I got when running this code : python jwt.py Traceback (most recent call last): File "C:\Users\admin\Desktop\Phone agent 2\jwt.py", line 1, in <module> from livekit import api File "C:\Users\admin\Desktop\Phone agent 2\my_env\Lib\site-packages\livekit\api\__init__.py", line 38, in <module> from .livekit_api import LiveKitAPI File "C:\Users\admin\Desktop\Phone agent 2\my_env\Lib\site-packages\livekit\api\livekit_api.py", line 3, in <module> from .room_service import RoomService File "C:\Users\admin\Desktop\Phone agent 2\my_env\Lib\site-packages\livekit\api\room_service.py", line 27, in <module> from ._service import Service File "C:\Users\admin\Desktop\Phone agent 2\my_env\Lib\site-packages\livekit\api\_service.py", line 6, in <module> from .access_token import AccessToken, VideoGrants, SIPGrants File "C:\Users\admin\Desktop\Phone agent 2\my_env\Lib\site-packages\livekit\api\access_token.py", line 20, in <module> import jwt File "C:\Users\admin\Desktop\Phone agent 2\jwt.py", line 4, in <module> at = api.AccessToken(api_key="APIacjLavgcVf9o", api_secret="yJbeIn3ERiie7D0Vk1eEgYlf1DlWSPEeFxx7pNA9HqzH") ^^^^^^^^^^^^^^^ AttributeError: partially initialized module 'livekit.api' from 'C:\Users\admin\Desktop\Phone agent 2\my_env\Lib\site-packages\livekit\api\__init__.py' has no attribute 'AccessToken' (most likely due to a circular import)
t
Hey there! The error you ran into,
AttributeError: partially initialized module 'livekit.api' has no attribute 'AccessToken'
, usually happens because your script file has the same name as a library it’s trying to use. For example, if your script is named
jwt.py
, it can clash with the
jwt
library that the
livekit
package relies on. Here’s a simple way to fix it: 1. Rename your script from
jwt.py
to something like `generate_jwt.py`—anything that’s not just
jwt.py
. 2. Delete any
jwt.pyc
or similar compiled files in the same folder to keep things clean. 3. Try running your renamed script again to generate the JWT. If you hit any more bumps or have questions, just let me know—I’m here to help!
w
now I get this error : Traceback (most recent call last): File "C:\Users\admin\Desktop\Phone agent 2\jtw.py", line 7, in <module> at.add_grant(api.VideoGrants(agent=True)) ^^^^^^^^^^^^ AttributeError: 'AccessToken' object has no attribute 'add_grant'
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
w
okay I fixed it now what do i do ?