Skip to content

fix(openai): apply mappings to synthesized reasoning effort - #8161

Merged
Wei-Shaw merged 7 commits into
Wei-Shaw:mainfrom
wucm667:fix/issue-8156-synthesized-reasoning-mapping
Nov 10, 2026
Merged

Wei-Shaw merged 7 commits into
Wei-Shaw:mainfrom
wucm667:fix/issue-8156-synthesized-reasoning-mapping

Conversation

@wucm667

@wucm667 wucm667 commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

背景

  • 合成的 reasoning effort 需要遵循 OpenAI reasoning effort 映射。

改动

  • 对合成的 reasoning effort 应用配置的映射规则。
  • 补充默认 effort 与禁用 thinking 场景的覆盖验证。

验证

  • 目标 handler/service 测试通过。
  • 后端 unit 全量测试通过。
  • 后端 integration 全量测试通过。
  • 全仓库静态检查通过,0 issues。
  • 服务端无 CGO 构建通过。
  • 前端依赖锁定安装、代码检查、类型检查、关键 30 个文件的 574 项测试及构建全部通过。
  • 差异格式检查通过。

Fixes #8156

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved issues allow unsupported aliases and sampling fields, cause token-counting failures, and can forward mapped reasoning that violates policy ceilings or GLM’s native scale.

5 open issues
What changed in this PR

This PR addresses #8156 by applying configured mappings to synthesized none reasoning effort in the Messages bridge, while preserving ordinary defaults and validation of unsupported explicit values.

Changes:

  • Bind none mappings when clients omit output_config.effort.
  • Validate final effort after policy application in Responses and Chat Completions forwarding.
  • Add regression coverage for disabled thinking, defaults, mappings, and rejection behavior.
File Description
backend/​internal/​service/​openai_reasoning_effort_policy.go Adds Messages-specific policy binding.
backend/​internal/​service/​openai_messages_effort_policy_test.go Tests forwarding and effort-policy behavior.
backend/​internal/​service/​openai_gateway_request_body.go Separates raw-effort validation from compatibility checks.
backend/​internal/​service/​openai_gateway_messages.go Defers synthesized-effort validation until after mapping.
backend/​internal/​service/​openai_gateway_messages_chat_fallback.go Validates final Chat Completions effort after mapping.
backend/​internal/​service/​apicompat_config.go Removes the replaced conversion wrapper.
backend/​internal/​pkg/​apicompat/​anthropic_to_responses.go Adds optional deferred effort validation.
backend/​internal/​pkg/​apicompat/​anthropic_effort_validation_test.go Tests strict and deferred conversion validation.
backend/​internal/​handler/​messages_effort_policy_test.go Tests policy binding across supported platforms.
backend/​internal/​handler/​composite_platform.go Uses Messages-specific policy binding.
backend/​internal/​handler/​composite_platform_test.go Covers synthesized defaults and disabled thinking.

Review effort: Balanced

Comment on lines +157 to +160
responsesReq, err := apicompat.AnthropicToResponsesWithOptions(&anthropicReq, apicompat.AnthropicToResponsesOptions{
MinOutputTokens: configuredMinOutputTokens(s.cfg),
DeferReasoningEffortValidation: true,
})

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已在当前 head f6ea845 保留 count_tokens 延迟 effort 校验,新增 disabled thinking/none 映射计数回归;完整本地检查及远端 CI 通过。

