fix(coderd/x/chatd): coerce Bedrock reasoning summaries to auto
This commit is contained in:
1 parent
b3f43513a8
commit
a4878f88c2
4 files changed
+155
No files matched your search
@@ -925,6 +925,8 @@ Debug recording replaces the wrapped client and preserves the resolved transport
|
||||
|
||||
Azure is deliberately exempt: its provider always enables the Responses API for known models and exposes no equivalent per-model hook, so the transport keeps following the known-model list for Azure. Ignoring the override there is what keeps the decisions above in agreement with the Azure client. The exemption is narrower than it appears, because chatd never builds an azure-typed provider as a fantasy azure client: `fantasyConfigForAIBridge` folds every provider type other than anthropic, bedrock, and openai into openai-compat, which always speaks Chat Completions. Bedrock is the exception within that set: its fantasy client depends on the model ID, so `anthropic.*` bedrock models fold to the Anthropic Messages client while non-anthropic bedrock models fold to the OpenAI Responses client.
|
||||
|
||||
For Bedrock models using the OpenAI wire format, `resolveModelCall` coerces any set `provider_options.openai.reasoning_summary` value to `auto` before building per-call provider options. Mantle rejects `concise` and `detailed` with HTTP 400 and only accepts `auto`, currently without returning summaries. Coercion keeps the stored config portable between Bedrock and direct OpenAI and allows summaries to flow if AWS adds support later. Anthropic models on Bedrock and non-Bedrock providers are unchanged.
|
||||
|
||||
Both transports read the same `provider_options.openai` config, but not every field applies to both wire formats. The table below records, per field, which transport honors it; `TestProviderOptionsTransportParity` fails when a field is honored on one transport and silently ignored on the other without being recorded there as intentional.
|
||||
|
||||
| `provider_options.openai` field | Responses | Chat Completions |
|
||||
|
||||
@@ -189,6 +189,25 @@ func (p *Server) newModel(
|
||||
)
|
||||
}
|
||||
|
||||
func coerceBedrockReasoningSummary(providerType database.AIProviderType, model string, callConfig codersdk.ChatModelCallConfig) codersdk.ChatModelCallConfig {
|
||||
if providerType != database.AIProviderTypeBedrock || bedrockIsAnthropicModel(model) ||
|
||||
callConfig.ProviderOptions == nil || callConfig.ProviderOptions.OpenAI == nil ||
|
||||
callConfig.ProviderOptions.OpenAI.ReasoningSummary == nil ||
|
||||
*callConfig.ProviderOptions.OpenAI.ReasoningSummary == "auto" {
|
||||
return callConfig
|
||||
}
|
||||
|
||||
// Mantle rejects concise/detailed with HTTP 400 and only accepts auto.
|
||||
// Coercing rather than dropping keeps configs portable to direct OpenAI
|
||||
// and allows summaries if AWS adds support later.
|
||||
providerOptions := *callConfig.ProviderOptions
|
||||
openAI := *providerOptions.OpenAI
|
||||
openAI.ReasoningSummary = new("auto")
|
||||
providerOptions.OpenAI = &openAI
|
||||
callConfig.ProviderOptions = &providerOptions
|
||||
return callConfig
|
||||
}
|
||||
|
||||
func parseModelConfigOptions(configOptions json.RawMessage) (codersdk.ChatModelCallConfig, error) {
|
||||
var callConfig codersdk.ChatModelCallConfig
|
||||
if len(configOptions) == 0 {
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"go.uber.org/mock/gomock"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"cdr.dev/slog/v3/sloggers/slogtest"
|
||||
"github.com/coder/coder/v2/coderd/aibridge"
|
||||
"github.com/coder/coder/v2/coderd/database"
|
||||
"github.com/coder/coder/v2/coderd/database/dbmock"
|
||||
@@ -458,6 +459,138 @@ func TestAIGatewayModelBedrockInferredOpenAIDefaultsToResponses(t *testing.T) {
|
||||
require.Equal(t, "/v1/responses", <-paths)
|
||||
}
|
||||
|
||||
func TestBedrockReasoningSummary(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, summary := range []*string{nil, new("auto"), new("concise"), new("detailed"), new(""), new("other")} {
|
||||
name := "Unset"
|
||||
if summary != nil {
|
||||
name = "Set_" + *summary
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
config := codersdk.ChatModelCallConfig{
|
||||
Temperature: new(0.5),
|
||||
ProviderOptions: &codersdk.ChatModelProviderOptions{
|
||||
OpenAI: &codersdk.ChatModelOpenAIProviderOptions{
|
||||
ReasoningSummary: summary,
|
||||
User: new("test-user"),
|
||||
},
|
||||
Anthropic: &codersdk.ChatModelAnthropicProviderOptions{SendReasoning: new(true)},
|
||||
},
|
||||
}
|
||||
before, err := json.Marshal(config)
|
||||
require.NoError(t, err)
|
||||
got := coerceBedrockReasoningSummary(database.AIProviderTypeBedrock, "openai.gpt-5.6-luna", config)
|
||||
after, err := json.Marshal(config)
|
||||
require.NoError(t, err)
|
||||
require.JSONEq(t, string(before), string(after), "input config must not be mutated")
|
||||
require.Equal(t, config.Temperature, got.Temperature)
|
||||
require.Equal(t, config.ProviderOptions.OpenAI.User, got.ProviderOptions.OpenAI.User)
|
||||
require.Same(t, config.ProviderOptions.Anthropic, got.ProviderOptions.Anthropic)
|
||||
if summary == nil || *summary == "auto" {
|
||||
require.Same(t, config.ProviderOptions, got.ProviderOptions)
|
||||
} else {
|
||||
require.NotSame(t, config.ProviderOptions, got.ProviderOptions)
|
||||
require.NotSame(t, config.ProviderOptions.OpenAI, got.ProviderOptions.OpenAI)
|
||||
require.Equal(t, "auto", *got.ProviderOptions.OpenAI.ReasoningSummary)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
for _, config := range []codersdk.ChatModelCallConfig{{}, {ProviderOptions: &codersdk.ChatModelProviderOptions{}}} {
|
||||
got := coerceBedrockReasoningSummary(database.AIProviderTypeBedrock, "openai.gpt-5.6-luna", config)
|
||||
require.Equal(t, config, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAIGatewayModelBedrockReasoningSummary(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
providerType database.AIProviderType
|
||||
model string
|
||||
wantSummary string
|
||||
wantPath string
|
||||
}{
|
||||
{name: "BedrockOpenAI", providerType: database.AIProviderTypeBedrock, model: "openai.gpt-5.6-luna", wantSummary: "auto", wantPath: "/v1/responses"},
|
||||
{name: "BedrockAnthropic", providerType: database.AIProviderTypeBedrock, model: "anthropic.claude-sonnet-4", wantSummary: "detailed", wantPath: "/v1/messages"},
|
||||
{name: "OpenAI", providerType: database.AIProviderTypeOpenai, model: "openai.gpt-5.6-luna", wantSummary: "detailed", wantPath: "/v1/responses"},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
requests := make(chan *http.Request, 1)
|
||||
factory := &aibridgeTestFactory{rt: roundTripFunc(func(req *http.Request) (*http.Response, error) {
|
||||
requests <- req
|
||||
body := `{"id":"resp_test","object":"response","created_at":0,"status":"completed","output":[{"id":"msg_test","type":"message","role":"assistant","content":[{"type":"output_text","text":"hello"}]}],"usage":{"input_tokens":1,"output_tokens":1,"total_tokens":2}}`
|
||||
if tt.wantPath == "/v1/messages" {
|
||||
body = `{"id":"msg_test","type":"message","role":"assistant","content":[{"type":"text","text":"hello"}],"stop_reason":"end_turn","usage":{"input_tokens":1,"output_tokens":1}}`
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Header: http.Header{"Content-Type": []string{"application/json"}},
|
||||
Body: io.NopCloser(strings.NewReader(body)),
|
||||
Request: req,
|
||||
}, nil
|
||||
})}
|
||||
provider := aibridgeTestAIProvider(uuid.New(), "test-provider", tt.providerType)
|
||||
db := dbmock.NewMockStore(gomock.NewController(t))
|
||||
db.EXPECT().GetAIProviderByID(gomock.Any(), provider.ID).Return(provider, nil)
|
||||
server := &Server{
|
||||
db: db,
|
||||
logger: slogtest.Make(t, nil),
|
||||
aibridgeTransportFactory: aibridgeTestFactoryPointer(factory),
|
||||
}
|
||||
config := codersdk.ChatModelCallConfig{
|
||||
MaxOutputTokens: new(int64(64)),
|
||||
ProviderOptions: &codersdk.ChatModelProviderOptions{
|
||||
OpenAI: &codersdk.ChatModelOpenAIProviderOptions{ReasoningSummary: new("detailed")},
|
||||
},
|
||||
}
|
||||
if tt.providerType == database.AIProviderTypeOpenai {
|
||||
config.OpenAIConfig = &codersdk.ChatModelOpenAIConfig{UseResponsesAPI: new(true)}
|
||||
}
|
||||
options, err := json.Marshal(config)
|
||||
require.NoError(t, err)
|
||||
resolved, err := server.resolveModelCall(t.Context(), modelCallSpec{
|
||||
chat: database.Chat{ID: uuid.New(), OwnerID: uuid.New()},
|
||||
explicitConfig: &database.ChatModelConfig{
|
||||
ID: uuid.New(),
|
||||
Model: tt.model,
|
||||
AIProviderID: uuid.NullUUID{UUID: provider.ID, Valid: true},
|
||||
Options: options,
|
||||
},
|
||||
buildOptions: modelBuildOptions{ActiveAPIKeyID: uuid.NewString()},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
call := resolved.newCall()
|
||||
call.Prompt = []fantasy.Message{{
|
||||
Role: fantasy.MessageRoleUser,
|
||||
Content: []fantasy.MessagePart{fantasy.TextPart{Text: "hello"}},
|
||||
}}
|
||||
_, err = resolved.model.LanguageModel().Generate(t.Context(), call)
|
||||
require.NoError(t, err)
|
||||
request := <-requests
|
||||
require.Equal(t, tt.wantPath, request.URL.Path)
|
||||
var body struct {
|
||||
Reasoning *struct {
|
||||
Summary string `json:"summary"`
|
||||
} `json:"reasoning"`
|
||||
}
|
||||
require.NoError(t, json.NewDecoder(request.Body).Decode(&body))
|
||||
if tt.wantPath == "/v1/messages" {
|
||||
require.Nil(t, body.Reasoning)
|
||||
require.Equal(t, config.ProviderOptions, resolved.callConfig.ProviderOptions)
|
||||
} else {
|
||||
require.NotNil(t, body.Reasoning)
|
||||
require.Equal(t, tt.wantSummary, body.Reasoning.Summary)
|
||||
}
|
||||
require.Equal(t, tt.wantSummary, *resolved.callConfig.ProviderOptions.OpenAI.ReasoningSummary)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAIGatewayModelAppliesResponsesAPIOverride(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
@@ -167,6 +167,7 @@ func (p *Server) resolveModelCall(ctx context.Context, spec modelCallSpec) (reso
|
||||
}
|
||||
out.model = model
|
||||
|
||||
out.callConfig = coerceBedrockReasoningSummary(out.route.Provider.Type, modelName, out.callConfig)
|
||||
out.providerOptions = chatprovider.ProviderOptionsForCall(out.model, out.callConfig, spec.requestedEffort)
|
||||
|
||||
p.logger.Debug(ctx, "resolved model call",
|
||||
|
||||
Reference in new issue
Block a user