跳到主要内容

异常映射

LiteLLM 将所有提供商的异常映射到其 OpenAI 对应的异常。

所有异常都可以从 litellm 导入 - 例如 from litellm import BadRequestError

LiteLLM 异常

状态码错误类型继承自描述
400BadRequestErroropenai.BadRequestError
400UnsupportedParamsErrorlitellm.BadRequestError当传递了不支持的参数时引发
400ContextWindowExceededErrorlitellm.BadRequestError上下文窗口超出错误消息的特殊错误类型 - 启用上下文窗口回退
400ContentPolicyViolationErrorlitellm.BadRequestError内容策略违规错误消息的特殊错误类型 - 启用内容策略回退
400InvalidRequestErroropenai.BadRequestError已弃用的错误,请改用 BadRequestError
401AuthenticationErroropenai.AuthenticationError
403PermissionDeniedErroropenai.PermissionDeniedError
404NotFoundErroropenai.NotFoundError当传递了无效模型时引发,例如 gpt-8
408Timeoutopenai.APITimeoutError发生超时时引发
422UnprocessableEntityErroropenai.UnprocessableEntityError
429RateLimitErroropenai.RateLimitError
500APIConnectionErroropenai.APIConnectionError如果返回任何未映射的错误,我们将返回此错误
500APIErroropenai.APIError通用的 500 状态码错误
503ServiceUnavailableErroropenai.APIStatusError如果提供商返回服务不可用错误,则引发此错误
>=500InternalServerErroropenai.InternalServerError如果返回任何未映射的 500 状态码错误,则引发此错误
N/AAPIResponseValidationErroropenai.APIResponseValidationError如果使用了规则,且请求/响应未能通过规则,则引发此错误
N/ABudgetExceededErrorException针对代理引发,当预算超出时
N/AJSONSchemaValidationErrorlitellm.APIResponseValidationError当响应不匹配预期的 JSON 模式时引发 - 如果在 response_schema 参数中传递了 enforce_validation=True 则使用此错误
N/AMockExceptionException内部异常,由 mock_completion 类引发。请勿直接使用
N/AOpenAIErroropenai.OpenAIError已弃用的内部异常,继承自 openai.OpenAIError。

基本情况,我们返回 APIConnectionError

我们所有的异常都继承自 OpenAI 的异常类型,因此您对 OpenAI 异常的任何错误处理都可以直接用于 LiteLLM。

在所有情况下,返回的异常都继承自原始的 OpenAI 异常,但包含 3 个附加属性

  • status_code - 异常的 HTTP 状态码
  • message - 错误消息
  • llm_provider - 引发异常的提供商

用法

import litellm
import openai

try:
response = litellm.completion(
model="gpt-4",
messages=[
{
"role": "user",
"content": "hello, write a 20 pageg essay"
}
],
timeout=0.01, # this will raise a timeout exception
)
except openai.APITimeoutError as e:
print("Passed: Raised correct exception. Got openai.APITimeoutError\nGood Job", e)
print(type(e))
pass

用法 - 捕获流式传输异常

import litellm
try:
response = litellm.completion(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": "hello, write a 20 pg essay"
}
],
timeout=0.0001, # this will raise an exception
stream=True,
)
for chunk in response:
print(chunk)
except openai.APITimeoutError as e:
print("Passed: Raised correct exception. Got openai.APITimeoutError\nGood Job", e)
print(type(e))
pass
except Exception as e:
print(f"Did not raise error `openai.APITimeoutError`. Instead raised error type: {type(e)}, Error: {e}")

用法 - 是否应该重试异常?

import litellm
import openai

try:
response = litellm.completion(
model="gpt-4",
messages=[
{
"role": "user",
"content": "hello, write a 20 pageg essay"
}
],
timeout=0.01, # this will raise a timeout exception
)
except openai.APITimeoutError as e:
should_retry = litellm._should_retry(e.status_code)
print(f"should_retry: {should_retry}")

详情

要了解其实现方式 - 查看代码

创建议题 提交 PR,如果您想改进异常映射。

注意 对于 OpenAI 和 Azure,我们返回原始异常(因为它们是 OpenAI 错误类型)。但我们为它们添加了 'llm_provider' 属性。 查看代码

自定义映射列表

基本情况 - 我们返回 litellm.APIConnectionError 异常(继承自 openai 的 APIConnectionError 异常)。

custom_llm_providerTimeoutContextWindowExceededErrorBadRequestErrorNotFoundErrorContentPolicyViolationErrorAuthenticationErrorAPIErrorRateLimitErrorServiceUnavailableErrorPermissionDeniedErrorUnprocessableEntityError
openai
watsonx
text-completion-openai
custom_openai
openai_compatible_providers
anthropic
replicate
bedrock
sagemaker
vertex_ai
palm
gemini
cloudflare
cohere
cohere_chat
huggingface
ai21
nlp_cloud
together_ai
aleph_alpha
ollama
ollama_chat
vllm
azure
  • ""✓" 表示指定的 custom_llm_provider 可以引发相应的异常。"
  • 空单元格表示没有关联,或该提供商不会引发函数所指示的特定异常类型。

要更深入地了解这些异常,您可以查看实现以获取更多信息。

ContextWindowExceededErrorInvalidRequestError 的子类。引入它是为了为异常处理场景提供更精细的控制。请参考此议题了解更多。

欢迎贡献以改进异常映射此处