{"record":{"id":"7fddb8c18da540aa","repo":"BerriAI/litellm","slug":"litellm-badrequesterror-sagemakerexception-err","errorCode":null,"errorMessage":"litellm.BadRequestError: SagemakerException - {error_str}","messagePattern":"litellm\\.BadRequestError: SagemakerException - (.+?)","errorType":"exception","errorClass":"BadRequestError","httpStatus":400,"severity":"error","filePath":"litellm/litellm_core_utils/exception_mapping_utils.py","lineNumber":978,"sourceCode":"                model=model,\n                llm_provider=custom_llm_provider,\n                litellm_debug_info=extra_information,\n                exception_status_code=original_exception.status_code,\n            )\n\n\ndef _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}\",","sourceCodeStart":960,"sourceCodeEnd":996,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/exception_mapping_utils.py#L960-L996","documentation":"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.","triggerScenarios":"Calling litellm.completion(model='sagemaker/<endpoint-name>', ...) when no AWS credentials are resolvable: no env vars, no ~/.aws/credentials, no attached instance/task role.","commonSituations":"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.","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"],"exampleFix":"# before\nlitellm.completion(model=\"sagemaker/my-endpoint\", messages=msgs)  # Unable to locate credentials\n\n# after\nimport os\nos.environ['AWS_ACCESS_KEY_ID'] = '...'\nos.environ['AWS_SECRET_ACCESS_KEY'] = '...'\nos.environ['AWS_REGION'] = 'us-east-1'\nlitellm.completion(model=\"sagemaker/my-endpoint\", messages=msgs)","handlingStrategy":"validation","validationCode":"import boto3\n\ndef has_aws_credentials() -> bool:\n    return boto3.session.Session().get_credentials() is not None\n\n# assert before any sagemaker/ call — note the mapped type is BadRequestError, not AuthenticationError","typeGuard":"import litellm\n\ndef is_sagemaker_creds_error(e: BaseException) -> bool:\n    return isinstance(e, litellm.BadRequestError) and 'Unable to locate credentials' in str(e)","tryCatchPattern":"try:\n    resp = litellm.completion(model='sagemaker/my-endpoint', messages=msgs)\nexcept litellm.BadRequestError as e:  # NOT AuthenticationError on this path\n    if 'Unable to locate credentials' in str(e):\n        raise RuntimeError('configure AWS credentials (aws configure / role)') from e\n    raise","preventionTips":["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"],"tags":["sagemaker","aws-credentials","bad-request","bedrock-vs-sagemaker"],"backgroundTag":"missing-aws-credentials","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}