快速结论:当 /v1/messages 遇到不属于 ProxyException 的上游失败(例如连接错误)时,错误处理分支引用了未绑定的变量 e,触发 UnboundLocalError,代理随后把这个异常误报为数据库不可用并返回 503。优先排查 Anthropic 端点错误处理代码,而不是真的去查数据库。
适用环境:LiteLLM main 分支 cbe361d970(包含 #49227);部署方式为 pip / Python SDK,通过 litellm --config ... --port ... 启动代理。Issue 明确说明 v1.109.0-rc.1 不包含 #49227,因此已发布版本不受影响。
最快修复方案:升级到包含 PR #49993 的 LiteLLM 版本(该 PR 把出错的 e 改为 answered);Issue 已确认修复合入 main,main 现在通过 answered 而非未绑定的 e。
注意事项:修复效果来自 main 分支验证;若你锁定的是 main 的旧提交,需先确认已包含 #49993,否则问题仍会复现。不要根据这条 503 去排查数据库连接配置,它只是异常被误分类后的表象。
问题场景
在 LiteLLM 代理中配置一个指向不可达 api_base(如 http://127.0.0.1:18099/v1,无人监听)的模型,然后向 /v1/messages(Anthropic 兼容端点)发起请求。上游连接失败会进入 litellm/proxy/anthropic_endpoints/endpoints.py 的通用 except Exception as raised 分支,该分支在第 257 行调用 is_agentic_followup_failure(e),但 e 只在更早的 except 子句中绑定。同一上游失败在 /v1/chat/completions 上能正常返回 500,说明问题只出在 Anthropic 端点的异常处理路径。
报错原文
{"type":"error","error":{"type":"api_error","message":"Service Unavailable, the authentication database is temporarily unreachable. Please retry shortly.","litellm_error_type":"no_db_connection"}}
HTTP 503
{"error":{"message":"litellm.InternalServerError: InternalServerError: OpenAIException - Connection error.\n\nLiteLLM: model group 'broken-upstream' failed with the error above. No fallback was attempted.","type":"internal_server_error","param":null,"code":"500"}}
HTTP 500
File "litellm/proxy/anthropic_endpoints/endpoints.py", line 257, in anthropic_response
failure_row_response_cost(base_llm_response_processor.data) if is_agentic_followup_failure(e) else 0
UnboundLocalError: cannot access local variable 'e' where it is not associated with a value
LiteLLM Proxy:WARNING: proxy_server.py:2552 - Database unavailable during request: UnboundLocalError
原因分析
最可能的原因是 #49227 引入的通用异常分支写错了变量名:except Exception as raised 绑定的是 raised,代码却读取了只在其他 except 子句中存在的 e,因此抛出 UnboundLocalError。该异常又被代理的上层逻辑归类为数据库不可用(no_db_connection),于是返回 503 “authentication database is temporarily unreachable”。Issue 作者认为原意是使用 answered,评论也确认修复方式为在 main 上把 e 替换为 answered。make lint 在 main 上同样报 F821,可视为同一问题的静态检查佐证。
环境排查
- 确认 LiteLLM 版本或提交 hash:仅在包含 #49227 的 main 构建上受影响;v1.109.0-rc.1 不受影响。
- 确认是否已包含修复 PR #49993(检查
anthropic_endpoints/endpoints.py第 257 行附近是否仍引用未绑定的e)。 - 确认部署方式为 pip / Python SDK 启动的代理,并核对启动命令与配置路径。
- 确认
model_list中目标模型的api_base是否可达,以复现上游连接失败场景。 - 若自行修改源码,运行
make lint检查是否仍有 F821 告警。
解决步骤
- 在出错版本上先复现:启动代理后分别请求
/v1/messages与/v1/chat/completions,确认前者返回 503 且日志中出现UnboundLocalError。 - 升级到包含 PR #49993 的 LiteLLM 版本(main 已合入该修复)。
- 如果暂时无法升级,可对照 #49993 的改动检查本地
anthropic_endpoints/endpoints.py中通用异常分支的变量引用,确认其使用的是已绑定的异常变量;Issue 未提供完整补丁内容,此处不做推测性改写。 - 重启代理使变更生效。
验证方法
用同样指向不可达 api_base 的配置再次请求 /v1/messages:不再出现 503 “authentication database is temporarily unreachable” 与 UnboundLocalError,而是像 /v1/chat/completions 一样返回上游连接失败对应的错误(如 500 InternalServerError / Connection error)。同时 Issue 中提到 main 上受影响的六个 Anthropic 端点单元测试也应随修复通过。
参考来源
BerriAI/litellm #49998(修复 PR:#49993)
AI 工具推荐
想把多个 AI 模型放在一个入口?
GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。
推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。
这个方案解决了吗?
可以继续搜索完整报错,或查看同一工具的其他排查指南。

![Misc. bug: [SYCL] CPY to q4_1/q5_1/q8_0 doesn't match CPU reference](https://www.chat-gpts.plus/wp-content/uploads/2026/11/31140-0ca3be80-768x403.jpg)
