Skip to content

fix(relay): fix Anthropic-compatible compatibility for GLM (avoid chunked encoding) - #5307

Merged
seefs001 merged 2 commits into
QuantumNous:mainfrom
Chen011214:main
Jun 5, 2026
Merged

seefs001 merged 2 commits into
QuantumNous:mainfrom
Chen011214:main

Conversation

@Chen011214

@Chen011214 Chen011214 commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

📝 变更描述 / Description

修复了 new-api 作为 Anthropic 兼容网关时,上游请求因分块传输编码(Transfer-Encoding: chunked)导致部分端点(如智谱 GLM-5.1、GLM-5-turbo)报错 400/500 的问题。

原因与修复:
在 Claude 的透传(pass-through)路径中,请求体大小在 BodyStorage 中虽已知,但未透传给 RelayInfo.UpstreamRequestBodySize。这导致后续构建上游请求时无法正确绑定 req.ContentLength,引发 Go HTTP 客户端回退为分块传输。本修改将 storage.Size() 显式透传,确保正确携带 Content-Length 头部。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 Issues 与 PRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

使用 Anthropic Python SDK 请求修复后的网关,成功返回 HTTP/1.1 200 OK ,并可以成功解析响应。

示例:

{
  "id": "msg_...",
  "type": "message",
  "role": "assistant",
  "model": "glm-5.1",
  "content": [
    {
      "type": "text",
      "text": "我就是一个随时准备帮你解答问题、提供建议和陪你聊天的AI助手。"
    }
  ],
  "stop_reason": "end_turn",
  "usage": {
    "input_tokens": 12,
    "output_tokens": 20
  }
}

Summary by CodeRabbit

  • Chores
    • Improved tracking of upstream request metrics for enhanced monitoring visibility.

Chen011214 and others added 2 commits June 4, 2026 16:18
Fix: Anthropic-compatible GLM upstream fails when request body is sent with Transfer-Encoding: chunked
Fix: Anthropic-compatible GLM upstream fails when request body is sent with Transfer-Encoding: chunked

Co-authored-by: jmc12138 <[email protected]>
Co-authored-by: z2x7b4 <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 186a7a4d-e180-404f-9980-d077a692bf7e

📥 Commits

Reviewing files that changed from the base of the PR and between 00d23ab and 83ccaa6.

📒 Files selected for processing (1)
  • relay/claude_handler.go

Walkthrough

The Claude handler now captures the request body size when pass-through mode is enabled, storing it in the relay info before constructing the upstream request. This ensures the final HTTP request includes a Content-Length header instead of defaulting to chunked transfer encoding.

Changes

Request body size tracking for Claude pass-through

Layer / File(s) Summary
Request body size tracking in Claude handler
relay/claude_handler.go
After retrieving the stored request body via common.GetBodyStorage(c), the handler records the body size by setting info.UpstreamRequestBodySize = storage.Size(). This propagation allows the request construction logic to set the proper Content-Length header.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Poem

A line so small, yet fixes the stream,
No more chunked requests that make upstreams scream,
The size is now known, Content-Length is clear,
GLM's Anthropic path now thrives without fear! 🐇✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: fixing Anthropic-compatible compatibility for GLM by avoiding chunked encoding when the body size is known.
Linked Issues check ✅ Passed The code change directly addresses the root cause identified in issue #5304 by setting info.UpstreamRequestBodySize when using the stored body in the Claude pass-through path.
Out of Scope Changes check ✅ Passed The change is narrowly focused on the specific fix described in the linked issue—propagating body size to RelayInfo—with no extraneous modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@seefs001
seefs001 merged commit 83068d1 into QuantumNous:main Jun 5, 2026
1 check passed
Soein added a commit to Soein/new-api that referenced this pull request Jun 6, 2026
… id 等)

