Skip to content

升级 pi-* 到 0.63.0 后自定义 Provider 报 'No API key for provider'

问题

@mariozechner/pi-agent-core@mariozechner/pi-ai@mariozechner/pi-coding-agent0.61.1 升级到 0.63.0 后,使用 openai-compatible API 的自定义 Provider(OpenRouter、DeepSeek、Kimi 等)全部失效,报错:

No API key for provider: <provider-name>

Windows 环境还有一个附加症状:cron 调度器仍在使用旧的 API key(即使更新了 openclaw.json 和系统环境变量),说明 key 解析缓存未刷新。

根本原因

OpenClaw 在覆盖 streamFnstreamSimple 时,丢失了 SDK 封装函数中的 modelRegistry.getApiKeyAndHeaders() 调用链。这导致自定义 Provider 的 auth 解析回调(getApiKey)被整体剥离,框架找不到 key 来源。

每次对某个 Provider 单独修补(OpenRouter、Discord、Telegram),同样的模式都会重现,根本原因是缺乏统一、不可覆盖的 auth 层。

解决方案

src/agents/pi-embedded-runner/run/attempt.ts 中,覆盖 streamFn 之后,立即恢复 getApiKey 回调:

diff
+      const agentWithDynamicAuth = activeSession.agent as typeof activeSession.agent & {
+        getApiKey?: (provider: string) => Promise<string | undefined>;
+      };
+      // 覆盖 SDK 封装的 streamFn 后,仍需恢复 per-provider 的 auth 查询回调
+      if (agentWithDynamicAuth.getApiKey) {
+        (activeSession.agent.streamFn as any).__getApiKey = agentWithDynamicAuth.getApiKey;
+      }
       activeSession.agent.streamFn = streamSimple;

临时规避方案:降级回 0.61.1,或等待官方 fix PR 合并。

参考