{"record":{"id":"ae745cf4b9d1405c","repo":"sgl-project/sglang","slug":"value-error-parameter-top-n-should-be-larger-than","errorCode":null,"errorMessage":"Value error, parameter top_n should be larger than 0.","messagePattern":"Value error, parameter top_n should be larger than 0\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/entrypoints/openai/protocol.py","lineNumber":1416,"sourceCode":"    instruct: Optional[str] = Field(\n        default=None,\n        description=\"The instruct to the reranker model.\",\n    )\n    top_n: Optional[int] = Field(\n        default=None,\n        description=\"Maximum number of documents to return. Defaults to returning all documents. \"\n        \"If specified value is greater than the total number of documents, all documents will be returned.\",\n    )\n    return_documents: bool = Field(\n        default=True,\n        description=\"Whether to return documents in the response. Only included when set to true.\",\n    )\n\n    @field_validator(\"top_n\")\n    @classmethod\n    def validate_top_n(cls, v):\n        if v is not None and v < 1:\n            raise ValueError(\"Value error, parameter top_n should be larger than 0.\")\n        return v\n\n    def is_multimodal(self) -> bool:\n        \"\"\"Check if the request contains any multimodal content.\"\"\"\n        if isinstance(self.query, list):\n            return True\n        for doc in self.documents:\n            if isinstance(doc, list):\n                return True\n        return False\n\n\nclass RerankResponse(BaseModel):\n    score: float\n    document: Optional[str] = None\n    index: int\n    meta_info: Optional[dict] = None\n","sourceCodeStart":1398,"sourceCodeEnd":1434,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/entrypoints/openai/protocol.py#L1398-L1434","documentation":"EmbeddingRequest.validate_top_n rejects top_n values less than 1 (0 or negative). top_n asks for the top-N embedding results, so it must be a positive integer.","triggerScenarios":"POST /v1/embeddings (or the embeddings endpoint using EmbeddingRequest) with top_n=0 or top_n=-3.","commonSituations":"Defaulting numeric params to 0 in client configs; passing -1 meaning 'all'; confusion with top_k sampling parameters elsewhere.","solutions":["Set top_n >= 1","Omit top_n to use the default","If you wanted 'all', pass the actual count of items"],"exampleFix":"// before\n{\"input\": \"text\", \"top_n\": 0}\n// after\n{\"input\": \"text\", \"top_n\": 5}","handlingStrategy":"validation","validationCode":"if body.get(\"top_n\") is not None:\n    assert isinstance(body[\"top_n\"], int) and body[\"top_n\"] >= 1, \"top_n must be >= 1\"","typeGuard":"def valid_top_n(n): return n is None or (isinstance(n, int) and n >= 1)","tryCatchPattern":null,"preventionTips":["Don't reuse -1/0 sentinel defaults across APIs","Validate positive integers before submit"],"tags":["embeddings","validation","openai-api","sglang"],"backgroundTag":"parameter-out-of-range","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}