Skip to content

fix(cloudflare): preserve embedding response data - #8041

Open
donaldxdonald wants to merge 1 commit into
QuantumNous:mainfrom
donaldxdonald:codex/fix-cloudflare-embeddings
Open

donaldxdonald wants to merge 1 commit into
QuantumNous:mainfrom
donaldxdonald:codex/fix-cloudflare-embeddings

Conversation

@donaldxdonald

@donaldxdonald donaldxdonald commented Oct 26, 2026 •

Copy link
Copy Markdown

Agent

Codex CLI 0.163.0 / Codex desktop,2026-10-26 UTC。代码与测试由 AI 辅助完成;运行环境未提供完整模型 ID。

Links

Closes #8040。Related: #6201(同类旧修复,已关闭未合并)。

User request

review 一下 Cloudflare 的 proivder/channel 实现,检查 embeddings endpoint 的适配情况,确保符合 Cloudflare workers AI 的最新文档。
good. fix it

Out of scope — refuse

Matched: no。修复已有官方渠道,未开启透传;不涉及第三方封装或接入咨询。

Open gate — do not open unless all are satisfied

Open: yes。Issue 已记录现象、影响、频率和对照;下文包含实际修复前后测试结果。正文已精简、脱敏。

Kind

  • Bug fix
  • New feature
  • Performance / refactor
  • Docs
  • Other

Issue facts

官方镜像 calciumion/new-api:v1.0.0-rc.47,VPS / Docker Compose / PostgreSQL。Cloudflare 模型 @cf/qwen/qwen3-embedding-0.6b 的 POST /v1/embeddings 返回 200,但缺少 data,调用方无法取得向量。用户报告必现,实际有效调用 1/1 复现;同文本的上游原生接口返回 1024 维向量。完整脱敏证据见 #8040。

Change

让 embeddings 分支调用已有 openai.OpenaiHandler,不再 fallthrough 到聊天处理器。该 handler 在 embedding 格式下保留响应体,并返回上游 usage 或已有输入估算作为结算 usage。无新抽象、依赖或 DTO。

Research

Duplicate / prior art

已搜索 Issues/PRs 的 Cloudflare、Cloudflare embedding。#6201 曾包含类似修复,但未合并;#6265 是不同的聊天 usage 问题。本 PR 仅处理 #8040。

Docs and code

  • new-api embeddings 文档:已读 Markdown 源,提供 POST /v1/embeddings。
  • DeepWiki:尝试读取被安全检查阻断,未据此作结论。
  • 已读 README.zh_CN.md、AGENTS.md、billing 规则;检查 Cloudflare adaptor、cfHandler 和 OpenaiHandler。
  • Cloudflare 官方兼容文档:现有 /ai/v1/embeddings URL 和 model / input 字段无需修改。

Alternatives considered

复用已有 handler,而不是新增 embeddings handler 或在聊天 handler 内增加专用重编码,避免重复转换和丢字段。

Files

Path Why
relay/channel/cloudflare/adaptor.go 修正 embeddings 响应分支
controller/channel_test_request_test.go 在现有兼容测试文件集中补充四个回归用例

Behavior

  • Before: 聊天 DTO 重编码丢失 data,忽略有效上游 usage。
  • After: 保留单条/批量向量、base64 payload 和额外字段;ForceFormat 不改变 embedding 结构。缺失 usage 时内部回退估算,不向下游响应注入新字段。
  • Non-goals: 聊天、Responses、请求协议、价格和数据库变更。

Verification

默认 Go 依赖源 DNS 失败,后续命令临时设置 GOPROXY=https://goproxy.cn,direct,未修改全局或仓库配置。

  • go test ./controller -run '^TestCloudflareEmbeddingResponseCompatibility$' -count=1 -v:四个用例通过,验证响应 JSON 保留及结算 usage。
  • go test -overlay=/tmp/new-api-cloudflare-before-overlay.json ./controller -run '^TestCloudflareEmbeddingResponseCompatibility$' -count=1 -v:overlay 仅将 adaptor 替换为修复前 HEAD 内容;相同四个用例全部失败,出现向量丢失及 usage 被估算值覆盖。
  • make test:根模块只有 advancedcustom/TestAdaptorMasksDialedHostsInDialFailure/channel_proxy_host 失败(得到 unexpected EOF,而非预期包含代理主机的错误);controller 等其他包通过。使用同一修复前 overlay 单独运行该测试也失败,未改动无关代码。
  • 因 make 在根模块退出,分别在 relaykit/、tokenkit/ 运行 GOWORK=off go test ./...:均通过。
  • git diff --check、gofmt -l:通过,无格式输出。
  • 线上复现及 cf ai run 对照见 Cloudflare embeddings 返回 HTTP 200 但缺少向量数据 #8040;无 UI 变更或数据库变更。未验证修复后线上部署、Cloudflare 兼容接口独立直连或其他模型;不声称全量测试通过。

