[BUG] AWS Claude /v1/messages 流式响应会覆盖模型重定向后的 upstream_model_name

Open Beginner friendly
#4,522 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
65/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
aws, go
Domain
api, backend, cloud

Research direction

Start with relay/channel/claude/relay-claude.go and inspect the message_start handling, then trace RelayInfo through relay/helper/model_mapped.go and relay/channel/aws/relay-aws.go. Check service/log_info_generate.go to confirm that a mapped upstream model remains available after streaming, without changing the non-streaming behavior.

Written by the indexing model from the issue text.

Description

bug

提交前必读(请勿删除本节)

您当前的 newapi 版本

9f8a4ec,提交时间:2026-04-28 11:29:30 +0800

提交确认

  • 我已确认目前没有类似 issue
  • 我已完整查看过文档 https://docs.newapi.ai/ 和项目 README,尤其是常见问题部分
  • 我未删除此模板中的任何引导内容或小节标题,并会按要求完整填写
  • 我理解项目维护者精力有限,不遵循模板要求的 issue 可能会被无视或直接关闭

问题描述

/v1/messages 使用 AWS Claude 渠道,并开启模型重定向/模型映射时,非流式请求可以正确显示重定向后的上游模型;但流式请求看起来会出现“模型重定向失效”。

已确认:

  • 未开启任何透传配置。
  • 渠道类型是 AWS Claude。
  • 请求入口是 /v1/messages
  • stream=true 时复现。
  • stream=false 时正常。
  • 渠道亲和性开启或关闭都不影响该问题。

预期行为

当模型映射生效后,RelayInfo.UpstreamModelName 应始终表示实际请求上游使用的模型名/模型 ID。流式响应解析不应把它改回响应里的 message.model,否则日志、usage fallback、计费相关的 other.upstream_model_name 会表现为模型重定向失效。

实际行为

AWS Claude 流式请求阶段实际已经使用了映射后的模型:

  • /v1/messages 会进入 Claude relay:
    • router/relay-router.go
  • 请求阶段会执行模型映射:
    • relay/claude_handler.go
  • ModelMappedHelper 会把映射后的模型写入 info.UpstreamModelName,并回写 request model:
    • relay/helper/model_mapped.go
  • AWS SDK 流式请求使用的是 getAwsModelID(info.UpstreamModelName),并传给 InvokeModelWithResponseStreamInput.ModelId
    • relay/channel/aws/relay-aws.go

但是在 Claude 流式响应处理时,message_start 又覆盖了 info.UpstreamModelName

if claudeResponse.Type == "message_start" {
    // message_start, 获取usage
    if claudeResponse.Message != nil {
        info.UpstreamModelName = claudeResponse.Message.Model
    }
}

位置:

  • relay/channel/claude/relay-claude.go

这会导致流式请求后续日志里的 upstream_model_name 变成响应中的 message.model,而不是实际请求 AWS Bedrock 使用的重定向后模型。

日志记录位置:

  • service/log_info_generate.go
if relayInfo.IsModelMapped {
    other["is_model_mapped"] = true
    other["upstream_model_name"] = relayInfo.UpstreamModelName
}

影响

  • /v1/messages + AWS Claude + stream=true 时,usage log / 请求日志中显示的 upstream_model_name 可能错误。
  • 从日志看会像模型重定向失效,但实际 AWS InvokeModelWithResponseStreamInput.ModelId 在请求创建时已经使用映射后的模型。
  • 后续依赖 info.UpstreamModelName 的 usage fallback 或计费逻辑也可能拿到被覆盖后的模型名。

初步判断

这是流式响应解析阶段对 RelayInfo.UpstreamModelName 的误修改。UpstreamModelName 更适合作为“实际请求上游使用的模型”保留,不应被响应中的 message.model 覆盖。

建议修复

可以考虑移除该赋值,或者至少在模型映射已生效时不要覆盖:

if claudeResponse.Type == "message_start" && claudeResponse.Message != nil && !info.IsModelMapped {
    info.UpstreamModelName = claudeResponse.Message.Model
}

更干净的方式是:响应里的模型名只记录到 claudeInfo.Model 或新增独立字段,不复用/覆盖 RelayInfo.UpstreamModelName

复现步骤

  1. 配置一个 AWS Claude 渠道。
  2. 配置模型重定向/模型映射,例如将客户端请求模型 A 映射到上游模型 B。
  3. 使用 /v1/messages 发起 Claude Messages 请求。
  4. 对比:
    • stream=false:日志中的上游模型显示正常。
    • stream=true:日志中的上游模型可能被 message_start.message.model 覆盖,看起来像重定向失效。
Dominant language
Go
Stars
48.5k
Forks
11.6k
Avg merge
1d 9h
Merged PRs (30d)
61

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from QuantumNous/new-api

All issues in QuantumNous/new-api

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.