异常映射
LiteLLM 将所有提供商的异常映射到其对应的 OpenAI 异常。
| 状态码 | 错误类型 |
|---|---|
| 400 | BadRequestError |
| 401 | AuthenticationError |
| 403 | PermissionDeniedError |
| 404 | NotFoundError |
| 422 | UnprocessableEntityError |
| 429 | RateLimitError |
| >=500 | InternalServerError |
| N/A | ContextWindowExceededError |
| 400 | ContentPolicyViolationError |
| 500 | APIConnectionError |
基本情况下,我们返回 APIConnectionError。
我们所有的异常都继承自 OpenAI 的异常类型,因此任何为 OpenAI 异常编写的错误处理代码,都可以直接与 LiteLLM 一起使用。
对于所有情况,返回的异常继承自原始的 OpenAI 异常,但包含三个额外的属性:
- 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, # 这将引发超时异常
)
except openai.APITimeoutError as e:
print("通过: 正确引发了异常。得到了 openai.APITimeoutError\n干得好", 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, # 这将引发异常
stream=True,
)
for chunk in response:
print(chunk)
except openai.APITimeoutError as e:
print("通过: 正确引发了异常。得到了 openai.APITimeoutError\n干得好", e)
print(type(e))
pass
except Exception as e:
print(f"没有引发 `openai.APITimeoutError` 异常。相反引发了异常类型: {type(e)}, 错误: {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, # 这将引发超时异常
)
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 异常)。
| 自定义 LLM 提供商 | 超时 | ContextWindowExceededError | BadRequestError | NotFoundError | ContentPolicyViolationError | AuthenticationError | APIError | RateLimitError | ServiceUnavailableError | PermissionDeniedError | UnprocessableEntityError |
|---|---|---|---|---|---|---|---|---|---|---|---|
| openai | ✓ | ✓ | ✓ | ✓ | ✓ | ||||||
| watsonx | ✓ | ||||||||||
| text-completion-openai | ✓ | ✓ | ✓ | ✓ | ✓ | ||||||
| 自定义_openai | ✓ | ✓ | ✓ | ✓ | ✓ | ||||||
| openai兼容提供者 | ✓ | ✓ | ✓ | ✓ | ✓ | ||||||
| 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 的子类。它被引入以在异常处理场景中提供更细粒度的控制。请参考此问题以了解更多。
欢迎贡献以改进异常映射。