BerriAI/litellm · error · BadRequestError
litellm.BadRequestError: SagemakerException
Error message
litellm.BadRequestError: SagemakerException - {error_str} What it means
LiteLLM throws BadRequestError from _map_sagemaker_exception when the SageMaker runtime call reports 'Unable to locate credentials'. Notably, SageMaker maps missing AWS credentials to a 400 BadRequestError while the Bedrock mapper promotes the same string to AuthenticationError - so exception handlers must catch a different type per provider. Retrying without provisioning credentials cannot succeed.
Solutions
- Provision AWS credentials (aws configure / env vars / attached role) and verify with aws sts get-caller-identity
- Pass aws_access_key_id / aws_secret_access_key / aws_region_name explicitly to the sagemaker completion call
- Catch litellm.BadRequestError for this SageMaker path (not AuthenticationError) in your handler
- Track upstream litellm: propose mapping this string to AuthenticationError for parity with Bedrock
Example fix
# before litellm.completion(model="sagemaker/my-endpoint", messages=msgs) # Unable to locate credentials # after import os os.environ['AWS_ACCESS_KEY_ID'] = '...' os.environ['AWS_SECRET_ACCESS_KEY'] = '...' os.environ['AWS_REGION'] = 'us-east-1' litellm.completion(model="sagemaker/my-endpoint", messages=msgs)
Defensive patterns
Strategy: validation
Validate before calling
import boto3
def has_aws_credentials() -> bool:
return boto3.session.Session().get_credentials() is not None
# assert before any sagemaker/ call — note the mapped type is BadRequestError, not AuthenticationError Type guard
import litellm
def is_sagemaker_creds_error(e: BaseException) -> bool:
return isinstance(e, litellm.BadRequestError) and 'Unable to locate credentials' in str(e) Try / catch
try:
resp = litellm.completion(model='sagemaker/my-endpoint', messages=msgs)
except litellm.BadRequestError as e: # NOT AuthenticationError on this path
if 'Unable to locate credentials' in str(e):
raise RuntimeError('configure AWS credentials (aws configure / role)') from e
raise Prevention
- Pre-flight boto3.session.Session().get_credentials() before sagemaker calls
- Catch BadRequestError for sagemaker credential failures - the mapper differs from Bedrock's
- Attach IAM roles to compute running sagemaker inference so creds auto-rotate
- Health-check endpoints with a tiny completion at startup to surface credential issues early
When it happens
Trigger: Calling litellm.completion(model='sagemaker/<endpoint-name>', ...) when no AWS credentials are resolvable: no env vars, no ~/.aws/credentials, no attached instance/task role.
Common situations: Local scripts without aws configure; containers or CI jobs without role assumption; AWS_PROFILE pointing at a missing profile; catch clauses written for Bedrock's AuthenticationError silently missing this error.
Related errors
- Bedrock Mantle auth failed: no Bearer token and no usable…
- BedrockException
- BedrockException Invalid Authentication
- BFL initial request failed
- Client-side 'metadata.tags' not allowed in request…
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/7fddb8c18da540aa.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/litellm_core_utils/exception_mapping_utils.py:978
model=model,
llm_provider=custom_llm_provider,
litellm_debug_info=extra_information,
exception_status_code=original_exception.status_code,
)
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}",View on GitHub (pinned to 77b7c6c40c)