FoundationAgents/MetaGPT · error · ValueError

To use google search engine, make sure you provide the `api_

Error message

To use google search engine, make sure you provide the `api_key` when constructing an object. You can obtain an API key from https://console.cloud.google.com/apis/credentials.

What it means

ValueError from GoogleAPIWrapper's pydantic model_validator (mode='before'): the constructor dict contains neither api_key nor the deprecated google_api_key alias. Google Custom Search requires an API key, so the model refuses instantiation. The validator first maps google_api_key -> api_key (with a DeprecationWarning), so either name works.

Source

Thrown at metagpt/tools/search_engine_googleapi.py:43

    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
    def validate_google(cls, values: dict) -> dict:
        if "google_api_key" in values:
            values.setdefault("api_key", values["google_api_key"])
            warnings.warn("`google_api_key` is deprecated, use `api_key` instead", DeprecationWarning, stacklevel=2)

        if "api_key" not in values:
            raise ValueError(
                "To use google search engine, make sure you provide the `api_key` when constructing an object. You can obtain "
                "an API key from https://console.cloud.google.com/apis/credentials."
            )

        if "google_cse_id" in values:
            values.setdefault("cse_id", values["google_cse_id"])
            warnings.warn("`google_cse_id` is deprecated, use `cse_id` instead", DeprecationWarning, stacklevel=2)

        if "cse_id" not in values:
            raise ValueError(
                "To use google search engine, make sure you provide the `cse_id` when constructing an object. You can obtain "
                "the cse_id from https://programmablesearchengine.google.com/controlpanel/create."
            )
        return values

    @property
    def google_api_client(self):
        build_kwargs = {"developerKey": self.api_key, "discoveryServiceUrl": self.discovery_service_url}

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. Pass api_key explicitly: GoogleAPIWrapper(api_key='...', cse_id='...')
  2. Or set the key in your MetaGPT config under the search engine section / env so it lands in the constructor values
  3. The legacy google_api_key name still works but warns; prefer api_key

Example fix

# before
engine = GoogleAPIWrapper()  # ValueError
# after
engine = GoogleAPIWrapper(api_key=os.environ["GOOGLE_API_KEY"], cse_id=os.environ["GOOGLE_CSE_ID"])
Defensive patterns

Strategy: validation

Validate before calling

import os
api_key = os.environ.get("GOOGLE_API_KEY")
if not api_key:
    raise SystemExit("set GOOGLE_API_KEY before constructing GoogleAPIWrapper")

Try / catch

try:
    engine = GoogleAPIWrapper(api_key=k, cse_id=c)
except ValueError as e:
    raise ConfigError(str(e))  # surface as a config problem, not a crash

Prevention

When it happens

Trigger: GoogleAPIWrapper() with no args; constructing from config where neither `api_key` nor legacy `google_api_key` is set (common when relying on old .yaml config files that omit it).

Common situations: Switching SearchEngine to googleapi in config2.yaml without adding credentials; env var for the key missing so config renders an empty value; code migrated to new field name but config not updated.

Related errors


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