chatBody = policyBody
}
}
if err := validateGPT61SolCompatRequest(chatBody, upstreamModel); err != nil {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

按维护者最新明确意见,f6ea845c58266af39bcaef3181d9b8f9692d2772 保持 main 的 GLM 归一化→分组映射/上限顺序,移除改变 downgrade 语义的额外 GLM helper;不在此 PR 新增原生档位限制。medium 出站与计费、显式 downgrade/deny 的流式和非流式边界已验证。

Comment on lines +104 to +107
if err := validateGPT61SolCompatRequest(chatBody, upstreamModel); err != nil {
writeAnthropicError(c, http.StatusBadRequest, "invalid_request_error", err.Error())
return nil, err
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当前 head f6ea845 在解析目标 gpt-6.1-sol 后移除 temperature/top_p,再校验最终 effort;转发边界回归与完整本地/远端 CI 通过。

return nil
}

func validateGPT61SolRawEffort(body []byte, model string) error {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当前 head f6ea845 对模型规范化后检查 minimal 后缀,保留显式不支持值的拒绝;Responses/Chat 边界测试及完整本地/远端 CI 通过。

// client omits output_config.effort, binding only mappings for synthesized none.
func WithOpenAIReasoningEffortPolicyForMessages(ctx context.Context, body []byte, maxEffort string, mappings []ReasoningEffortMapping, overLimit string) context.Context {
if strings.TrimSpace(gjson.GetBytes(body, "output_config.effort").String()) == "" {
maxEffort = ""

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当前 head f6ea845 对 disabled thinking 保留上限,映射后继续 downgrade/deny;普通省略 effort 不限制默认档。对应边界回归及完整本地/远端 CI 通过。

@Wei-Shaw

Wei-Shaw commented Nov 4, 2026

Copy link
Copy Markdown
Owner

感谢更新。上轮提到的两点已确认修复:省略 effort 时不再触发 deny 上限 403;gpt-6.1-sol 的兼容校验已移到策略之后。剩余以下问题:

1. 省略 effort + thinking disabled 时可绕过分组上限

WithOpenAIReasoningEffortPolicyForMessages 在省略 effort 时清空 maxEffort,只保留 none 源映射。转换器在 thinking.type=disabled 时合成 none,若分组映射为 none → xhigh 且上限为 low,ApplyReasoningEffortPolicy 映射成 xhigh 后因 hasMax=false 不再截顶,最终发出 xhigh。同样配置下显式传 effort 会被截到 low 或 403。也就是说客户端只要省略 effort 就能绕过上限;NormalizeReasoningEffortMappings 也不会拦截"映射目标高于上限"的配置。

建议:省略 effort 且 thinking.type == "disabled" 时保留 maxEffort。none 本身不参与上限比较(reasoningEffortRank 对 none 返回 false),所以保留上限不会误伤默认 medium,只会作用在映射后的结果上;并补一条"省略 + disabled + 映射超上限"的用例。

2. 与 #8309 冲突,需 rebase

本 PR 落后 main 约 90 个提交(单独合入 main 无冲突),但与 #8309 在 bindOpenAIReasoningEffortPolicyForMessagesRequest 和 openai_gateway_messages_chat_fallback.go 的同一段冲突,任意顺序都一样。建议 #8309 先修复合入后本 PR rebase。合并时请注意:

3. 小问题

  • 新函数不再检查 Type == String,非字符串 effort 会被当成显式值(service 层后续 Unmarshal 会失败,不算回归)。
  • validateGPT61SolCompatRequest 的注释 "before conversion" 已过时。

另:#5602 与本 PR 语义相反(禁止 none 作为映射源),且基于旧签名,已无法应用到 main;main 在 7969751 已支持 none 映射源,不影响本 PR。

@Wei-Shaw

Wei-Shaw commented Nov 6, 2026

Copy link
Copy Markdown
Owner

感谢更新。上轮三点已确认修复:省略 effort 且 thinking.type=disabled 时保留上限;CN 目标恢复为"省略 effort 不绑定";WithOpenAIReasoningEffortPolicyForMessages 补了 Type == String 判断。count_tokens 延迟校验、ForwardAsAnthropic 策略后最终校验、chat fallback 对 sol 去掉 temperature/top_p,都没有问题。

剩余一个阻塞问题:

1. GLM 路径把 downgrade 强制改成 deny,导致原本可用的请求返回 403

新增的 applyOpenAIReasoningEffortPolicyForGLMFromContext(OpenAI 平台账号走 Messages chat fallback、上游为 glm-* 时生效):模型不是 glm-5.3 且分组上限 ≤ medium 时,legalCeiling 为空,代码会直接执行 overLimit = ReasoningEffortOverLimitDeny,不管分组配置的是什么。

  • 分组配置为 downgrade(默认值)+ 上限 medium + 上游 glm-5.2 时,显式 effort 的请求全部 403。显式 low 也会被拦:NormalizeGLMOpenAIReasoningEffort 先把 low/medium 归一化为 high,high 再超出上限。
  • main 原来的行为是截到 medium 后转发,并按 medium 档计费。TestOpenAIMessagesGLMReasoningPricingUsesForwardedEffort 中的用例 {"xhigh", "medium", "medium", 1.5} 覆盖的就是这个场景。
  • 本 PR 把这条用例改成了 {"xhigh", "high", "high", 2}。把 main 原版测试放到本 PR 代码上运行,两个子用例都失败:
reasoning effort "max" exceeds this group's limit of "medium"

这部分改动不在 #8156 的范围内。建议回退 GLM helper,chat fallback 保持 main 原有的"先 GLM 归一化、再应用策略"的写法,并恢复原测试用例。如果确实要处理"GLM 不接受 medium",请另开 PR 并附上上游证据;即便如此,也不应把管理员配置的 downgrade 改成 deny。

2. 冲突

@wucm667

wucm667 commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

已在 f6ea845 按最新意见移除 GLM 专用复检,恢复先归一化、再应用分组策略,不再把 downgrade 改为 deny。恢复 medium 上限与计费断言,并覆盖 low 请求、显式 downgrade、deny、流式和非流式出站边界。已整合最新 main、解决冲突;独立复核、本地完整检查及当前 head 远端 CI/安全检查通过。保留 #8309 先合、本 PR 后整合的顺序。

@Wei-Shaw
Wei-Shaw merged commit 5fe13c3 into Wei-Shaw:main Nov 10, 2026
9 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 10, 2026
@wucm667
wucm667 deleted the fix/issue-8156-synthesized-reasoning-mapping branch November 11, 2026 07:02
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Reasoning effort mapping is skipped for synthesized none in Messages → Responses bridge

3 participants