异常映射
LiteLLM 将所有提供商的异常映射到其 OpenAI 对应的异常。
所有异常都可以从 litellm
导入 - 例如 from litellm import BadRequestError
LiteLLM 异常
状态码 | 错误类型 | 继承自 | 描述 |
---|---|---|---|
400 | BadRequestError | openai.BadRequestError | |
400 | UnsupportedParamsError | litellm.BadRequestError | 当传递了不支持的参数时引发 |
400 | ContextWindowExceededError | litellm.BadRequestError | 上下文窗口超出错误消息的特殊错误类型 - 启用上下文窗口回退 |
400 | ContentPolicyViolationError | litellm.BadRequestError | 内容策略违规错误消息的特殊错误类型 - 启用内容策略回退 |
400 | InvalidRequestError | openai.BadRequestError | 已弃用的错误,请改用 BadRequestError |
401 | AuthenticationError | openai.AuthenticationError | |
403 | PermissionDeniedError | openai.PermissionDeniedError | |
404 | NotFoundError | openai.NotFoundError | 当传递了无效模型时引发,例如 gpt-8 |
408 | Timeout | openai.APITimeoutError | 发生超时时引发 |
422 | UnprocessableEntityError | openai.UnprocessableEntityError | |
429 | RateLimitError | openai.RateLimitError | |
500 | APIConnectionError | openai.APIConnectionError | 如果返回任何未映射的错误,我们将返回此错误 |
500 | APIError | openai.APIError | 通用的 500 状态码错误 |
503 | ServiceUnavailableError | openai.APIStatusError | 如果提供商返回服务不可用错误,则引发此错误 |
>=500 | InternalServerError | openai.InternalServerError | 如果返回任何未映射的 500 状态码错误,则引发此错误 |
N/A | APIResponseValidationError | openai.APIResponseValidationError | 如果使用了规则,且请求/响应未能通过规则,则引发此错误 |
N/A | BudgetExceededError | Exception | 针对代理引发,当预算超出时 |
N/A | JSONSchemaValidationError | litellm.APIResponseValidationError | 当响应不匹配预期的 JSON 模式时引发 - 如果在 response_schema 参数中传递了 enforce_validation=True 则使用此错误 |
N/A | MockException | Exception | 内部异常,由 mock_completion 类引发。请勿直接使用 |
N/A | OpenAIError | openai.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}")
详情
要了解其实现方式 - 查看代码
注意 对于 OpenAI 和 Azure,我们返回原始异常(因为它们是 OpenAI 错误类型)。但我们为它们添加了 'llm_provider' 属性。 查看代码
自定义映射列表
基本情况 - 我们返回 litellm.APIConnectionError
异常(继承自 openai 的 APIConnectionError 异常)。
custom_llm_provider | Timeout | ContextWindowExceededError | BadRequestError | NotFoundError | ContentPolicyViolationError | AuthenticationError | APIError | RateLimitError | ServiceUnavailableError | PermissionDeniedError | UnprocessableEntityError |
---|---|---|---|---|---|---|---|---|---|---|---|
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
可以引发相应的异常。" - 空单元格表示没有关联,或该提供商不会引发函数所指示的特定异常类型。
要更深入地了解这些异常,您可以查看此实现以获取更多信息。
ContextWindowExceededError
是 InvalidRequestError
的子类。引入它是为了为异常处理场景提供更精细的控制。请参考此议题了解更多。
欢迎贡献以改进异常映射此处