{"record":{"id":"4dc563b0584dc16f","repo":"BerriAI/litellm","slug":"sagemakerexception-the-value-of-n-must-be-0","errorCode":null,"errorMessage":"SagemakerException - the value of 'n' must be > 0 and <= 2 for sagemaker endpoints","messagePattern":"SagemakerException - the value of 'n' must be > 0 and <= 2 for sagemaker endpoints","errorType":"exception","errorClass":"BadRequestError","httpStatus":400,"severity":"error","filePath":"litellm/litellm_core_utils/exception_mapping_utils.py","lineNumber":985,"sourceCode":"def _map_sagemaker_exception(\n    *,\n    model: str,\n    original_exception: _ProviderHTTPException,\n    custom_llm_provider: str,\n    error_str: str,\n    exception_type: str,\n    exception_provider: str,\n    extra_information: str,\n) -> None:\n    if \"Unable to locate credentials\" in error_str:\n        raise BadRequestError(\n            message=f\"litellm.BadRequestError: SagemakerException - {error_str}\",\n            model=model,\n            llm_provider=\"sagemaker\",\n            response=getattr(original_exception, \"response\", None),\n        )\n    elif \"Input validation error: `best_of` must be > 0 and <= 2\" in error_str:\n        raise BadRequestError(\n            message=\"SagemakerException - the value of 'n' must be > 0 and <= 2 for sagemaker endpoints\",\n            model=model,\n            llm_provider=\"sagemaker\",\n            response=getattr(original_exception, \"response\", None),\n        )\n    elif (\n        \"`inputs` tokens + `max_new_tokens` must be <=\" in error_str\n        or \"instance type with more CPU capacity or memory\" in error_str\n    ):\n        raise ContextWindowExceededError(\n            message=f\"SagemakerException - {error_str}\",\n            model=model,\n            llm_provider=\"sagemaker\",\n            response=getattr(original_exception, \"response\", None),\n        )\n    elif hasattr(original_exception, \"status_code\"):\n        if original_exception.status_code == 500:\n            raise ServiceUnavailableError(","sourceCodeStart":967,"sourceCodeEnd":1003,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/exception_mapping_utils.py#L967-L1003","documentation":"LiteLLM throws BadRequestError with a fixed human-readable message - 'the value of n must be > 0 and <= 2 for sagemaker endpoints' - when the SageMaker endpoint (HuggingFace TGI-style) validates best_of and rejects values above 2. LiteLLM forwards the OpenAI-style n parameter as best_of to these endpoints, so the OpenAI range for n does not apply.","triggerScenarios":"litellm.completion(model='sagemaker/...', n=3) or higher: litellm maps n to best_of, and the deployed endpoint enforces best_of <= 2, rejecting anything larger.","commonSituations":"Porting OpenAI code that used n>2 for candidate generation to SageMaker endpoints; assuming OpenAI's n semantics (up to 5+) transfer to every provider; UI layers exposing a free-form 'number of completions' control over sagemaker models.","solutions":["Set n <= 2 (or omit n entirely) for sagemaker calls","If you need more candidates, issue parallel independent completion calls instead of one n=N call","Clamp or hide n > 2 in any UI/API layer that fronts sagemaker models"],"exampleFix":"# before\nresp = litellm.completion(model=\"sagemaker/my-endpoint\", messages=msgs, n=4)  # BadRequestError\n\n# after\nresp = litellm.completion(model=\"sagemaker/my-endpoint\", messages=msgs, n=2)\n# or fan out for more candidates:\n# resps = [litellm.completion(model=\"sagemaker/my-endpoint\", messages=msgs, temperature=0.9) for _ in range(4)]","handlingStrategy":"validation","validationCode":"def sagemaker_n(n: int | None) -> int | None:\n    if n is None:\n        return None\n    if not 0 < n <= 2:\n        raise ValueError('sagemaker endpoints accept n in (0, 2]')\n    return n\n\n# litellm.completion(model='sagemaker/...', messages=msgs, n=sagemaker_n(n))","typeGuard":"import litellm\n\ndef is_sagemaker_n_error(e: BaseException) -> bool:\n    return isinstance(e, litellm.BadRequestError) and \"'n' must be > 0 and <= 2\" in str(e)","tryCatchPattern":"try:\n    resp = litellm.completion(model='sagemaker/my-endpoint', messages=msgs, n=n)\nexcept litellm.BadRequestError as e:\n    if \"'n' must be > 0 and <= 2\" in str(e):\n        resp = litellm.completion(model='sagemaker/my-endpoint', messages=msgs, n=2)\n    else:\n        raise","preventionTips":["Clamp or reject n > 2 in API/UI layers fronting sagemaker models","Get more candidates via parallel independent calls, not one n=N call","Remember litellm maps n to best_of on these TGI-style endpoints","Encode per-provider parameter limits in a shared validation module"],"tags":["sagemaker","parameter-range","best-of","n"],"backgroundTag":"parameter-out-of-range","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}