microsoft/semantic-kernel · error · ServiceInvalidRequestError

Failed to get search results.

Error message

Failed to get search results.

What it means

Raised when the Brave API GET request returns an HTTP error status (response.raise_for_status() throws HTTPStatusError). This is a ServiceInvalidRequestError — the request reached Brave but was rejected (4xx/5xx). The underlying HTTPStatusError is chained.

Source

Thrown at python/semantic_kernel/connectors/brave.py:235

        )

        url = self._get_url()
        params = self._build_request_parameters(query, options)

        logger.info(f"Sending GET request to {url}")

        headers = {
            "X-Subscription-Token": self.settings.api_key.get_secret_value(),
            "user_agent": SEMANTIC_KERNEL_USER_AGENT,
        }
        try:
            async with AsyncClient(timeout=5) as client:
                response = await client.get(url, headers=headers, params=params)
                response.raise_for_status()
                return BraveSearchResponse.model_validate_json(response.text)
        except HTTPStatusError as ex:
            logger.error(f"Failed to get search results: {ex}")
            raise ServiceInvalidRequestError("Failed to get search results.") from ex
        except RequestError as ex:
            logger.error(f"Client error occurred: {ex}")
            raise ServiceInvalidRequestError("A client error occurred while getting search results.") from ex
        except Exception as ex:
            logger.error(f"An unexpected error occurred: {ex}")
            raise ServiceInvalidRequestError("An unexpected error occurred while getting search results.") from ex

    def _validate_options(self, options: SearchOptions) -> None:
        if options.top <= 0:
            raise ServiceInvalidRequestError("count value must be greater than 0.")
        if options.top >= 21:
            raise ServiceInvalidRequestError("count value must be less than 21.")

        if options.skip < 0:
            raise ServiceInvalidRequestError("offset must be greater than or equal to 0.")
        if options.skip > 9:
            raise ServiceInvalidRequestError("offset must be less than 10.")

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Inspect exc.__cause__ (HTTPStatusError) — its response.status_code identifies the problem: 401 → fix key, 429 → slow down / upgrade plan.
  2. For 429, implement exponential backoff retry at the caller and respect Retry-After.
  3. Confirm the API key is valid and the Brave plan covers the request volume.

Example fix

// before
results = await connector.search("python tutorials")
// after
try:
    results = await connector.search("python tutorials")
except ServiceInvalidRequestError as e:
    code = getattr(e.__cause__, "response", None) and e.__cause__.response.status_code
    raise RuntimeError(f"Brave HTTP error {code}: {e.__cause__}") from e
Defensive patterns

Strategy: retry

Try / catch

from semantic_kernel.exceptions import ServiceInvalidRequestError

for attempt in range(4):
    try:
        results = await connector.search(query, top=top)
        break
    except ServiceInvalidRequestError as e:
        resp = getattr(e.__cause__, "response", None)
        code = getattr(resp, "status_code", None)
        if code == 429:
            import asyncio
            await asyncio.sleep(2 ** attempt)
            continue
        raise

Prevention

When it happens

Trigger: Calling search() and the Brave endpoint responds with a non-2xx status: 401 unauthorized (bad/expired API key), 403 forbidden (plan/quota), 429 rate limit, or 5xx server errors. The HTTPStatusError branch at brave.py:233-235 fires.

Common situations: Expired or wrong API key (401), exceeding the Brave subscription quota (429/402), endpoint URL change, or transient Brave-side outages (5xx).

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/597823b15105758f. Report an issue: GitHub.