Risks

有效上游 usage 现在优先于本地估算,可能纠正该渠道的 token 结算量;价格及配额算法不变。缺失 usage 的回退已有回归覆盖。无认证变更。

Scope check

  • Single focused change: yes
  • Secrets included: no;未包含线上域名、账户 ID 或令牌。
  • Out of scope: no

Summary by CodeRabbit

  • Bug Fixes
    • Cloudflare embedding responses are now returned in the expected format for vector and base64 embeddings, including batched requests.
    • Responses without upstream usage data now include an estimated input usage total. Successful responses retain their JSON content type and response body.

@coderabbitai

coderabbitai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2a614dd5-d7ff-4ab2-a77a-5e35e635bd71
Commits

Reviewing files that changed from the base of the PR and between 34003e9 and 0863686.

Files selected for processing: 2
  • controller/channel_test_request_test.go
  • relay/channel/cloudflare/adaptor.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Walkthrough

The Cloudflare adaptor now sends embedding responses to openai.OpenaiHandler. New tests cover vector and base64 responses, batches, forced formatting, and absent upstream usage.

Changes

Cloudflare embedding response handling

Layer / File(s) Summary
Embedding response handling
relay/channel/cloudflare/adaptor.go, controller/channel_test_request_test.go
The adaptor routes embedding responses through openai.OpenaiHandler. Tests check response JSON, HTTP status, content type, and usage totals, including an estimate when upstream usage is absent.

Priority: Normal

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • #8038 — Describes the same Cloudflare embedding response defect, where the chat handler drops embedding data.

Possibly related PRs

  • QuantumNous/new-api#8026 — Changes the estimate used when an embedding response has no upstream usage, which is asserted by this PR’s tests.

Suggested reviewers: calcium-ion

Merge Risk: Minimal · up to 08636

The embedding response test covers the changed routing, and no specific merge-blocking issue has been identified. Normal checks remain appropriate.

Pre-merge checks: Passed 4 · Failed 1

Failed 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%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed 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 clearly and concisely describes the main change: preserving Cloudflare embedding response data.
Linked Issues check Passed The change satisfies #8040. relay/channel/cloudflare/adaptor.go now routes RelayModeEmbeddings to openai.OpenaiHandler instead of the chat handler. This preserves the upstream embeddings respons…
Out of Scope Changes check Passed The changes remain within #8040. The pull request changes one Cloudflare response branch and adds focused compatibility tests for that branch. No unrelated protocol, chat, Responses, pricing, database…
Full details: Linked Issues check

Explanation

The change satisfies #8040. relay/channel/cloudflare/adaptor.go now routes RelayModeEmbeddings to openai.OpenaiHandler instead of the chat handler. This preserves the upstream embeddings response, including data[].embedding and index. The added regression test covers single vectors, batches, base64 embeddings, extra fields, forced formatting, upstream usage, and missing-usage fallback.

Full details: Out of Scope Changes check

Explanation

The changes remain within #8040. The pull request changes one Cloudflare response branch and adds focused compatibility tests for that branch. No unrelated protocol, chat, Responses, pricing, database, dependency, or authentication changes are present in the reviewed diff.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

⚠️ This pull request shows signs of AI-generated slop (description_diff_mismatch). It has been flagged by CodeRabbit slop detection and should be reviewed carefully.

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

A rabbit checks the vectors in a row
The Cloudflare response lets embeddings show
Through OpenAI’s handler, payloads pass
With usage checked for each small batch
The bunny hops; the tests all glow

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

This branch has not been deployed

No deployments
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.

Cloudflare embeddings 返回 HTTP 200 但缺少向量数据

1 participant