{"record":{"id":"4589495706a812c1","repo":"microsoft/semantic-kernel","slug":"count-value-must-be-less-than-21","errorCode":null,"errorMessage":"count value must be less than 21.","messagePattern":"count value must be less than 21\\.","errorType":"exception","errorClass":"ServiceInvalidRequestError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/brave.py","lineNumber":247,"sourceCode":"            async with AsyncClient(timeout=5) as client:\n                response = await client.get(url, headers=headers, params=params)\n                response.raise_for_status()\n                return BraveSearchResponse.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 <= 0:\n            raise ServiceInvalidRequestError(\"count value must be greater than 0.\")\n        if options.top >= 21:\n            raise ServiceInvalidRequestError(\"count value must be less than 21.\")\n\n        if options.skip < 0:\n            raise ServiceInvalidRequestError(\"offset must be greater than or equal to 0.\")\n        if options.skip > 9:\n            raise ServiceInvalidRequestError(\"offset must be less than 10.\")\n\n    def _get_url(self) -> str:\n        return DEFAULT_URL\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)\n        return visitor.filters\n","sourceCodeStart":229,"sourceCodeEnd":265,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/brave.py#L229-L265","documentation":"Brave's web search API caps results at 20 per request; the connector enforces this client-side by rejecting top >= 21 with ServiceInvalidRequestError before the call. This prevents an guaranteed API-side rejection.","triggerScenarios":"Calling search() with top >= 21 (e.g. top=50 to fetch a page). The check at brave.py:247-248 fires in _validate_options.","commonSituations":"Pagination logic requesting large pages, or confusing Brave's per-request cap with the total available results.","solutions":["Lower top to <= 20 per request.","For more than 20 results, paginate with skip/top across multiple requests (respecting the skip <= 9 cap).","Clamp user-supplied count to 20 before calling search."],"exampleFix":"// before\nresults = await connector.search(query, top=50)\n// after\nresults = await connector.search(query, top=min(20, user_count))\n","handlingStrategy":"validation","validationCode":"if top > 20:\n    raise ValueError(\"Brave caps results at 20 per request\")\nresults = await connector.search(query, top=min(20, top))","typeGuard":"def is_valid_top(top: int) -> bool:\n    return isinstance(top, int) and 1 <= top <= 20","tryCatchPattern":null,"preventionTips":["Cap top at 20 per request; paginate with skip for more.","Validate count inputs upstream against Brave's per-request limit."],"tags":["brave","validation","input"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}