High severity beginner · Fix: 2-5 min

ValueError: SERPER_API_KEY not set

crewai.tools.SerperDevTool initialization error: ValueError raised when SERPER_API_KEY environment variable is missing

What this error means
CrewAI's SerperDevTool requires the SERPER_API_KEY environment variable to be set before initialization, and raises ValueError when it's missing or empty.

Stack trace

traceback
Traceback (most recent call last):
  File "main.py", line 12, in <module>
    search_tool = SerperDevTool()
  File "/path/to/site-packages/crewai/tools/serper_dev_tool.py", line 45, in __init__
    raise ValueError("SERPER_API_KEY not set. Please set the SERPER_API_KEY environment variable.")
ValueError: SERPER_API_KEY not set. Please set the SERPER_API_KEY environment variable.
QUICK FIX
Add load_dotenv() at the top of your main script, create a .env file with SERPER_API_KEY=your_serper_key, then run with that directory as working directory.

Why it happens

SerperDevTool is a web search integration that requires authentication with Serper.dev's API. The tool's __init__ method explicitly checks for the SERPER_API_KEY environment variable and raises ValueError if it's not found, empty, or None. This is a deliberate safety check to prevent runtime failures when the agent attempts search queries without valid credentials.

Detection

Catch ValueError during agent/tool initialization with a try/except block, or validate environment variables before instantiating any CrewAI tools that require API keys. Add a startup health check that iterates required env vars and logs missing ones before agents run.

Causes & fixes

1

SERPER_API_KEY environment variable not set in shell, .env file, or deployment environment

✓ Fix

Set SERPER_API_KEY in your .env file (SERPER_API_KEY=your_key_here), then load it with python-dotenv: from dotenv import load_dotenv; load_dotenv(). Or set it in your deployment platform's secrets manager (GitHub Actions, Docker, AWS Lambda, etc.).

2

SERPER_API_KEY is set but os.environ['SERPER_API_KEY'] is called before .env file is loaded

✓ Fix

Call load_dotenv() from python-dotenv at the very top of your main script BEFORE importing or instantiating any CrewAI tools. Place it before any tool or agent initialization.

3

Using os.environ.get('SERPER_API_KEY') without a fallback, then passing empty string or None to SerperDevTool

✓ Fix

SerperDevTool does not accept a key parameter: it reads directly from os.environ. Ensure the env var is set before tool initialization, not passed as an argument.

4

Running CrewAI script in a different shell or environment context where SERPER_API_KEY was not exported

✓ Fix

Export the variable before running: export SERPER_API_KEY='your-key' && python main.py (Linux/Mac), or set SERPER_API_KEY=your-key && python main.py (Windows PowerShell)

Code: broken vs fixed

Broken - triggers the error
python
import os
from crewai import Agent, Task, Crew
from crewai.tools import SerperDevTool

# BUG: SerperDevTool initialized WITHOUT checking if SERPER_API_KEY is set
search_tool = SerperDevTool()  # This line will raise ValueError

agent = Agent(
    role="Research Analyst",
    goal="Search for information",
    tools=[search_tool]
)

print("Agent created")  # Never reaches here
Fixed - works correctly
python
import os
from dotenv import load_dotenv
from crewai import Agent, Task, Crew
from crewai.tools import SerperDevTool

# FIX: Load .env file FIRST, then check if SERPER_API_KEY is set
load_dotenv()

if not os.environ.get('SERPER_API_KEY'):
    raise ValueError(
        "SERPER_API_KEY environment variable is not set. "
        "Please add it to your .env file or system environment."
    )

search_tool = SerperDevTool()  # Now safe — SERPER_API_KEY is loaded

agent = Agent(
    role="Research Analyst",
    goal="Search for information",
    tools=[search_tool]
)

print(f"Agent created successfully with search tool")
Added load_dotenv() at the top to load .env file variables, then added an explicit check for SERPER_API_KEY before tool initialization so errors are caught early with a clear message.

Workaround

If you cannot set environment variables in your current environment, temporarily hardcode the key directly by modifying CrewAI's tool instantiation (though not recommended for production). Alternatively, create a wrapper function that accepts the key as a parameter and sets os.environ['SERPER_API_KEY'] = key before importing SerperDevTool, then instantiate: os.environ['SERPER_API_KEY'] = your_key; search_tool = SerperDevTool(). This only works if you control where the key comes from (e.g., a secure config service).

Prevention

Adopt a startup validation pattern: create a function that checks all required API keys (SERPER_API_KEY, OPENAI_API_KEY, etc.) on app launch, before any agents or tasks run. Use a secrets manager (AWS Secrets Manager, HashiCorp Vault, GitHub Secrets) in production instead of .env files. For local development, version-control a .env.example file with placeholder values and require developers to copy and fill it. Add unit tests that mock SerperDevTool to catch missing env vars in CI/CD before deployment.

Python 3.9+ · crewai >=0.30.0 · tested on 0.55.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.