what is the correct way to use vertex ai api for r...
# ask-ai
s
what is the correct way to use vertex ai api for realtime models in livekit cause when i used the credential json it keeps throwing error this is not supported
t
Thanks for reaching out, Ajay. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To get started with the Vertex AI API for real-time models in LiveKit, here’s a simple guide to help you along the way: 1. Enable Vertex AI: When you initialize the
RealtimeModel
, just set the
vertexai
parameter to
true
. For example, in Python:
Copy code
from livekit.plugins.google import beta

model = beta.realtime.RealtimeModel(
    vertexai=True,
    project="your-project-id",
    location="us-central1"
)
And if you’re using Node.js, it looks like this:
Copy code
import * as google from '@livekit/agents-plugin-google';

const realtimeModel = new google.beta.realtime.RealtimeModel({
  vertexai: true,
  project: 'your-project-id', 
  location: 'us-central1'
});
1. Authentication: Make sure your
GOOGLE_APPLICATION_CREDENTIALS
environment variable points to your service account key file. You can authenticate easily using
gcloud auth application-default login
. 2. Model Selection: The default model for Vertex AI is
gemini-2.0-flash-exp
, but feel free to specify other compatible models if you have preferences. 3. Environment Variables: You can also set these optional environment variables to configure your setup:
Copy code
export GOOGLE_CLOUD_PROJECT=your-project-id
export GOOGLE_CLOUD_LOCATION=us-central1
export GOOGLE_GENAI_USE_VERTEXAI=true
If you run into errors like "this is not supported," here are a few things to double-check: • Is the Vertex AI API enabled in your Google Cloud project? • Are your Application Default Credentials (ADC) set up correctly? • Do the
project
and
location
parameters perfectly match your Google Cloud configuration? Please don’t hesitate to reach out if you have any questions or need a hand with anything else—I’m here to help! Sources: Gemini Live API plugin | LiveKit Docs | plugins/google/src/beta/realtime/realtime_api.ts | Realtime model plugins | LiveKit Docs | Google AI and LiveKit | LiveKit Docs
s
i enabled vertex ai and i have a service account and i created a key there so i have json now now how do i make the livekit use that
t
To help you get LiveKit set up with your service account JSON key for Vertex AI, here’s a simple step-by-step guide: 1. Set the Environment Variable: Kindly point the
GOOGLE_APPLICATION_CREDENTIALS
environment variable to where your JSON key file is located. For example:
Copy code
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account-key.json"
1. Verify Authentication: To make sure everything is working smoothly, please run this command:
Copy code
gcloud auth application-default login
1. Restart Your Application: After that, just restart your LiveKit application so it can recognize the updated environment variable. Once you’ve completed these steps, LiveKit will happily use your service account JSON key to authenticate with Vertex AI. If you run into any bumps along the way, feel free to reach out—I’m here to help!
s
in my env this is there GOOGLE_APPLICATION_CREDENTIALS= "yojnamitra-474406-4866b96fccff.json" but i got this error 2025-10-07 122606,028 - ERROR livekit.agents - unhandled exception while running the job task Traceback (most recent call last): File "D:\voicebots\.venv\Lib\site-packages\opentelemetry\util\_decorator.py", line 71, in async_wrapper return await func(*args, **kwargs) # type: ignore ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\voicebots\.venv\Lib\site-packages\livekit\agents\ipc\job_proc_lazy_main.py", line 240, in _traceable_entrypoint await self._job_entrypoint_fnc(job_ctx) File "D:\voicebots\vertexai_livekit\agent.py", line 196, in entrypoint llm=google.beta.realtime.RealtimeModel( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\voicebots\.venv\Lib\site-packages\livekit\plugins\google\beta\realtime\realtime_api.py", line 229, in init raise ValueError( ValueError: Project is required for VertexAI via project kwarg or GOOGLE_CLOUD_PROJECT environment variable
t
It looks like the error is happening because the
GOOGLE_CLOUD_PROJECT
environment variable isn’t set, or the
project
parameter is missing when you’re initializing the
RealtimeModel
. No worries, let’s walk through how to fix this together: 1. Set the
GOOGLE_CLOUD_PROJECT
Environment Variable:
Just add this to your environment configuration:
Copy code
export GOOGLE_CLOUD_PROJECT="your-project-id"
1. Double-check Your Project ID: Make sure the project ID you’re using matches the one where Vertex AI is enabled and your service account is set up. 2. Alternatively, Pass the Project Parameter Directly: If you’d rather not use the environment variable, you can provide the
project
parameter right when you initialize the
RealtimeModel
like this:
Copy code
llm = google.beta.realtime.RealtimeModel(
    vertexai=True,
    project="your-project-id",
    location="us-central1"
)
Once you’ve made these updates, just restart your application and give it another try. If you run into any more issues or have questions, I’m here to help!
s
2025-10-07 122934,682 - DEBUG google.auth._default - Checking yojnamitra-474406-4866b96fccff.json for explicit credentials as part of auth process... Entering Engagement 2025-10-07 122935,058 - WARNING livekit.agents - Background audio is not supported in console mode. Audio will not be played. 2025-10-07 122935,100 - DEBUG livekit.agents - using audio io:
ChatCLI
->
AgentSession
->
TranscriptSynchronizer
->
ChatCLI
2025-10-07 122935,100 - DEBUG livekit.agents - using transcript io:
AgentSession
->
TranscriptSynchronizer
->
ChatCLI
Engagement has spoken. Trimming history if needed... [speech_created] No trimming needed. 2025-10-07 122937,182 - ERROR livekit.plugins.google - Gemini Realtime API error: received 1008 (policy violation) Permission denied on resource project yojnamitra-474406-4866b96fccff.json.; then sent 1008 (policy violation) Permission denied on resource project yojnamitra-474406-4866b96fccff.json. Traceback (most recent call last): File "D:\voicebots\.venv\Lib\site-packages\livekit\plugins\google\beta\realtime\realtime_api.py", line 589, in _main_task async with self._client.aio.live.connect( File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\contextlib.py", line 210, in aenter return await anext(self.gen) ^^^^^^^^^^^^^^^^^^^^^ File "D:\voicebots\.venv\Lib\site-packages\google\genai\live.py", line 1053, in connect logger.info(await ws.recv(decode=False)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\voicebots\.venv\Lib\site-packages\websockets\asyncio\connection.py", line 322, in recv raise self.protocol.close_exc from self.recv_exc websockets.exceptions.ConnectionClosedError: received 1008 (policy violation) Permission denied on resource project yojnamitra-474406-4866b96fccff.json.; then sent 1008 (policy violation) Permission denied on resource project yojnamitra-474406-4866b96fccff.json. 2025-10-07 122937,217 - ERROR livekit.agents - AgentSession is closing due to unrecoverable error Traceback (most recent call last): File "D:\voicebots\.venv\Lib\site-packages\livekit\plugins\google\beta\realtime\realtime_api.py", line 589, in _main_task async with self._client.aio.live.connect( File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\contextlib.py", line 210, in aenter return await anext(self.gen) ^^^^^^^^^^^^^^^^^^^^^ File "D:\voicebots\.venv\Lib\site-packages\google\genai\live.py", line 1053, in connect logger.info(await ws.recv(decode=False)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\voicebots\.venv\Lib\site-packages\websockets\asyncio\connection.py", line 322, in recv raise self.protocol.close_exc from self.recv_exc websockets.exceptions.ConnectionClosedError: received 1008 (policy violation) Permission denied on resource project yojnamitra-474406-4866b96fccff.json.; then sent 1008 (policy violation) Permission denied on resource project yojnamitra-474406-4866b96fccff.json. 2025-10-07 122937,218 - ERROR livekit.plugins.google - Error in _main_task Traceback (most recent call last): File "D:\voicebots\.venv\Lib\site-packages\livekit\plugins\google\beta\realtime\realtime_api.py", line 589, in _main_task async with self._client.aio.live.connect( File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\contextlib.py", line 210, in aenter return await anext(self.gen) ^^^^^^^^^^^^^^^^^^^^^ File "D:\voicebots\.venv\Lib\site-packages\google\genai\live.py", line 1053, in connect logger.info(await ws.recv(decode=False)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\voicebots\.venv\Lib\site-packages\websockets\asyncio\connection.py", line 322, in recv raise self.protocol.close_exc from self.recv_exc websockets.exceptions.ConnectionClosedError: received 1008 (policy violation) Permission denied on resource project yojnamitra-474406-4866b96fccff.json.; then sent 1008 (policy violation) Permission denied on resource project yojnamitra-474406-4866b96fccff.json. The above exception was the direct cause of the following exception: Traceback (most recent call last): File "D:\voicebots\.venv\Lib\site-packages\livekit\agents\utils\log.py", line 16, in async_fn_logs return await fn(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\voicebots\.venv\Lib\site-packages\livekit\plugins\google\beta\realtime\realtime_api.py", line 639, in _main_task raise APIConnectionError(message="Failed to connect to Gemini Live") from e livekit.agents._exceptions.APIConnectionError: Failed to connect to Gemini Live (body=None, retryable=True) 2025-10-07 122940,115 - ERROR livekit.agents - Error in _realtime_reply_task Traceback (most recent call last): File "D:\voicebots\.venv\Lib\site-packages\livekit\agents\utils\log.py", line 16, in async_fn_logs return await fn(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\voicebots\.venv\Lib\site-packages\livekit\agents\voice\agent_activity.py", line 1752, in _realtime_reply_task generation_ev = await self._rt_session.generate_reply( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ livekit.agents.llm.realtime.RealtimeError: generate_reply timed out waiting for generation_created event. 2025-10-07 122940,134 - DEBUG livekit.agents - session closed {"reason": "error", "error": "type='realtime_model_error' timestamp=1759820377.2168162 label='livekit.plugins.google.beta.realtime.realtime_api.RealtimeModel' error=ConnectionClosedError(Close(code=1008, reason='Permission denied on resource project yojnamitra-474406-4866b96fccff.json.'), Close(code=1008, reason='Permission denied on resource project yojnamitra-474406-4866b96fccff.json.'), True) recoverable=False"} 2025-10-07 122941,876 - INFO livekit.agents - shutting down worker {"id": "unregistered"} 2025-10-07 122941,876 - DEBUG livekit.agents - shutting down job task {"reason": "", "user_initiated": false} 2025-10-07 122941,878 - DEBUG livekit.agents - http_session(): closing the httpclient ctx 2025-10-07 122941,878 - DEBUG livekit.agents - job exiting {"reason": "", "tid": 7980, "job_id": "simulated-job-133f6a9c1c70"} 2025-10-07 122941,878 - DEBUG livekit.agents - http_session(): creating a new httpclient ctx 2025-10-07 122941,880 - INFO livekit.agents - process exiting {"reason": "", "pid": 20108, "inference": true} forrtl: error (200): program aborting due to control-C event Image PC Routine Line Source KERNELBASE.dll 00007FFB39D9A09D Unknown Unknown Unknown KERNEL32.DLL 00007FFB3A9BE8D7 Unknown Unknown Unknown ntdll.dll 00007FFB3CB48D9C Unknown Unknown Unknown 2025-10-07 122942,711 - ERROR asyncio - Task exception was never retrieved future: <Task finished name='AgentActivity.realtime_reply' coro=<AgentActivity._realtime_reply_task() done, defined at D\voicebots\.venv\Lib\site packages\livekit\agents\utils\log.py13> exception=RealtimeError('generate_reply timed out waiting for generation_created event.')> Traceback (most recent call last): File "D:\voicebots\.venv\Lib\site-packages\livekit\agents\utils\log.py", line 16, in async_fn_logs return await fn(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\voicebots\.venv\Lib\site-packages\livekit\agents\voice\agent_activity.py", line 1752, in _realtime_reply_task generation_ev = await self._rt_session.generate_reply( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ livekit.agents.llm.realtime.RealtimeError: generate_reply timed out waiting for generation_created event.
t
It seems like the error is related to a permission issue with your project configuration. Don’t worry, I’m here to help you through it! Please try these steps to get things back on track: 1. Verify Authentication: First, make sure your
GOOGLE_APPLICATION_CREDENTIALS
environment variable is correctly pointing to your service account JSON file. You can do this by running:
Copy code
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account-key.json"
1. Enable Vertex AI API: Next, double-check that the Vertex AI API is enabled in your Google Cloud project by running:
Copy code
gcloud services enable aiplatform.googleapis.com
1. Set Project and Location: Then, ensure the
GOOGLE_CLOUD_PROJECT
and
GOOGLE_CLOUD_LOCATION
environment variables are set properly:
Copy code
export GOOGLE_CLOUD_PROJECT="your-project-id"
export GOOGLE_CLOUD_LOCATION="us-central1"
1. Assign IAM Roles: It’s important that your service account has the
Vertex AI User
role. You can assign it with this command:
Copy code
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
  --member="serviceAccount:your-service-account@your-project.iam.gserviceaccount.com" \
  --role="roles/aiplatform.user"
1. Restart Your Application: Finally, restart your application so all these changes take effect. If you still encounter any issues after trying these steps, please don’t hesitate to reach out. I’m here to help you get this resolved! Sources: Google Gemini LLM plugin guide | LiveKit Docs | Gemini Live API plugin | LiveKit Docs | plugins/google/README.md | livekit-plugins/livekit-plugins-google/livekit/plugins/google/beta/realtime...