# "API key not valid. Please pass a valid API key.", throwing this error using Gemini API

**URL:** <https://community.crewai.com/t/api-key-not-valid-please-pass-a-valid-api-key-throwing-this-error-using-gemini-api/3610>\
**Category:** CrewAI Community Support\
**Tags:** crewai\
**Created:** [February 6, 2025, 1:58pm UTC](https://community.crewai.com/t/api-key-not-valid-please-pass-a-valid-api-key-throwing-this-error-using-gemini-api/3610 "2025-02-06T13:58:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunny0717](https://sea1.discourse-cdn.com/flex025/user_avatar/community.crewai.com/sunny0717/32/1395_2.png) [@sunny0717](https://community.crewai.com/u/sunny0717)\
**Post date:** [February 6, 2025, 1:58pm UTC](https://community.crewai.com/t/api-key-not-valid-please-pass-a-valid-api-key-throwing-this-error-using-gemini-api/3610/1 "2025-02-06T13:58:14Z")

</div>

The code throwing error after Task 1:

from crewai import Agent, LLM  
import os  
from dotenv import load\_dotenv  
load\_dotenv()

llm = LLM(  
model = “gemini/gemini-1.5-pro”,  
verbose = True,  
temperature = 0.5,  
google\_api\_key = os.getenv(“GOOGLE\_API\_KEY”)

)

planner = Agent(  
role=“Content Planner”,  
goal=“Plan engaging and factually accurate content on {topic}”,  
backstory="You’re working on planning a blog article "  
“about the topic: {topic}.”  
"You collect information that helps the "  
"audience learn something "  
"and make informed decisions. "  
"Your work is the basis for "  
“the Content Writer to write an article on this topic.”,

```
llm=llm,
memory = True,
allow_delegation=True,
verbose=True

```

)

writer = Agent(  
role=“Content Writer”,  
goal="Write insightful and factually accurate "  
“opinion piece about the topic: {topic}”,  
backstory="You’re working on a writing "  
"a new opinion piece about the topic: {topic}. "  
"You base your writing on the work of "  
"the Content Planner, who provides an outline "  
"and relevant context about the topic. "  
"You follow the main objectives and "  
"direction of the outline, "  
"as provide by the Content Planner. "  
"You also provide objective and impartial insights "  
"and back them up with information "  
"provide by the Content Planner. "  
"You acknowledge in your opinion piece "  
"when your statements are opinions "  
“as opposed to objective statements.”,  
llm=llm,  
memory = True,  
allow\_delegation=True,  
verbose=True  
)

editor = Agent(  
role=“Editor”,  
goal="Edit a given blog post to align with "  
"the writing style of the organization. ",  
backstory="You are an editor who receives a blog post "  
"from the Content Writer. "  
"Your goal is to review the blog post "  
“to ensure that it follows journalistic best practices,”  
"provides balanced viewpoints "  
"when providing opinions or assertions, "  
"and also avoids major controversial topics "  
“or opinions when possible.”,  
llm=llm,  
memory = True,  
allow\_delegation=True,  
verbose=True  
)

**Error message:**

ERROR:root:LiteLLM call failed: litellm.AuthenticationError: geminiException - {  
“error”: {  
“code”: 400,  
“message”: “API key not valid. Please pass a valid API key.”,  
“status”: “INVALID\_ARGUMENT”,

The API key is setup correctly and working, but the code throwing this error. Please help anyone if know the solution.

---

<div class="post-metadata">

**Author:** ![sunny0717](https://sea1.discourse-cdn.com/flex025/user_avatar/community.crewai.com/sunny0717/32/1395_2.png) [@sunny0717](https://community.crewai.com/u/sunny0717)\
**Post date:** [February 6, 2025, 2:05pm UTC](https://community.crewai.com/t/api-key-not-valid-please-pass-a-valid-api-key-throwing-this-error-using-gemini-api/3610/2 "2025-02-06T14:05:30Z")

</div>

this is the output:

 ![gg](https://us1.discourse-cdn.com/flex025/uploads/crewai/original/2X/2/2b7ce9aef904860cf197b6fea76588ecbb286ba7.png)
