{"record":{"id":"051c7fb87ca8abfe","repo":"microsoft/semantic-kernel","slug":"failed-to-get-search-results-051c7f","errorCode":null,"errorMessage":"Failed to get search results.","messagePattern":"Failed to get search results\\.","errorType":"exception","errorClass":"ServiceInvalidRequestError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/google_search.py","lineNumber":273,"sourceCode":"\n    async def _inner_search(self, query: str, options: SearchOptions) -> GoogleSearchResponse:\n        self._validate_options(options)\n\n        logger.info(\n            f\"Received request for google web search with \\\n                params:\\nnum_results: {options.top}\\noffset: {options.skip}\"\n        )\n\n        full_url = f\"{CUSTOM_SEARCH_URL}{self._build_query(query, options)}\"\n        headers = {\"user_agent\": SEMANTIC_KERNEL_USER_AGENT}\n        try:\n            async with AsyncClient(timeout=5) as client:\n                response = await client.get(full_url, headers=headers)\n                response.raise_for_status()\n                return GoogleSearchResponse.model_validate_json(response.text)\n        except HTTPStatusError as ex:\n            logger.error(f\"Failed to get search results: {ex}\")\n            raise ServiceInvalidRequestError(\"Failed to get search results.\") from ex\n        except RequestError as ex:\n            logger.error(f\"Client error occurred: {ex}\")\n            raise ServiceInvalidRequestError(\"A client error occurred while getting search results.\") from ex\n        except Exception as ex:\n            logger.error(f\"An unexpected error occurred: {ex}\")\n            raise ServiceInvalidRequestError(\"An unexpected error occurred while getting search results.\") from ex\n\n    def _validate_options(self, options: SearchOptions) -> None:\n        if options.top > 10:\n            raise ServiceInvalidRequestError(\"count value must be less than or equal to 10.\")\n\n    def _parse_filter_lambda(self, filter_lambda: Callable | str) -> list[dict[str, str]]:\n        \"\"\"Parse a string lambda or string expression into a list of {field: value} dicts using AST.\"\"\"\n        expr = filter_lambda if isinstance(filter_lambda, str) else getsource(filter_lambda).strip()\n        tree = ast.parse(expr, mode=\"eval\")\n        node = tree.body\n        visitor = SearchLambdaVisitor(valid_parameters=QUERY_PARAMETERS)\n        visitor.visit(node)","sourceCodeStart":255,"sourceCodeEnd":291,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/google_search.py#L255-L291","documentation":"_inner_search issues an async GET to the Google Custom Search API and calls response.raise_for_status(). Any HTTP 4xx/5xx becomes an httpx.HTTPStatusError, which is logged and re-raised as ServiceInvalidRequestError('Failed to get search results.'). The original HTTPStatusError is preserved in __cause__ so the status code and body are recoverable.","triggerScenarios":"HTTP 403 (invalid/disabled API key), 400 (bad cx / engine_id or malformed query params), 429 (quota/rate limit exceeded), or 5xx (Google server error) returned by https://www.googleapis.com/customsearch/v1.","commonSituations":"Wrong or revoked API key; incorrect search_engine_id (cx); daily quota exhausted on the free tier; billing disabled; transient Google 5xx during outages.","solutions":["Inspect ex.__cause__.response.status_code and .text to identify the exact HTTP failure.","Verify the API key and cx (engine_id) are valid and enabled in the Google Cloud / Programmable Search console.","For 429, back off and retry with exponential backoff; enable billing or raise quota.","For 5xx, retry a limited number of times before surfacing the error to the user."],"exampleFix":"# before\nresults = await google.search('skates')  # 403/429 -> [1303]\n\n# after\nfrom semantic_kernel.exceptions import ServiceInvalidRequestError\ntry:\n    results = await google.search('skates')\nexcept ServiceInvalidRequestError as ex:\n    resp = ex.__cause__.response\n    if resp.status_code in (429, 500, 502, 503):\n        await asyncio.sleep(2 ** attempt)\n        results = await google.search('skates')\n    else:\n        raise","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"from semantic_kernel.exceptions import ServiceInvalidRequestError\n\nRETRYABLE = {429, 500, 502, 503, 504}\nfor attempt in range(5):\n    try:\n        results = await google.search(query, top=top)\n        break\n    except ServiceInvalidRequestError as ex:\n        cause = ex.__cause__\n        status = getattr(getattr(cause, 'response', None), 'status_code', None)\n        if status in RETRYABLE and attempt < 4:\n            await asyncio.sleep(2 ** attempt)\n            continue\n        raise","preventionTips":["Inspect ex.__cause__.response.status_code to distinguish key/quota/server errors.","Keep cx (engine_id) and api_key valid and enabled in the Google console.","Treat 429/5xx as retryable; surface 4xx (other than 429) as configuration problems."],"tags":["google-search","network","http","quota"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}