fix(perplexity): fix streaming errors and silent maxOutputTokens drop#3780
fix(perplexity): fix streaming errors and silent maxOutputTokens drop#3780rhawk301 wants to merge 2 commits into
Conversation
Perplexity class did not override getCallSettings(), so temperature, topP, and maxOutputTokens were silently dropped from every request. Every other provider (Claude, DeepSeek, Gemini, Groq…) overrides this method. Also: Perplexity's API now enforces max_tokens >= 16. Apply Math.max(16, …) so a user-configured value below that never triggers HTTP 400. UI: LazyNumberInput for Perplexity's maxTokens now has min=16 (was 0). Package: bump @ai-sdk/perplexity to >=3.0.42 <4.0.0. The 3.x series up to 3.0.41 shipped a streaming-schema bug where delta.role was required on every SSE chunk; Perplexity's API only sends role in the first and last chunks, causing AI_TypeValidationError on all middle chunks. 3.0.42 fixes this by making role optional. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
scripts/fix-perplexity-streaming.py patches the installed Chatbox app.asar directly so users can fix the streaming error today, without waiting for a source build or official release. The script: - Requires only Python 3.8+ and macOS codesign (no Node.js, no npm) - Auto-detects Chatbox.app in /Applications or ~/Applications - Accepts --app PATH override for non-standard installs - Patches both main-process (dist/main/main.js) and renderer-process (dist/renderer/js/vendor-ai.*.js) bundles — each ships its own copy of @ai-sdk/perplexity, both need the delta.role optional fix - Finds the renderer file by glob (the hash suffix changes each build) - Creates a timestamped backup before any change - Re-signs the app bundle with an ad-hoc signature (required on macOS) - Verifies all patches applied before committing the new asar to disk - --status, --restore, --dry-run, --force flags Bug patched: @ai-sdk/perplexity <=3.0.41 validates delta.role as required on every SSE chunk. Perplexity's API only sends role on the first and last chunks. Source fix is upgrading to >=3.0.42 (previous commit), but that requires a pnpm-workspace minimumReleaseAge of 7 days (until 2026-07-09). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
themez
left a comment
There was a problem hiding this comment.
核心修复方向已经验证:补齐 getCallSettings() 是必要的,升级后的 Perplexity SDK 也确实修复了流式 chunk schema;补生成 lockfile 后,新增的 6 个定向测试可以通过。不过当前 PR 仍有安装阻断、热补丁失败处理和格式问题,另外 max_tokens 下限与当前官方文档不一致,请处理下列评论后再合并。
| "@ai-sdk/openai": "^3.0.53", | ||
| "@ai-sdk/openai-compatible": "^2.0.3", | ||
| "@ai-sdk/perplexity": "^3.0.3", | ||
| "@ai-sdk/perplexity": ">=3.0.42 <4.0.0", |
There was a problem hiding this comment.
[P1] 请同步提交 pnpm-lock.yaml。 当前 PR 只修改了 manifest,lockfile 仍记录 ^3.0.3 / 3.0.17。我在该 HEAD 上使用 Node 22、pnpm 10.33 运行 pnpm install --frozen-lockfile --ignore-scripts,会直接报 ERR_PNPM_OUTDATED_LOCKFILE;CI 环境默认使用 frozen lockfile,因此当前提交无法完成依赖安装。
| finally: | ||
| tmp_path.unlink(missing_ok=True) | ||
|
|
||
| sign_app(app) |
There was a problem hiding this comment.
[P2] 签名失败不能继续报告成功。 sign_app() 在 codesign 缺失或执行失败时返回 False,但这里忽略结果,随后仍输出 Done 并返回成功;脚本自己的说明也指出这种状态可能导致应用无法启动。至少应将签名失败视为硬错误并恢复刚创建的备份。现在 3.0.42 已超过 minimumReleaseAge 等待期,更建议删除这整个临时 asar 热补丁,只保留源码依赖升级。
| const baseModel: ProviderModelInfo = { modelId: 'sonar-pro' } | ||
| const emptyOptions: CallChatCompletionOptions = {} | ||
|
|
||
| function makeModel(opts: { |
There was a problem hiding this comment.
[P2] 新增测试目前未通过 Biome 格式检查。 定向运行 pnpm exec biome check .../perplexity.test.ts 会在这里退出非零;formatter 会把参数类型整理为单行。请运行 pnpm format 并提交格式化结果。
| return { | ||
| temperature: this.options.temperature, | ||
| topP: this.options.topP, | ||
| // Perplexity API rejects max_tokens < 16 with HTTP 400. |
There was a problem hiding this comment.
这里的 16 下限需要补充可复现证据。当前 Perplexity 官方 Sonar API 文档给出的 max_tokens 范围是 0 < x <= 128000,与“1–15 必然返回 400”冲突:https://docs.perplexity.ai/api-reference/sonar-post 。请提供当前模型、请求和错误响应的复现;如果这是模型特有限制,应明确限定,否则不应静默把文档允许的用户值改写为 16。
Summary
Fixes two independent bugs that break the Perplexity provider in Chatbox 1.21.1.
Bug 1 —
getCallSettings()not overridden (maxOutputTokens silently dropped)File:
src/shared/providers/definitions/models/perplexity.tsThe
Perplexityclass extendsAbstractAISDKModelbut does not overridegetCallSettings(). Every other provider (Azure, Claude, DeepSeek, Gemini, Groq…) overrides this to forwardtemperature,topP, andmaxOutputTokensto the AI SDK. Perplexity doesn't — they're silently dropped on every request.Effect: user-configured
maxTokensis always ignored. Perplexity's API now enforcesmax_tokens >= 16; a naive fix without a floor would cause HTTP 400 for any value 1–15.Fix:
Also:
LazyNumberInputfor Perplexity's maxTokens inSessionSettings.tsxnow hasmin={16}(wasmin={0}) to prevent UI entry of invalid values.Bug 2 — stale
@ai-sdk/perplexitywith broken streaming schemaChatbox bundles
@ai-sdk/perplexity@3.0.17which has a bug inperplexityChunkSchema:Perplexity's streaming API only sends
rolein the first and last SSE chunks. Middle chunks omitrole, failing zod-mini literal validation with:This error fires even when the API call succeeds — the streaming parser fails on every non-trivial response.
Fix: bump
@ai-sdk/perplexityto>=3.0.42 <4.0.0.Immediate workaround (no rebuild required)
scripts/fix-perplexity-streaming.pyapplies the same schema fix directly to the installedapp.asarusing binary surgery — no Node, npm, or build tools needed:Requires Python 3.8+ and macOS
codesign. Patches both the main-process and renderer-process bundles (each ships its own copy of@ai-sdk/perplexity). Finds the renderer'svendor-ai.*.jsby glob so it works across builds with different content-hash suffixes.Test plan
pnpm exec vitest run src/shared/providers/definitions/models/perplexity.test.ts— 6 tests passpnpm test— full unit suite passesAI_TypeValidationErrormaxTokens≥ 16 → verify it is respected in the requestmaxTokens< 16 in UI → verify input is clamped to 16Maintainer verification prompt
Paste into Claude (or any LLM) for an independent review of this PR:
Description
Fixes two bugs in the Perplexity provider: missing
getCallSettings()override causing silent parameter drops, and a stale@ai-sdk/perplexityversion with a broken streaming schema.Additional Notes
Both bugs were diagnosed against a live Perplexity API. Bug 2 affects every streaming response — the error fires even when the model answers correctly, because the streaming parser fails on middle chunks that omit
delta.role.Contributor Agreement