-
-
Notifications
You must be signed in to change notification settings - Fork 911
fix(provider): 修复因缺少 key 导致 provider 无法加载的问题 #2810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
fix(provider): 修复因缺少 key 导致 provider 无法加载的问题 #2810
Conversation
在 load_provider 方法中,对 provider_config 进行预处理,确保 'key' 字段存在且不为空列表,从而避免在实例化 OpenAI 兼容的 Provider 时因缺少 API Key 而抛出异常。
这是我能想到的最优雅,最精炼,并且最少侵入性的修复方案。 因为几乎所有的openai格式,默认apikey都至少是一个空文本,而不应该是不存在这个属性。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你好 - 我已审阅了你的更改 - 这里有一些反馈意见:
- 考虑将
provider_config.copy()
移动到load_provider
的最顶部(在任何提前返回之前),以避免在启用检查后就地修改原始配置。 - 当你自动注入空的 API 密钥时,记录日志或发出警告可能很有帮助,这样用户就能轻松发现底层的错误配置。
- 与其默默地替换一个空字符串,不如验证是否存在非空密钥并引发明确的配置错误来引导用户。
AI 代理的提示
请处理此代码审查中的评论:
## 总体评论
- 考虑将 provider_config.copy() 移动到 load_provider 的最顶部(在任何提前返回之前),以避免在启用检查后就地修改原始配置。
- 当你自动注入空的 API 密钥时,记录日志或发出警告可能很有帮助,这样用户就能轻松发现底层的错误配置。
- 与其默默地替换一个空字符串,不如验证是否存在非空密钥并引发明确的配置错误来引导用户。
帮助我更有用!请点击每个评论上的 👍 或 👎,我将利用这些反馈来改进你的评论。
Original comment in English
Hey there - I've reviewed your changes - here's some feedback:
- Consider moving the provider_config.copy() to the very top of load_provider (before any early returns) to avoid mutating the original config in-place after the enable check.
- It could be helpful to log or warn when you auto-inject an empty API key so users can easily spot the underlying misconfiguration.
- Rather than silently substituting an empty string, you might validate the presence of a non-empty key and raise a clear configuration error to guide users.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider moving the provider_config.copy() to the very top of load_provider (before any early returns) to avoid mutating the original config in-place after the enable check.
- It could be helpful to log or warn when you auto-inject an empty API key so users can easily spot the underlying misconfiguration.
- Rather than silently substituting an empty string, you might validate the presence of a non-empty key and raise a clear configuration error to guide users.
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
你懂个屁,没启动为什么要拷贝一份。 apikey默认为空才是正确的实现。 |
这个问题是什么情况呢? 之前我写了一个每次reload都重新加入提供商地图,但是显然没这个优雅。 |
根源1、如果新增供应商的时候初始化失败就是不加入,然后后续再也没有加入的方法,除非重启,并且重启之后合法了才能加入。而导致这个原因是,apikey默认是空列表,而不是空字符串。空列表就会导致不存在这个字段导致无法初始化。 已有 |
在 load_provider 方法中,对 provider_config 进行预处理,确保 'key' 字段存在且不为空列表,从而避免在实例化 OpenAI 兼容的 Provider 时因缺少 API Key 而抛出异常。
fixes #2763
修复因缺少 key 导致 provider 无法加载的问题" --body "
问题根源
在
新增
Provider 时,如果配置不合法(如key
字段为空列表),ProviderManager
的load_provider
方法会因为底层的Provider
类(如ProviderOpenAIOfficial
)在__init__
中抛出异常而提前终止。这导致负责将新实例注册到
inst_map
的代码行永远不会被执行,使得inst_map
中从始至终都没有这个新 Provider 的条目,最终在运行时导致“找不到提供商”的错误。解决方案
本 PR 采用了一个简单、直接且从根源上解决问题的方案:
在
ProviderManager.load_provider
方法的入口处,对传入的provider_config
进行预处理。如果发现key
字段不存在或是一个空列表,就强制为其设置一个包含空字符串的列表['']
。效果
Provider
类的__init__
方法不会因为key
为空而抛出异常。load_provider
总是能成功执行到底,并将新的实例注册到inst_map
中,从而保证了配置与运行时实例地图的一致性。key
为\"\"
的 Provider 时,底层的 API 客户端会返回一个明确的“无效 API Key”错误,而不是误导性的“找不到提供商”错误。这个修改以最小的代价,从根源上解决了问题,并提高了系统的健壮性。"
Compatibility & Breaking Changes / 兼容性与破坏性变更
Checklist / 检查清单
requirements.txt
和pyproject.toml
文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations inrequirements.txt
andpyproject.toml
.Sourcery 总结
Bug 修复:
Original summary in English
Summary by Sourcery
Bug Fixes: