fix(providers): remove hardcoded max_tokens in anthropic-messages provider

- remove hardcoded max_tokens value (4096) from buildRequestBody
- read max_tokens directly from options parameter
- add error handling when max_tokens is missing from options
- update test cases to include max_tokens in options

This fix ensures the provider respects the config default value (32768)
or system fallback (8192) instead of always using the hardcoded 4096.
This commit is contained in:
ZaneTung 2026-03-12 09:59:53 +08:00
parent ddfa437f9c
commit 94c7d879f8
2 changed files with 38 additions and 19 deletions

View file

@ -140,15 +140,16 @@ func buildRequestBody(
model string, model string,
options map[string]any, options map[string]any,
) (map[string]any, error) { ) (map[string]any, error) {
result := map[string]any{ // max_tokens is required and guaranteed by agent loop
"model": model, maxTokens, ok := asInt(options["max_tokens"])
"max_tokens": int64(4096), if !ok {
"messages": []any{}, return nil, fmt.Errorf("max_tokens is required in options")
} }
// Set max_tokens from options result := map[string]any{
if mt, ok := asInt(options["max_tokens"]); ok { "model": model,
result["max_tokens"] = int64(mt) "max_tokens": int64(maxTokens),
"messages": []any{},
} }
// Set temperature from options // Set temperature from options

View file

@ -28,10 +28,12 @@ func TestBuildRequestBody(t *testing.T) {
{Role: "user", Content: "Hello, world!"}, {Role: "user", Content: "Hello, world!"},
}, },
model: "test-model", model: "test-model",
options: map[string]any{}, options: map[string]any{
"max_tokens": 8192,
},
want: map[string]any{ want: map[string]any{
"model": "test-model", "model": "test-model",
"max_tokens": int64(4096), "max_tokens": int64(8192),
"messages": []any{ "messages": []any{
map[string]any{ map[string]any{
"role": "user", "role": "user",
@ -47,10 +49,12 @@ func TestBuildRequestBody(t *testing.T) {
{Role: "assistant", Content: "4"}, {Role: "assistant", Content: "4"},
}, },
model: "test-model", model: "test-model",
options: map[string]any{}, options: map[string]any{
"max_tokens": 8192,
},
want: map[string]any{ want: map[string]any{
"model": "test-model", "model": "test-model",
"max_tokens": int64(4096), "max_tokens": int64(8192),
"messages": []any{ "messages": []any{
map[string]any{ map[string]any{
"role": "user", "role": "user",
@ -75,10 +79,12 @@ func TestBuildRequestBody(t *testing.T) {
{Role: "user", Content: "Hello"}, {Role: "user", Content: "Hello"},
}, },
model: "test-model", model: "test-model",
options: map[string]any{}, options: map[string]any{
"max_tokens": 8192,
},
want: map[string]any{ want: map[string]any{
"model": "test-model", "model": "test-model",
"max_tokens": int64(4096), "max_tokens": int64(8192),
"system": "You are a helpful assistant.", "system": "You are a helpful assistant.",
"messages": []any{ "messages": []any{
map[string]any{ map[string]any{
@ -110,6 +116,16 @@ func TestBuildRequestBody(t *testing.T) {
}, },
}, },
}, },
{
name: "missing max_tokens returns error",
messages: []Message{
{Role: "user", Content: "Test"},
},
model: "test-model",
options: map[string]any{},
want: nil,
wantErr: true,
},
{ {
name: "with tools", name: "with tools",
messages: []Message{ messages: []Message{
@ -133,10 +149,12 @@ func TestBuildRequestBody(t *testing.T) {
}, },
}, },
model: "test-model", model: "test-model",
options: map[string]any{}, options: map[string]any{
"max_tokens": 8192,
},
want: map[string]any{ want: map[string]any{
"model": "test-model", "model": "test-model",
"max_tokens": int64(4096), "max_tokens": int64(8192),
"messages": []any{ "messages": []any{
map[string]any{ map[string]any{
"role": "user", "role": "user",