BerriAI/litellm · error · BadRequestError
SagemakerException - the value of 'n' must be > 0 and <= 2…
Error message
SagemakerException - the value of 'n' must be > 0 and <= 2 for sagemaker endpoints
What it means
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.
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
Example fix
# before resp = litellm.completion(model="sagemaker/my-endpoint", messages=msgs, n=4) # BadRequestError # after resp = litellm.completion(model="sagemaker/my-endpoint", messages=msgs, n=2) # or fan out for more candidates: # resps = [litellm.completion(model="sagemaker/my-endpoint", messages=msgs, temperature=0.9) for _ in range(4)]
Defensive patterns
Strategy: validation
Validate before calling
def sagemaker_n(n: int | None) -> int | None:
if n is None:
return None
if not 0 < n <= 2:
raise ValueError('sagemaker endpoints accept n in (0, 2]')
return n
# litellm.completion(model='sagemaker/...', messages=msgs, n=sagemaker_n(n)) Type guard
import litellm
def is_sagemaker_n_error(e: BaseException) -> bool:
return isinstance(e, litellm.BadRequestError) and "'n' must be > 0 and <= 2" in str(e) Try / catch
try:
resp = litellm.completion(model='sagemaker/my-endpoint', messages=msgs, n=n)
except litellm.BadRequestError as e:
if "'n' must be > 0 and <= 2" in str(e):
resp = litellm.completion(model='sagemaker/my-endpoint', messages=msgs, n=2)
else:
raise Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Failed to parse response
- HF response not in expected format
- Input must be a list of strings
- litellm.BadRequestError: SagemakerException
- LiteLLM Error: Unable to parse sagemaker RAW RESPONSE
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/4dc563b0584dc16f.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/litellm_core_utils/exception_mapping_utils.py:985
def _map_sagemaker_exception(
*,
model: str,
original_exception: _ProviderHTTPException,
custom_llm_provider: str,
error_str: str,
exception_type: str,
exception_provider: str,
extra_information: str,
) -> None:
if "Unable to locate credentials" in error_str:
raise BadRequestError(
message=f"litellm.BadRequestError: SagemakerException - {error_str}",
model=model,
llm_provider="sagemaker",
response=getattr(original_exception, "response", None),
)
elif "Input validation error: `best_of` must be > 0 and <= 2" in error_str:
raise BadRequestError(
message="SagemakerException - the value of 'n' must be > 0 and <= 2 for sagemaker endpoints",
model=model,
llm_provider="sagemaker",
response=getattr(original_exception, "response", None),
)
elif (
"`inputs` tokens + `max_new_tokens` must be <=" in error_str
or "instance type with more CPU capacity or memory" in error_str
):
raise ContextWindowExceededError(
message=f"SagemakerException - {error_str}",
model=model,
llm_provider="sagemaker",
response=getattr(original_exception, "response", None),
)
elif hasattr(original_exception, "status_code"):
if original_exception.status_code == 500:
raise ServiceUnavailableError(View on GitHub (pinned to 77b7c6c40c)