Skip to content

Commit cea3e57

Browse files
committed
fix: send max_completion_tokens to Azure OpenAI reasoning models
Bump the fantasy fork to its sync with upstream v0.48.3 (coder/fantasy#86), whose Chat Completions reasoning check knows gpt-6 through gpt-9. Azure OpenAI models now also obey the Reasoning Model setting, and the model form shows it for them, as a deployment name does not have to say what model it serves. This replaces the earlier approach of routing Azure OpenAI through the OpenAI client.
1 parent b42b888 commit cea3e57

21 files changed

Lines changed: 261 additions & 310 deletions

‎coderd/exp_chats.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8583,7 +8583,7 @@ func (e *chatModelConfigProviderModelError) Error() string {
85838583
}
85848584

85858585
func validateChatModelConfigProviderModel(aiProvider database.AIProvider, model string, config *codersdk.ChatModelCallConfig) *chatModelConfigProviderModelError {
8586-
if err := chatopenai.ValidateReasoningMode(codersdk.AIProviderType(aiProvider.Type), config); err != nil {
8586+
if err := chatopenai.ValidateReasoningMode(string(aiProvider.Type), config); err != nil {
85878587
return &chatModelConfigProviderModelError{
85888588
Response: codersdk.Response{Message: "Invalid model config.", Detail: err.Error()},
85898589
}

‎coderd/x/chatd/ARCHITECTURE.md‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,9 +1262,7 @@ Request preparation reads the transport from the model instead of recomputing it
12621262

12631263
The first two happen together in `chatprovider.ProviderOptionsForCall`, the only entry point in `chatprovider` that builds provider options for a call; it delegates transport-aware OpenAI conversion to `chatopenai.ProviderOptionsFromChatConfig`. Config conversion and effort injection cannot pick different option types because one function owns both.
12641264

1265-
`TransportFor` has no Azure case, because chatd never builds an azure-typed provider as a fantasy azure client: `fantasyConfigForAIBridge` maps openai and azure to the OpenAI client, because Azure OpenAI serves the OpenAI API, and folds every other provider type except anthropic and bedrock into openai-compat, which always speaks Chat Completions. A bedrock model's client depends on the model ID: `anthropic.*` bedrock models fold to the Anthropic Messages client, and other bedrock models fold to the OpenAI client.
1266-
1267-
For bedrock and azure, the provider type, not the model name, decides an unset `use_responses_api`: `newModel` fills it in before it builds the client. Bedrock serves OpenAI-format models only through the Responses API, so it defaults to true. An Azure model name is a deployment name that does not identify the model, so azure defaults to false, because every Azure chat deployment supports Chat Completions.
1265+
`TransportFor` has no Azure case, 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.
12681266

12691267
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.
12701268

‎coderd/x/chatd/chatd_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7687,7 +7687,7 @@ func TestActiveServer_AnthropicUsageMatchesFinalDelta(t *testing.T) {
76877687
require.Equal(t, database.ChatMessageRoleAssistant, last.Role)
76887688
require.Equal(t, sql.NullInt64{Int64: 200, Valid: true}, last.InputTokens)
76897689
require.Equal(t, sql.NullInt64{Int64: 75, Valid: true}, last.OutputTokens)
7690-
require.Equal(t, sql.NullInt64{Int64: 275, Valid: true}, last.TotalTokens)
7690+
require.Equal(t, sql.NullInt64{Int64: 455, Valid: true}, last.TotalTokens)
76917691
require.Equal(t, sql.NullInt64{Int64: 30, Valid: true}, last.CacheCreationTokens)
76927692
require.Equal(t, sql.NullInt64{Int64: 150, Valid: true}, last.CacheReadTokens)
76937693
}
@@ -7728,7 +7728,7 @@ func TestActiveServer_AnthropicPersistsPerRequestUsage(t *testing.T) {
77287728
require.Equal(t, sql.NullInt64{Int64: 7770, Valid: true}, last.CacheCreationTokens)
77297729
require.Equal(t, sql.NullInt64{Int64: 2, Valid: true}, last.InputTokens)
77307730
require.Equal(t, sql.NullInt64{Int64: 4996, Valid: true}, last.OutputTokens)
7731-
require.Equal(t, sql.NullInt64{Int64: 4998, Valid: true}, last.TotalTokens)
7731+
require.Equal(t, sql.NullInt64{Int64: 152724, Valid: true}, last.TotalTokens)
77327732
}
77337733

77347734
func TestActiveServer_ChatTurnDebugRunRecordsStreamStep(t *testing.T) {

‎coderd/x/chatd/chatdebug/model_coverage_internal_test.go‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,19 @@ func TestNormalizationFieldCoverage(t *testing.T) {
109109
name: "fantasy.CallWarning → normalizedWarning",
110110
typ: reflect.TypeFor[fantasy.CallWarning](),
111111
fields: fieldDisposition{
112-
"Type": "normalized",
113-
"Setting": "normalized",
114-
"Tool": "skipped: interface value, warning message+type sufficient for debug panel",
115-
"Details": "normalized",
116-
"Message": "normalized",
112+
"Type": "normalized",
113+
"Setting": "normalized",
114+
"Tool": "skipped: interface value, warning message+type sufficient for debug panel",
115+
"Details": "normalized",
116+
"Message": "normalized",
117+
"RawFinishReason": "skipped: warning message sufficient for debug panel",
117118
},
118119
},
119120
{
120121
name: "fantasy.StreamPart → appendNormalizedStreamContent",
121122
typ: reflect.TypeFor[fantasy.StreamPart](),
122123
fields: fieldDisposition{
124+
"ReceivedAt": "skipped: receipt time, not content",
123125
"Type": "normalized",
124126
"ID": "normalized: as ToolCallID in content parts",
125127
"ToolCallName": "normalized: as ToolName in content parts",

‎coderd/x/chatd/chatopenai/reasoning_mode.go‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,19 @@ func ReasoningMode(config *codersdk.ChatModelCallConfig) *string {
1414
return config.ProviderOptions.OpenAI.ReasoningMode
1515
}
1616

17-
// ValidateReasoningMode checks an explicit mode against the configured AI
18-
// provider type and the OpenAI overrides it cannot coexist with. The provider
19-
// type is the vendor, not the client: Azure and Bedrock share the OpenAI
20-
// client but do not accept the mode. Model support is not checked here:
17+
// ValidateReasoningMode checks an explicit mode against the provider and the
18+
// OpenAI overrides it cannot coexist with. Model support is not checked here:
2119
// OpenAI rejects the request when the model lacks the mode, and a model
2220
// released after this code still works.
23-
func ValidateReasoningMode(providerType codersdk.AIProviderType, config *codersdk.ChatModelCallConfig) error {
21+
func ValidateReasoningMode(provider string, config *codersdk.ChatModelCallConfig) error {
2422
mode := ReasoningMode(config)
2523
if mode == nil {
2624
return nil
2725
}
2826
if *mode != "pro" {
2927
return xerrors.New("provider_options.openai.reasoning_mode must be pro")
3028
}
31-
if providerType != codersdk.AIProviderTypeOpenAI {
29+
if provider != "openai" {
3230
return xerrors.New("provider_options.openai.reasoning_mode requires an OpenAI provider")
3331
}
3432
if openAIConfig := config.OpenAIConfig; openAIConfig != nil {

‎coderd/x/chatd/chatopenai/reasoning_mode_test.go‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,38 @@ func TestValidateReasoningMode(t *testing.T) {
1919
}
2020
t.Run("Unset", func(t *testing.T) {
2121
t.Parallel()
22-
require.NoError(t, chatopenai.ValidateReasoningMode(codersdk.AIProviderTypeOpenAI, nil))
23-
require.NoError(t, chatopenai.ValidateReasoningMode(codersdk.AIProviderTypeAzure, &codersdk.ChatModelCallConfig{}))
24-
require.NoError(t, chatopenai.ValidateReasoningMode(codersdk.AIProviderTypeAnthropic, &codersdk.ChatModelCallConfig{
22+
require.NoError(t, chatopenai.ValidateReasoningMode("openai", nil))
23+
require.NoError(t, chatopenai.ValidateReasoningMode("azure", &codersdk.ChatModelCallConfig{}))
24+
require.NoError(t, chatopenai.ValidateReasoningMode("anthropic", &codersdk.ChatModelCallConfig{
2525
ProviderOptions: &codersdk.ChatModelProviderOptions{OpenAI: &codersdk.ChatModelOpenAIProviderOptions{}},
2626
}))
2727
})
2828
t.Run("Pro", func(t *testing.T) {
2929
t.Parallel()
3030
// Model support is left to OpenAI so unreleased models need no code change.
31-
require.NoError(t, chatopenai.ValidateReasoningMode(codersdk.AIProviderTypeOpenAI, proConfig(nil)))
32-
require.NoError(t, chatopenai.ValidateReasoningMode(codersdk.AIProviderTypeOpenAI, proConfig(&codersdk.ChatModelOpenAIConfig{UseResponsesAPI: new(true), ReasoningModel: new(true)})))
31+
require.NoError(t, chatopenai.ValidateReasoningMode("openai", proConfig(nil)))
32+
require.NoError(t, chatopenai.ValidateReasoningMode("openai", proConfig(&codersdk.ChatModelOpenAIConfig{UseResponsesAPI: new(true), ReasoningModel: new(true)})))
3333
})
3434
for _, mode := range []string{"standard", "", "PRO", " pro ", "turbo"} {
3535
t.Run("Mode="+mode, func(t *testing.T) {
3636
t.Parallel()
3737
config := proConfig(nil)
3838
*config.ProviderOptions.OpenAI.ReasoningMode = mode
39-
require.ErrorContains(t, chatopenai.ValidateReasoningMode(codersdk.AIProviderTypeOpenAI, config), "must be pro")
39+
require.ErrorContains(t, chatopenai.ValidateReasoningMode("openai", config), "must be pro")
4040
})
4141
}
42-
for _, providerType := range []codersdk.AIProviderType{codersdk.AIProviderTypeAzure, codersdk.AIProviderTypeAnthropic, codersdk.AIProviderTypeOpenAICompat, codersdk.AIProviderTypeBedrock, ""} {
43-
t.Run("Provider="+string(providerType), func(t *testing.T) {
42+
for _, provider := range []string{"azure", "anthropic", "openai-compat", "bedrock", ""} {
43+
t.Run("Provider="+provider, func(t *testing.T) {
4444
t.Parallel()
45-
require.ErrorContains(t, chatopenai.ValidateReasoningMode(providerType, proConfig(nil)), "requires an OpenAI provider")
45+
require.ErrorContains(t, chatopenai.ValidateReasoningMode(provider, proConfig(nil)), "requires an OpenAI provider")
4646
})
4747
}
4848
t.Run("ForcedSamplingModel", func(t *testing.T) {
4949
t.Parallel()
50-
require.ErrorContains(t, chatopenai.ValidateReasoningMode(codersdk.AIProviderTypeOpenAI, proConfig(&codersdk.ChatModelOpenAIConfig{ReasoningModel: new(false)})), "requires a reasoning model")
50+
require.ErrorContains(t, chatopenai.ValidateReasoningMode("openai", proConfig(&codersdk.ChatModelOpenAIConfig{ReasoningModel: new(false)})), "requires a reasoning model")
5151
})
5252
t.Run("ForcedChatCompletions", func(t *testing.T) {
5353
t.Parallel()
54-
require.ErrorContains(t, chatopenai.ValidateReasoningMode(codersdk.AIProviderTypeOpenAI, proConfig(&codersdk.ChatModelOpenAIConfig{UseResponsesAPI: new(false)})), "requires the Responses API")
54+
require.ErrorContains(t, chatopenai.ValidateReasoningMode("openai", proConfig(&codersdk.ChatModelOpenAIConfig{UseResponsesAPI: new(false)})), "requires the Responses API")
5555
})
5656
}

‎coderd/x/chatd/chatprovider/chatprovider.go‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,10 @@ var mediaByRoute = map[mediaRoute]routeMedia{
143143
{provider: database.AIProviderTypeOpenai, transport: fantasyopenai.Name}: {
144144
images: commonImageTypes, pdf: true, audio: true,
145145
},
146-
// Azure takes the Responses API only when a model config forces it, and
147-
// its PDF support there is not verified.
148-
{provider: database.AIProviderTypeAzure, transport: fantasyopenai.Name, responses: true}: {
149-
images: commonImageTypes,
150-
},
151146
// Azure Chat Completions rejects file content parts with HTTP 400, and
152147
// Gemini's OpenAI-compatible endpoint does not process them, so neither
153148
// route takes PDFs.
154-
{provider: database.AIProviderTypeAzure, transport: fantasyopenai.Name}: {
149+
{provider: database.AIProviderTypeAzure, transport: fantasyopenaicompat.Name}: {
155150
images: commonImageTypes, audio: true,
156151
},
157152
{provider: database.AIProviderTypeGoogle, transport: fantasyopenaicompat.Name}: {
@@ -563,7 +558,7 @@ func bedrockIsAnthropicModel(model string) bool {
563558
// ClientProvider returns the fantasy provider whose client chatd builds for a
564559
// model on an AI provider of the given type. Bedrock serves Claude models in
565560
// the Anthropic Messages format and other models through the OpenAI Responses
566-
// API. Azure OpenAI serves the OpenAI API, so it shares the OpenAI client.
561+
// API.
567562
func ClientProvider(providerType database.AIProviderType, model string) string {
568563
switch providerType {
569564
case database.AIProviderTypeAnthropic:
@@ -575,7 +570,7 @@ func ClientProvider(providerType database.AIProviderType, model string) string {
575570
return fantasyanthropic.Name
576571
}
577572
return fantasyopenai.Name
578-
case database.AIProviderTypeOpenai, database.AIProviderTypeAzure:
573+
case database.AIProviderTypeOpenai:
579574
return fantasyopenai.Name
580575
default:
581576
return fantasyopenaicompat.Name
@@ -776,9 +771,6 @@ func effectiveOpenAIConfig(callConfig *codersdk.ChatModelCallConfig) *codersdk.C
776771
// HTTP headers on every request. httpClient, when non-nil, is used for
777772
// all provider HTTP requests. callConfig carries the model's client-scoped
778773
// OpenAI settings applied here: the transport override and reasoning mode.
779-
// Callers must validate the reasoning mode against the configured AI provider
780-
// type with chatopenai.ValidateReasoningMode, because providerHint names the
781-
// client, and Azure and Bedrock share the OpenAI client.
782774
func ModelFromConfig(
783775
providerHint string,
784776
modelName string,
@@ -793,6 +785,9 @@ func ModelFromConfig(
793785
return Model{}, err
794786
}
795787

788+
if err := chatopenai.ValidateReasoningMode(provider, callConfig); err != nil {
789+
return Model{}, err
790+
}
796791
openAIConfig := effectiveOpenAIConfig(callConfig)
797792

798793
apiKey := providerKeys.APIKey(provider)
@@ -862,6 +857,12 @@ func ModelFromConfig(
862857
if httpClient != nil {
863858
options = append(options, fantasyopenaicompat.WithHTTPClient(httpClient))
864859
}
860+
if openAIConfig != nil && openAIConfig.ReasoningModel != nil {
861+
reasoningModel := *openAIConfig.ReasoningModel
862+
options = append(options, fantasyopenaicompat.WithLanguageModelOptions(
863+
fantasyopenai.WithLanguageModelReasoningModelFunc(func(string) bool { return reasoningModel }),
864+
))
865+
}
865866
providerClient, err = fantasyopenaicompat.New(options...)
866867
default:
867868
return Model{}, xerrors.Errorf("unsupported model provider %q", provider)

‎coderd/x/chatd/chatprovider/file_parts_test.go‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,10 @@ func TestRouteMedia(t *testing.T) {
7272
rejects: []string{"text/plain"},
7373
},
7474
{
75-
name: "AzureChatCompletions", transport: "openai", provider: "azure", modelID: "gpt-6-luna", override: &forceCompletions,
75+
name: "Azure", transport: "openai-compat", provider: "azure",
7676
accepts: []string{"image/jpeg", "image/gif", "audio/mpeg"},
7777
rejects: []string{"image/heic", "image/bmp", "image/svg+xml", "application/pdf", "text/plain", "application/json"},
7878
},
79-
{
80-
name: "AzureForcedResponses", transport: "openai", provider: "azure", modelID: "gpt-6-luna", override: &forceResponses,
81-
accepts: []string{"image/png", "image/webp"},
82-
rejects: []string{"image/heic", "application/pdf", "text/plain", "audio/mpeg"},
83-
},
8479
{
8580
name: "Google", transport: "openai-compat", provider: "google",
8681
accepts: []string{"image/jpeg", "image/png", "image/webp", "image/heic", "image/heif", "audio/mpeg"},

‎coderd/x/chatd/chatprovider/responses_api_test.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,11 @@ func TestModelFromConfig_OpenAIReasoningModelOverride(t *testing.T) {
262262
override *bool
263263
wantReasoning bool
264264
}{
265-
{"ForceReasoningOnUnknownModel", "brand-new-model", new(true), true},
265+
{"ForceReasoningOnNonReasoningModel", "gpt-4o", new(true), true},
266266
{"ForceSamplingOnReasoningModel", "gpt-5", new(false), false},
267267
{"UnsetReasoningModel", "gpt-5", nil, true},
268-
{"UnsetUnknownModel", "brand-new-model", nil, false},
268+
{"UnsetUnknownModel", "brand-new-model", nil, true},
269+
{"UnsetNonReasoningModel", "gpt-4o", nil, false},
269270
{"UnsetGPT6Astra", "gpt-6-astra", nil, true},
270271
} {
271272
t.Run(tc.name, func(t *testing.T) {

‎coderd/x/chatd/chattest/anthropic_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func TestAnthropic_StreamingUsageIncludesCacheTokens(t *testing.T) {
111111
require.True(t, found)
112112
require.Equal(t, int64(200), finishPart.Usage.InputTokens)
113113
require.Equal(t, int64(75), finishPart.Usage.OutputTokens)
114-
require.Equal(t, int64(275), finishPart.Usage.TotalTokens)
114+
require.Equal(t, int64(455), finishPart.Usage.TotalTokens)
115115
require.Equal(t, int64(30), finishPart.Usage.CacheCreationTokens)
116116
require.Equal(t, int64(150), finishPart.Usage.CacheReadTokens)
117117
}

0 commit comments

Comments
 (0)