{"record":{"id":"7f36ae512f99b100","repo":"BerriAI/litellm","slug":"sagemakerexception-original-exception-message","errorCode":null,"errorMessage":"SagemakerException - {original_exception.message}","messagePattern":"SagemakerException - (.+?)","errorType":"exception","errorClass":"ServiceUnavailableError","httpStatus":503,"severity":"error","filePath":"litellm/litellm_core_utils/exception_mapping_utils.py","lineNumber":1003,"sourceCode":"        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(\n                message=f\"SagemakerException - {original_exception.message}\",\n                llm_provider=custom_llm_provider,\n                model=model,\n                response=httpx.Response(\n                    status_code=500,\n                    request=httpx.Request(method=\"POST\", url=\"https://api.openai.com/v1/\"),\n                ),\n            )\n        elif original_exception.status_code == 401:\n            raise AuthenticationError(\n                message=f\"SagemakerException - {original_exception.message}\",\n                llm_provider=custom_llm_provider,\n                model=model,\n                response=getattr(original_exception, \"response\", None),\n            )\n        elif original_exception.status_code == 400:\n            raise BadRequestError(\n                message=f\"SagemakerException - {original_exception.message}\",","sourceCodeStart":985,"sourceCodeEnd":1021,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/exception_mapping_utils.py#L985-L1021","documentation":"LiteLLM normalizes an HTTP 500 from a SageMaker inference endpoint into its generic ServiceUnavailableError. The 500 means the model container behind the endpoint hit an unhandled exception (crash, OOM, broken handler), not that LiteLLM failed. The message embeds the raw endpoint exception text (original_exception.message). Note the attached httpx.Response is synthetic (a placeholder POST to api.openai.com/v1/), so do not rely on its URL or headers when debugging.","triggerScenarios":"Any litellm.completion(..., model='sagemaker/<endpoint>') call where the SageMaker InvokeEndpoint API responds with HTTP 500: the container's inference handler raised a Python exception, the container was OOM-killed mid-request, or the model server (DJL/TGI/HuggingFace) returned an internal error status.","commonSituations":"A custom inference handler with a bug (expects 'inputs' key but receives OpenAI-style payload); container memory limits smaller than the model needs; endpoint just deployed and the model still loading; a bad container image whose /invocations route crashes.","solutions":["Check the endpoint's CloudWatch logs (group /aws/sagemaker/Endpoints/<endpoint-name>) for the container stack trace at the failure timestamp","Reproduce with a minimal direct boto3 invoke_endpoint call to confirm the failure is in the container, not in litellm","If the handler crashes on OpenAI-format payloads, supply the input format / custom prompt template your container expects","If OOM: move the endpoint to a larger instance type or reduce max_new_tokens","Set num_retries on the call (litellm.completion(..., num_retries=3)) since 500s during model loading are often transient"],"exampleFix":"# before\nresponse = litellm.completion(\n    model='sagemaker/my-endpoint',\n    messages=messages,\n)\n\n# after\nresponse = litellm.completion(\n    model='sagemaker/my-endpoint',\n    messages=messages,\n    num_retries=3,  # transient container 500s get retried\n)\n# still failing: inspect CloudWatch logs for the container stack trace","handlingStrategy":"retry","validationCode":"import boto3\n\ndef sagemaker_endpoint_healthy(endpoint_name: str, region: str) -> bool:\n    sm = boto3.client('sagemaker', region_name=region)\n    return sm.describe_endpoint(EndpointName=endpoint_name)['EndpointStatus'] == 'InService'","typeGuard":"import litellm\n\ndef is_sagemaker_500(exc: BaseException) -> bool:\n    return isinstance(exc, litellm.exceptions.ServiceUnavailableError) and 'SagemakerException' in str(exc)","tryCatchPattern":"import litellm\nfrom litellm.exceptions import ServiceUnavailableError\n\ntry:\n    resp = litellm.completion(model='sagemaker/my-endpoint', messages=messages)\nexcept ServiceUnavailableError as e:\n    if 'SagemakerException' not in str(e):\n        raise\n    # container 500s during model load are transient: back off and retry\n    time.sleep(2 ** attempt)\n    resp = litellm.completion(model='sagemaker/my-endpoint', messages=messages, num_retries=3)","preventionTips":["Alarm on the endpoint's CloudWatch Invocation5XXErrors metric before users see failures","Load-test new endpoint configurations before routing production traffic","Always set num_retries on sagemaker calls to absorb transient container 500s"],"tags":["sagemaker","aws","http-500","model-container","retryable","litellm"],"backgroundTag":"internal-server-error","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}