FoundationAgents/MetaGPT · error · ImportError

To use this module, you should have the `google-api-python-c

Error message

To use this module, you should have the `google-api-python-client` Python package installed. You can install it by running the command: `pip install -e.[search-google]`

What it means

Module-level ImportError raised while importing metagpt.tools.search_engine_googleapi: googleapiclient.discovery (provided by google-api-python-client) is not installed. Like the DDG module, it fails on import, so merely referencing the module in config can crash a run.

Source

Thrown at metagpt/tools/search_engine_googleapi.py:18

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import annotations

import asyncio
import json
import warnings
from concurrent import futures
from typing import Optional
from urllib.parse import urlparse

import httplib2
from pydantic import BaseModel, ConfigDict, model_validator

try:
    from googleapiclient.discovery import build
except ImportError:
    raise ImportError(
        "To use this module, you should have the `google-api-python-client` Python package installed. "
        "You can install it by running the command: `pip install -e.[search-google]`"
    )


class GoogleAPIWrapper(BaseModel):
    model_config = ConfigDict(arbitrary_types_allowed=True)

    api_key: str
    cse_id: str
    discovery_service_url: Optional[str] = None

    loop: Optional[asyncio.AbstractEventLoop] = None
    executor: Optional[futures.Executor] = None
    proxy: Optional[str] = None

    @model_validator(mode="before")
    @classmethod

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. Install the extra: pip install -e.[search-google]
  2. Verify: python -c "from googleapiclient.discovery import build"
  3. Or use an already-installed engine (e.g. serper/serpapi) instead

Example fix

# before: ImportError on import
from metagpt.tools.search_engine_googleapi import GoogleAPIWrapper
# after
# pip install -e.[search-google]
from metagpt.tools.search_engine_googleapi import GoogleAPIWrapper(api_key=..., cse_id=...)
Defensive patterns

Strategy: fallback

Validate before calling

try:
    from googleapiclient.discovery import build  # noqa: F401
    ok = True
except ImportError:
    ok = False

Try / catch

try:
    from metagpt.tools.search_engine_googleapi import GoogleAPIWrapper
except ImportError:
    from metagpt.tools.search_engine_ddg import DDGAPIWrapper  # fallback engine

Prevention

When it happens

Trigger: import metagpt.tools.search_engine_googleapi or setting GoogleAPIWrapper as the search provider without the search-google extra installed.

Common situations: Core-only MetaGPT install; switching SearchEngine to 'GoogleAPIWrapper' in config without adding deps; fresh environment built from a slim requirements list.

Related errors


AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14). Data as JSON: /api/errors/368d3eb469d69fcd. Report an issue: GitHub.