Agno - facing 401 error while using gemini to load knowledge bases

Code:
import os
from agno.agent import Agent
from agno.knowledge.csv import CSVKnowledgeBase
from agno.vectordb.pgvector import PgVector
from dotenv import load_dotenv
from agno.models.google import Gemini
from agno.embedder.google import GeminiEmbedder
from agno.document.chunking.fixed import FixedSizeChunking
from environs import Env
import vertexai

load_dotenv()
vertexai.init(project=“xxxx-xxxx-4xxx7”, location=“asia-south1”)

GOOGLE_API_KEY = os.getenv(‘GOOGLE_API_KEY’)

env = Env()
env.read_env()
db_url = env.str(“DB_URL”)
csv_path = “./knowledge/xyz.csv”
table_name = “chunks4” # Or whatever your current table name is

def create_knowledge_base(csv_path, table_name):
kb = CSVKnowledgeBase(
path=csv_path,
vector_db=PgVector(
table_name=table_name,
db_url=db_url,
embedder=GeminiEmbedder(api_key=GOOGLE_API_KEY),
),
chunking_strategy=FixedSizeChunking(chunk_size=512), # Removed chunk_overlap as per earlier fix
)
kb.load()
return kb

kb4 = create_knowledge_base(csv_path, table_name)

relocation_bot = Agent(
model=Gemini(
id=“gemini-1.5-pro-002”
),
role=“Strict Relocation Assistant”,
description=“…”,
instructions=[… ],
knowledge=kb4,
search_knowledge=True,
prevent_hallucinations=True,
)

Error:
ERROR Error processing document ‘relocation_benefits’: 401 UNAUTHENTICATED. {‘error’: {‘code’: 401, ‘message’: ‘API keys are not supported
by this API. Expected OAuth2 access token or other authentication credentials that assert a principal. , ‘status’: ‘UNAUTHENTICATED’, ‘details’: [{’@type’:
type.googleapis.com/google.rpc.ErrorInfo’, ‘reason’: ‘CREDENTIALS_MISSING’, ‘domain’: ‘googleapis.com’, ‘metadata’: {‘method’:
‘google.cloud.aiplatform.v1beta1.PredictionService.Predict’, ‘service’: ‘aiplatform.googleapiscom’}}]}}

I have recently moved from phi to agno, and I am getting this error while trying to load the knowledge base.
I am using the google api key, which is absolutely functional, but somehow I am getting the same error again and again 401, unauthenticated.

Could you please provide me with the appropriate solution for this?strong text

Hi @aashishanegi23
thanks for reaching out and supporting Agno!
We’ve shared this with the team and are working through requests one by one—we’ll get back to you as soon as we can.
We’ve just kicked off the Global Agent Hackathon, so things are a bit busier than usual. If you’re up for it, we’d love for you to join—it’s a great chance to build, win some exciting prizes and connect with the agent community!
If it’s urgent, just let us know. Thanks for your patience!

Hey @aashishanegi23 ,

The error you’re seeing is due to using a Google API key with Vertex AI, which doesn’t support API key authentication.

Instead, Vertex AI expects OAuth2 access tokens or similar credentials that assert a principal.

You should either use Vertex AI with proper OAuth2 credentials, or stick with the standard Google API if you’re using an API key.

Please refer to our documentation for the correct setup:
:link: Gemini - Agno

Let me know if you need help setting it up!