{"record":{"id":"c62b421b832852d4","repo":"microsoft/semantic-kernel","slug":"count-value-must-be-greater-than-0","errorCode":null,"errorMessage":"count value must be greater than 0.","messagePattern":"count value must be greater than 0\\.","errorType":"exception","errorClass":"ServiceInvalidRequestError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/brave.py","lineNumber":245,"sourceCode":"        }\n        try:\n            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)","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/brave.py#L227-L263","documentation":"A validation guard in _validate_options: Brave's web search count parameter must be at least 1. Passing top <= 0 raises ServiceInvalidRequestError before any network call is made. This is caller-side input validation, not a Brave API response.","triggerScenarios":"Calling search() with top=0 or a negative number, either directly or via SearchOptions(top=...). The check at brave.py:245-246 fires immediately inside _inner_search.","commonSituations":"A default of 0 passed from upstream pagination logic, a computed top that underflows to 0, or reusing an options object whose top was zeroed out.","solutions":["Pass top >= 1 (e.g. top=5 default) when calling search.","Clamp user-supplied count to a minimum of 1 before calling search.","Validate pagination inputs upstream so top is never zero or negative."],"exampleFix":"// before\nresults = await connector.search(query, top=0)\n// after\nresults = await connector.search(query, top=max(1, user_count))\n","handlingStrategy":"validation","validationCode":"if top <= 0:\n    raise ValueError(\"top must be >= 1 for Brave search\")\nresults = await connector.search(query, top=top)","typeGuard":"def is_valid_top(top: int) -> bool:\n    return isinstance(top, int) and 1 <= top <= 20","tryCatchPattern":null,"preventionTips":["Clamp top to the [1, 20] range before calling search.","Default top to a sane positive value (e.g. 5) in pagination helpers."],"tags":["brave","validation","input"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}