bedrock: GPT models are asked a reply's length as max_completion_tokens, and Claude Code's metadata is left out (#176: 400 Unsupported parameter: 'max_tokens' is not supported with this model; Request metadata contains a value that violates the regular expression) — Bedrock's OpenAI endpoint turns max_tokens away for its GPT models, so the endpoint test and every chat request to it (sent on as it came or built from another API) now ask max_completion_tokens; and Claude Code's metadata.user_id, a JSON string now, is not the plain id Bedrock's Anthropic endpoint checks it against, so it is dropped there. Tested against a fake Bedrock runtime (both fail on main)
This commit is contained in:
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user