From 31f2fc8ed4cc94dedcd0dc4c9d91fd5422c3ad89 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 29 Sep 2026 15:12:44 +0800 Subject: [PATCH] =?UTF-8?q?bedrock:=20GPT=20models=20are=20asked=20a=20rep?= =?UTF-8?q?ly's=20length=20as=20max=5Fcompletion=5Ftokens,=20and=20Claude?= =?UTF-8?q?=20Code's=20metadata=20is=20left=20out=20(#176:=20400=20Unsuppo?= =?UTF-8?q?rted=20parameter:=20'max=5Ftokens'=20is=20not=20supported=20wit?= =?UTF-8?q?h=20this=20model;=20Request=20metadata=20contains=20a=20value?= =?UTF-8?q?=20that=20violates=20the=20regular=20expression)=20=E2=80=94=20?= =?UTF-8?q?Bedrock's=20OpenAI=20endpoint=20turns=20max=5Ftokens=20away=20f?= =?UTF-8?q?or=20its=20GPT=20models,=20so=20the=20endpoint=20test=20and=20e?= =?UTF-8?q?very=20chat=20request=20to=20it=20(sent=20on=20as=20it=20came?= =?UTF-8?q?=20or=20built=20from=20another=20API)=20now=20ask=20max=5Fcompl?= =?UTF-8?q?etion=5Ftokens;=20and=20Claude=20Code's=20metadata.user=5Fid,?= =?UTF-8?q?=20a=20JSON=20string=20now,=20is=20not=20the=20plain=20id=20Bed?= =?UTF-8?q?rock's=20Anthropic=20endpoint=20checks=20it=20against,=20so=20i?= =?UTF-8?q?t=20is=20dropped=20there.=20Tested=20against=20a=20fake=20Bedro?= =?UTF-8?q?ck=20runtime=20(both=20fail=20on=20main)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/gateway/bedrock_test.go | 18 ++++++++++---- internal/gateway/gateway.go | 39 +++++++++++++++++++++++++++++++ internal/provider/bedrock_test.go | 17 ++++++++++++++ internal/provider/test.go | 3 +++ 4 files changed, 72 insertions(+), 5 deletions(-) diff --git a/internal/gateway/bedrock_test.go b/internal/gateway/bedrock_test.go index 467a11dee..7d9561096 100644 --- a/internal/gateway/bedrock_test.go +++ b/internal/gateway/bedrock_test.go @@ -23,6 +23,7 @@ type bedrockCall struct { path string head http.Header model string + body map[string]any } func (b *bedrock) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -31,8 +32,10 @@ func (b *bedrock) ServeHTTP(w http.ResponseWriter, r *http.Request) { Model string `json:"model"` } json.Unmarshal(body, &v) + var m map[string]any + json.Unmarshal(body, &m) b.mu.Lock() - b.calls = append(b.calls, bedrockCall{r.URL.Path, r.Header.Clone(), v.Model}) + b.calls = append(b.calls, bedrockCall{r.URL.Path, r.Header.Clone(), v.Model, m}) b.mu.Unlock() w.Header().Set("Content-Type", "text/event-stream") switch r.URL.Path { @@ -90,16 +93,16 @@ func TestBedrockRoutes(t *testing.T) { anthropic bool }{ {"claude on messages", "/v1/messages", - `{"model":"bedrock/apac.anthropic.claude-opus-5-5","max_tokens":20,"messages":[{"role":"user","content":"hi"}]}`, + `{"model":"bedrock/apac.anthropic.claude-opus-5-5","max_tokens":20,"metadata":{"user_id":"{\"device_id\":\"d\"}"},"messages":[{"role":"user","content":"hi"}]}`, "/anthropic/v1/messages", "apac.anthropic.claude-opus-5-5", "from claude", true}, {"claude on chat", "/v1/chat/completions", - `{"model":"bedrock/apac.anthropic.claude-opus-5-5","max_tokens":20,"messages":[{"role":"user","content":"hi"}]}`, + `{"model":"bedrock/apac.anthropic.claude-opus-5-5","max_tokens":20,"metadata":{"user_id":"{\"device_id\":\"d\"}"},"messages":[{"role":"user","content":"hi"}]}`, "/anthropic/v1/messages", "apac.anthropic.claude-opus-5-5", "from claude", true}, {"gpt-oss on messages", "/v1/messages", - `{"model":"bedrock/openai.gpt-oss-120b-1:0","max_tokens":20,"messages":[{"role":"user","content":"hi"}]}`, + `{"model":"bedrock/openai.gpt-oss-120b-1:0","max_tokens":20,"metadata":{"user_id":"{\"device_id\":\"d\"}"},"messages":[{"role":"user","content":"hi"}]}`, "/openai/v1/chat/completions", "openai.gpt-oss-120b-1:0", "from chat", false}, {"gpt-oss on chat", "/v1/chat/completions", - `{"model":"bedrock/openai.gpt-oss-120b-1:0","max_tokens":20,"messages":[{"role":"user","content":"hi"}]}`, + `{"model":"bedrock/openai.gpt-oss-120b-1:0","max_tokens":20,"metadata":{"user_id":"{\"device_id\":\"d\"}"},"messages":[{"role":"user","content":"hi"}]}`, "/openai/v1/chat/completions", "openai.gpt-oss-120b-1:0", "from chat", false}, } { t.Run(tc.name, func(t *testing.T) { @@ -112,11 +115,16 @@ func TestBedrockRoutes(t *testing.T) { t.Fatalf("upstream: %s %q", c.path, c.model) } if tc.anthropic { + if _, ok := c.body["metadata"]; ok { + t.Fatalf("metadata sent: %v", c.body) + } if c.head.Get("x-api-key") != "ABSK-test" || c.head.Get("Authorization") != "" || c.head.Get("anthropic-version") != "2023-06-01" { t.Fatalf("headers: %v", c.head) } } else if c.head.Get("Authorization") != "Bearer ABSK-test" { t.Fatalf("headers: %v", c.head) + } else if _, ok := c.body["max_tokens"]; ok || c.body["max_completion_tokens"] != float64(20) { + t.Fatalf("length asked as: %v", c.body) } }) } diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 9ef6dc282..add9eff7e 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -1172,7 +1172,15 @@ func (s *Server) passthrough(w http.ResponseWriter, r *http.Request, p provider. if strings.HasSuffix(p.Host(), "openai.com") { body = withoutFields(body, "enable_thinking") } + if p.IsBedrock() { + body = asCompletionTokens(body) + } case provider.Anthropic: + if p.IsBedrock() { + // Claude Code's metadata.user_id, a JSON string these days, is + // not the plain id Bedrock checks it against (#176) + body = withoutFields(body, "metadata") + } if !anthropicModel.MatchString(model) { body = thinkingOffUnlessAsked(body) } @@ -1361,6 +1369,9 @@ func (s *Server) forwardTranslated(ctx context.Context, p provider.Provider, to r.CacheKey, req = "", &r } body := build(to, req, model, p.Host(), p.RejectsTemperature(model)) + if to == provider.Chat && p.IsBedrock() { + body = asCompletionTokens(body) + } if to == provider.CodeAssist && p.Account != nil { body = buildCodeAssist(req, model, p.Account.Agent) } @@ -1929,6 +1940,34 @@ func withFields(body []byte, fields map[string]any) []byte { // withoutFields drops fields the vendor refuses to see: Qoder asks every // model for Qwen's enable_thinking, which OpenAI turns away as an // unrecognized argument. +// asCompletionTokens asks a chat request's reply length as +// max_completion_tokens in place of max_tokens, as Bedrock's OpenAI +// endpoint takes it: its GPT models turn max_tokens away (#176). +func asCompletionTokens(body []byte) []byte { + if !bytes.Contains(body, []byte(`"max_tokens"`)) { + return body + } + dec := json.NewDecoder(bytes.NewReader(body)) + dec.UseNumber() + var m map[string]any + if dec.Decode(&m) != nil { + return body + } + n, ok := m["max_tokens"] + if !ok { + return body + } + delete(m, "max_tokens") + if _, has := m["max_completion_tokens"]; !has { + m["max_completion_tokens"] = n + } + out, err := json.Marshal(m) + if err != nil { + return body + } + return out +} + func withoutFields(body []byte, fields ...string) []byte { found := false for _, f := range fields { diff --git a/internal/provider/bedrock_test.go b/internal/provider/bedrock_test.go index c69f1756e..c1a7746de 100644 --- a/internal/provider/bedrock_test.go +++ b/internal/provider/bedrock_test.go @@ -100,3 +100,20 @@ func TestBedrockAuth(t *testing.T) { t.Fatalf("chat: %v", h) } } + +// Bedrock's GPT models turn max_tokens away (#176): its chat test asks +// the reply's length as max_completion_tokens. +func TestBedrockTestAsksCompletionTokens(t *testing.T) { + p, err := FromPreset("bedrock") + if err != nil { + t.Fatal(err) + } + _, body := tinyBody(p, Chat, "global.openai.gpt-6-sol") + if !strings.Contains(body, `"max_completion_tokens":16`) || strings.Contains(body, `"max_tokens"`) { + t.Fatalf("body: %s", body) + } + _, body = tinyBody(p, Anthropic, "global.anthropic.claude-opus-5-5") + if !strings.Contains(body, `"max_tokens":16`) { + t.Fatalf("anthropic body: %s", body) + } +} diff --git a/internal/provider/test.go b/internal/provider/test.go index 2946aeeb9..ab26e16e0 100644 --- a/internal/provider/test.go +++ b/internal/provider/test.go @@ -63,6 +63,9 @@ func tiny(q Provider, proto Protocol, model string) (url, body string) { func tinyBody(q Provider, proto Protocol, model string) (url, body string) { switch proto { case Chat: + if q.IsBedrock() { + return q.Chat + "/chat/completions", fmt.Sprintf(`{"model":%q,"messages":[{"role":"user","content":"hi"}],"max_completion_tokens":16}`, model) + } return q.Chat + "/chat/completions", fmt.Sprintf(`{"model":%q,"messages":[{"role":"user","content":"hi"}],"max_tokens":16}`, model) case Responses: return q.Responses + "/responses", fmt.Sprintf(`{"model":%q,"input":"hi","max_output_tokens":16}`, model)