{"record":{"id":"0a7bf6916d787fa3","repo":"FoundationAgents/MetaGPT","slug":"to-use-google-search-engine-make-sure-you-provide","errorCode":null,"errorMessage":"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.","messagePattern":"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\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"metagpt/tools/search_engine_googleapi.py","lineNumber":43,"sourceCode":"    model_config = ConfigDict(arbitrary_types_allowed=True)\n\n    api_key: str\n    cse_id: str\n    discovery_service_url: Optional[str] = None\n\n    loop: Optional[asyncio.AbstractEventLoop] = None\n    executor: Optional[futures.Executor] = None\n    proxy: Optional[str] = None\n\n    @model_validator(mode=\"before\")\n    @classmethod\n    def validate_google(cls, values: dict) -> dict:\n        if \"google_api_key\" in values:\n            values.setdefault(\"api_key\", values[\"google_api_key\"])\n            warnings.warn(\"`google_api_key` is deprecated, use `api_key` instead\", DeprecationWarning, stacklevel=2)\n\n        if \"api_key\" not in values:\n            raise ValueError(\n                \"To use google search engine, make sure you provide the `api_key` when constructing an object. You can obtain \"\n                \"an API key from https://console.cloud.google.com/apis/credentials.\"\n            )\n\n        if \"google_cse_id\" in values:\n            values.setdefault(\"cse_id\", values[\"google_cse_id\"])\n            warnings.warn(\"`google_cse_id` is deprecated, use `cse_id` instead\", DeprecationWarning, stacklevel=2)\n\n        if \"cse_id\" not in values:\n            raise ValueError(\n                \"To use google search engine, make sure you provide the `cse_id` when constructing an object. You can obtain \"\n                \"the cse_id from https://programmablesearchengine.google.com/controlpanel/create.\"\n            )\n        return values\n\n    @property\n    def google_api_client(self):\n        build_kwargs = {\"developerKey\": self.api_key, \"discoveryServiceUrl\": self.discovery_service_url}","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/tools/search_engine_googleapi.py#L25-L61","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Pass api_key explicitly: GoogleAPIWrapper(api_key='...', cse_id='...')","Or set the key in your MetaGPT config under the search engine section / env so it lands in the constructor values","The legacy google_api_key name still works but warns; prefer api_key"],"exampleFix":"# before\nengine = GoogleAPIWrapper()  # ValueError\n# after\nengine = GoogleAPIWrapper(api_key=os.environ[\"GOOGLE_API_KEY\"], cse_id=os.environ[\"GOOGLE_CSE_ID\"])","handlingStrategy":"validation","validationCode":"import os\napi_key = os.environ.get(\"GOOGLE_API_KEY\")\nif not api_key:\n    raise SystemExit(\"set GOOGLE_API_KEY before constructing GoogleAPIWrapper\")","typeGuard":null,"tryCatchPattern":"try:\n    engine = GoogleAPIWrapper(api_key=k, cse_id=c)\nexcept ValueError as e:\n    raise ConfigError(str(e))  # surface as a config problem, not a crash","preventionTips":["Fail fast on missing credentials at app startup, not at first search","Keep api_key/cse_id in env or .env and inject explicitly","Update legacy google_api_key configs to api_key"],"tags":["search","google","config","credentials","pydantic"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}