主要变更(QuantumNous/new-api → adc390c):
- feat: 渠道被禁用后可配置是否清空渠道粘性 (QuantumNous#5306)
- feat(web): profile 页显示 user id (QuantumNous#5317)
- fix: 复用 channel handler 的 stream scanner buffer (QuantumNous#5225)
- fix: 收窄 OpenAI o 系列模型适配范围 (QuantumNous#5293)
- fix(relay): GLM Anthropic 兼容避免 chunked encoding (QuantumNous#5307)
- fix: 新增 relay idle 连接超时配置 (QuantumNous#5309)
- fix: 限制匿名请求体大小 (QuantumNous#5244)
- fix(distributor): 修复 video generations task_id 模型解析 (QuantumNous#5133)
- fix(dify): 远程图片字段赋值前初始化 file pointer (QuantumNous#5134)
- fix(i18n): 优化 thinking adapter 文案 (QuantumNous#5242)

去合规一致性:上游未触及任何合规文件,合并完整保留本地去合规状态
(payment_setting.go / payment-settings-section.tsx / recharge-form-card.tsx /
risk-acknowledgement-dialog.tsx 均未被改动)。

i18n:web/default 6 语言(en/zh/fr/ja/ru/vi)key 并集三路合并 + i18n:sync
规范化,4584 keys/语言,missing/extras/untranslated 全为 0。

验证:go build ./... ✅ + bun run typecheck ✅
Ember-Moth pushed a commit to Herta-Network/new-api that referenced this pull request Jun 7, 2026
szxufan pushed a commit to szxufan/new-api that referenced this pull request Jun 9, 2026
52assert added a commit to 52assert/new-api that referenced this pull request Jun 10, 2026
…codes

* origin/main: (45 commits)
  fix(openai): support streaming image relay and image edit for images API  (QuantumNous#4608)
  perf(web): improve dialog sizing and footer layout
  feat(web): add shared dialog wrapper
  perf(web): simplify public page hero copy
  perf(model-pricing): move pricing tabs into page title
  feat(json-editor): add reusable JSON code editor
  perf(model-pricing): improve JSON pricing editor layout
  perf(model-pricing): reduce duplicate model name display
  fix: support six-decimal steps in model pricing editor
  fix: respect theme for multiselect combobox popover
  fix: reuse stream scanner buffer in channel handlers (QuantumNous#5225)
  fix: 收窄 OpenAI o 系列模型适配范围 (QuantumNous#5293)
  fix(i18n): clarify thinking adapter copy (QuantumNous#5242)
  fix: limit anonymous request body (QuantumNous#5244)
  fix(relay): fix Anthropic-compatible compatibility for GLM (avoid chunked encoding) (QuantumNous#5307)
  feat: 支持配置渠道被禁用后是否清空渠道粘性 (QuantumNous#5306)
  fix: add relay idle connection timeout config (QuantumNous#5309)
  feat(web): show user id on profile page
  perf(model-pricing): refine visual editor actions
  refactor(model-pricing): split visual pricing editor modules
  ...

# Conflicts:
#	router/api-router.go
#	web/default/src/features/usage-logs/components/usage-logs-mobile-card.tsx
endercat-alu pushed a commit to endercat-alu/new-api that referenced this pull request Jun 10, 2026
ljdzxx pushed a commit to ljdzxx/new-api that referenced this pull request Jun 13, 2026
OuYang-HX pushed a commit to OuYang-HX/new-api that referenced this pull request Jun 13, 2026
YeMao11 pushed a commit to YeMao11/new-api that referenced this pull request Jun 16, 2026
Upstream changes (50+ commits, v1.0.0-rc.11):
- data-table perf: row selection memo, column pinning, badge display
- Fixes: channel test dialog (QuantumNous#5517), CC Switch model selector (QuantumNous#5515),
  API key form options (QuantumNous#5512), cell overflow (QuantumNous#5510), kimi k2.6 temp (QuantumNous#5390),
  Anthropic-compatible GLM chunked encoding (QuantumNous#5307), streaming image relay (QuantumNous#4608)
- Feat: audit auth method tracking (QuantumNous#5462), channel affinity clear toggle (QuantumNous#5306),
  relay idle timeout config (QuantumNous#5309), 6-decimal pricing precision (QuantumNous#5332)
- Classic frontend: Rsbuild support, Semi React 19 adapter
- Shared dialog wrapper, JSON code editor, debounce channel search

Conflict resolved: web/bun.lock (accepted upstream, will regenerate)

Co-Authored-By: Claude <[email protected]>
YeMao11 pushed a commit to YeMao11/new-api that referenced this pull request Jun 16, 2026
Merge upstream v1.0.0-rc.11 (50+ commits):
- data-table perf: row selection memo, column pinning, badge display
- Fixes: channel test dialog (QuantumNous#5517), CC Switch (QuantumNous#5515), API key (QuantumNous#5512),
  kimi k2.6 temp (QuantumNous#5390), GLM chunked encoding (QuantumNous#5307), streaming image (QuantumNous#4608)
- Feat: audit auth tracking (QuantumNous#5462), channel affinity toggle (QuantumNous#5306),
  relay idle timeout (QuantumNous#5309), 6-decimal pricing (QuantumNous#5332)
- Shared dialog wrapper, JSON code editor, classic Rsbuild support

SEO optimization:
- robots.txt: 10 AI crawler blocks + 22 path disallows + crawl-delay
- sitemap.xml: 7 public URLs with 6-language hreflang annotations
- index.html: hreflang tags, og:locale:alternate, og:image, canonical,
  5 structured data types (Organization, SoftwareApplication, FAQPage,
  WebSite, SearchAction), expanded keywords (gateway, agent router,
  aggregation, orchestration)
- i18n/config.ts: sync <html lang> with active language for SEO

Co-Authored-By: Claude <[email protected]>
ruanhangjian pushed a commit to ruanhangjian/new-api that referenced this pull request Jul 11, 2026
zhaodechao2008 pushed a commit to zhaodechao2008/new-api that referenced this pull request Jul 27, 2026
330079598 pushed a commit to 330079598/new-api that referenced this pull request Aug 19, 2026
salem-2007 added a commit to salem-2007/new-api that referenced this pull request Sep 10, 2026
junjundesk pushed a commit to junjundesk/new-api that referenced this pull request Oct 3, 2026
NaturalSelect pushed a commit to NaturalSelect/new-api that referenced this pull request Nov 5, 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.

Anthropic-compatible GLM upstream fails when request body is sent with Transfer-Encoding: chunked

2 participants