Skip to content

fix(provider): Cerebras 等严格验证参数的 provider 返回 422 错误#1056

Closed
cdxiaodong wants to merge 1 commit intoiOfficeAI:mainfrom
cdxiaodong:fix/1038-cerebras-422-stream-options
Closed

fix(provider): Cerebras 等严格验证参数的 provider 返回 422 错误#1056
cdxiaodong wants to merge 1 commit intoiOfficeAI:mainfrom
cdxiaodong:fix/1038-cerebras-422-stream-options

Conversation

@cdxiaodong
Copy link
Copy Markdown
Member

概述

修复 Cerebras provider 在创建 chat 时返回 422 status code (no body) 的问题。

根因分析

@office-ai/aioncli-coreOpenAIContentGenerator.generateContentStream() 在构造流式请求时硬编码了 stream_options: { include_usage: true } 参数。

  • OpenAI、DeepSeek、OpenRouter 等 provider 支持或忽略此参数 → 正常工作
  • Cerebras 严格验证请求参数,遇到不支持的字段直接返回 422 → 报错

这也解释了为什么模型列表正常(简单 GET 请求,无 body)但聊天失败(POST 请求包含不支持的参数)。

修复方案

通过 patch-package@office-ai/aioncli-core 打补丁:

  1. 新增 shouldIncludeStreamOptions() 方法,检查当前 provider 的 hostname
  2. 对已知会拒绝 stream_options 的 provider(如 api.cerebras.ai)不发送此参数
  3. 其他 provider 保持原有行为不变

影响文件

  • patches/@office-ai+aioncli-core+0.24.5.patch(新增)

测试计划

  • 使用 Cerebras provider 创建聊天,确认不再返回 422
  • 使用 OpenAI / DeepSeek 等 provider 创建聊天,确认 stream_options 仍正常发送
  • 确认模型列表功能不受影响

Closes #1038

aioncli-core 的 OpenAIContentGenerator.generateContentStream() 硬编码了
stream_options: { include_usage: true },Cerebras API 严格验证请求参数,
遇到不支持的字段直接返回 422。

通过 patch-package 添加 shouldIncludeStreamOptions() 方法,对已知会
拒绝 stream_options 的 provider(如 Cerebras)不发送此参数。

关联 iOfficeAI#1038
@kuishou68
Copy link
Copy Markdown
Collaborator

@cdxiaodong Thanks for investigating the Cerebras 422 issue! However, the patch-package approach isn't the right solution here:

  1. aioncli-core is maintained by us — the fix should be made upstream in the aioncli-core repo directly, not patched locally.
  2. Pinning the aioncli-core version — this prevents us from receiving future updates and creates maintenance burden.

The correct approach is to submit the shouldIncludeStreamOptions() fix to the aioncli-core repo, then bump the version here once it's published.

Could you open a PR against aioncli-core instead? Thanks!

@cdxiaodong
Copy link
Copy Markdown
Member Author

@kuishou68 Thanks for the review! You're right — the fix should go upstream.

I've submitted the upstream PR: iOfficeAI/aioncli#32

Once it's merged and a new version of @office-ai/aioncli-core is published, I'll update this PR to bump the dependency version and remove the patch-package patch.

@kuishou68
Copy link
Copy Markdown
Collaborator

@cdxiaodong The code logic itself is correct and follows the existing shouldIncludeMetadata() pattern nicely. However, there are two blocking issues:

1. Patch will not be applied

The project uses Bun's native patching (patchedDependencies in package.json), not patch-package. The patch file naming convention (+ separator) and the missing patchedDependencies entry mean this patch is never applied during bun install.

2. Version mismatch

main has since upgraded @office-ai/aioncli-core from ^0.24.5 to ^0.30.0. This patch targets the old version.

Recommended path forward

As discussed earlier, the fix should go upstream in the aioncli-core repo. Your upstream PR (iOfficeAI/aioncli#32) is the right approach — once that's merged and a new version is published, we can bump the dependency here and close this PR.

@kuishou68 kuishou68 closed this Mar 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Cerebras Provider Chat Returns 422 Error

2 participants