Skip to content

Commit 9283c15

Browse files
committed
fix(models): enforce OpenAI maxTokens/maxCompletionTokens mutual exclusivity and minimum
Add a type-level CEL validation rule rejecting ModelConfigs that set both maxTokens and maxCompletionTokens, and add the missing Minimum=1 marker to maxTokens so non-positive values are rejected at admission rather than silently ignored by the translator. Regenerate CRD bases and helm templates, and add an envtest-based CEL test covering both-set rejection, minimum enforcement, and single-field configs. Signed-off-by: anthonytwh <[email protected]>
1 parent 0b813ae commit 9283c15

4 files changed

Lines changed: 175 additions & 0 deletions

File tree

‎go/api/config/crd/bases/kagent.dev_modelconfigs.yaml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,13 +600,16 @@ spec:
600600
`max_completion_tokens` request parameter (an upper bound on visible
601601
output plus reasoning tokens). This is the parameter reasoning models
602602
(GPT-5 / o-series) require in place of the deprecated maxTokens.
603+
Mutually exclusive with maxTokens.
603604
minimum: 1
604605
type: integer
605606
maxTokens:
606607
description: |-
607608
Maximum tokens to generate. Sent as the OpenAI `max_tokens` request
608609
parameter, which is deprecated and rejected by reasoning models
609610
(GPT-5 / o-series). For those models set maxCompletionTokens instead.
611+
Mutually exclusive with maxCompletionTokens.
612+
minimum: 1
610613
type: integer
611614
"n":
612615
description: N value
@@ -663,6 +666,9 @@ spec:
663666
description: Top-p sampling parameter
664667
type: string
665668
type: object
669+
x-kubernetes-validations:
670+
- message: maxTokens and maxCompletionTokens are mutually exclusive
671+
rule: '!(has(self.maxTokens) && has(self.maxCompletionTokens))'
666672
provider:
667673
default: OpenAI
668674
description: The provider of the model
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
Copyright 2025.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha2
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
"github.com/stretchr/testify/require"
24+
corev1 "k8s.io/api/core/v1"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
"k8s.io/apimachinery/pkg/runtime"
27+
ctrl_client "sigs.k8s.io/controller-runtime/pkg/client"
28+
"sigs.k8s.io/controller-runtime/pkg/envtest"
29+
)
30+
31+
// TestOpenAIConfigCELValidation pins the OpenAIConfig admission rules against a
32+
// real kube-apiserver loaded with the shipped CRDs:
33+
// - maxTokens and maxCompletionTokens are mutually exclusive (type-level
34+
// XValidation), because native OpenAI reasoning models reject max_tokens
35+
// while some OpenAI-compatible endpoints reject max_completion_tokens, so
36+
// sending both risks hard 400s.
37+
// - both fields carry Minimum=1, so a non-positive value is rejected at
38+
// admission rather than silently ignored by the translator.
39+
func TestOpenAIConfigCELValidation(t *testing.T) {
40+
testEnv := &envtest.Environment{
41+
BinaryAssetsDirectory: envtestAssetsDir(t),
42+
CRDDirectoryPaths: []string{crdBasesDir(t)},
43+
ErrorIfCRDPathMissing: true,
44+
}
45+
cfg, err := testEnv.Start()
46+
require.NoError(t, err)
47+
t.Cleanup(func() { _ = testEnv.Stop() })
48+
49+
scheme := runtime.NewScheme()
50+
require.NoError(t, corev1.AddToScheme(scheme))
51+
require.NoError(t, AddToScheme(scheme))
52+
cl, err := ctrl_client.New(cfg, ctrl_client.Options{Scheme: scheme})
53+
require.NoError(t, err)
54+
55+
ctx := context.Background()
56+
const ns = "openai-cel"
57+
require.NoError(t, cl.Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: ns}}))
58+
59+
cases := []struct {
60+
name string
61+
build func() ctrl_client.Object
62+
wantReject string // substring in admission error; empty means accept
63+
}{
64+
{
65+
name: "both maxTokens and maxCompletionTokens rejected",
66+
build: func() ctrl_client.Object {
67+
return &ModelConfig{
68+
ObjectMeta: metav1.ObjectMeta{Name: "mc-both-token-caps", Namespace: ns},
69+
Spec: ModelConfigSpec{
70+
Model: "gpt-4",
71+
Provider: ModelProviderOpenAI,
72+
OpenAI: &OpenAIConfig{MaxTokens: 1000, MaxCompletionTokens: 1000},
73+
},
74+
}
75+
},
76+
wantReject: "maxTokens and maxCompletionTokens are mutually exclusive",
77+
},
78+
{
79+
name: "maxTokens below minimum rejected",
80+
build: func() ctrl_client.Object {
81+
return &ModelConfig{
82+
ObjectMeta: metav1.ObjectMeta{Name: "mc-maxtokens-neg", Namespace: ns},
83+
Spec: ModelConfigSpec{
84+
Model: "gpt-4",
85+
Provider: ModelProviderOpenAI,
86+
OpenAI: &OpenAIConfig{MaxTokens: -1},
87+
},
88+
}
89+
},
90+
wantReject: "maxTokens",
91+
},
92+
{
93+
name: "maxCompletionTokens below minimum rejected",
94+
build: func() ctrl_client.Object {
95+
return &ModelConfig{
96+
ObjectMeta: metav1.ObjectMeta{Name: "mc-maxcompletion-neg", Namespace: ns},
97+
Spec: ModelConfigSpec{
98+
Model: "gpt-4",
99+
Provider: ModelProviderOpenAI,
100+
OpenAI: &OpenAIConfig{MaxCompletionTokens: -1},
101+
},
102+
}
103+
},
104+
wantReject: "maxCompletionTokens",
105+
},
106+
{
107+
name: "only maxTokens accepted",
108+
build: func() ctrl_client.Object {
109+
return &ModelConfig{
110+
ObjectMeta: metav1.ObjectMeta{Name: "mc-maxtokens-only", Namespace: ns},
111+
Spec: ModelConfigSpec{
112+
Model: "gpt-4",
113+
Provider: ModelProviderOpenAI,
114+
OpenAI: &OpenAIConfig{MaxTokens: 1000},
115+
},
116+
}
117+
},
118+
},
119+
{
120+
name: "only maxCompletionTokens accepted",
121+
build: func() ctrl_client.Object {
122+
return &ModelConfig{
123+
ObjectMeta: metav1.ObjectMeta{Name: "mc-maxcompletion-only", Namespace: ns},
124+
Spec: ModelConfigSpec{
125+
Model: "gpt-5",
126+
Provider: ModelProviderOpenAI,
127+
OpenAI: &OpenAIConfig{MaxCompletionTokens: 1000},
128+
},
129+
}
130+
},
131+
},
132+
{
133+
name: "neither token cap accepted",
134+
build: func() ctrl_client.Object {
135+
return &ModelConfig{
136+
ObjectMeta: metav1.ObjectMeta{Name: "mc-no-token-caps", Namespace: ns},
137+
Spec: ModelConfigSpec{
138+
Model: "gpt-4",
139+
Provider: ModelProviderOpenAI,
140+
OpenAI: &OpenAIConfig{},
141+
},
142+
}
143+
},
144+
},
145+
}
146+
147+
for _, c := range cases {
148+
t.Run(c.name, func(t *testing.T) {
149+
err := cl.Create(ctx, c.build())
150+
if c.wantReject == "" {
151+
require.NoError(t, err)
152+
return
153+
}
154+
require.Error(t, err)
155+
require.Contains(t, err.Error(), c.wantReject)
156+
})
157+
}
158+
}

‎go/api/v1alpha2/modelconfig_types.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ type TokenExchangeConfig struct {
138138
}
139139

140140
// OpenAIConfig contains OpenAI-specific configuration options
141+
//
142+
// +kubebuilder:validation:XValidation:message="maxTokens and maxCompletionTokens are mutually exclusive",rule="!(has(self.maxTokens) && has(self.maxCompletionTokens))"
141143
type OpenAIConfig struct {
142144
// Base URL for the OpenAI API (overrides default)
143145
// +optional
@@ -154,13 +156,16 @@ type OpenAIConfig struct {
154156
// Maximum tokens to generate. Sent as the OpenAI `max_tokens` request
155157
// parameter, which is deprecated and rejected by reasoning models
156158
// (GPT-5 / o-series). For those models set maxCompletionTokens instead.
159+
// Mutually exclusive with maxCompletionTokens.
157160
// +optional
161+
// +kubebuilder:validation:Minimum=1
158162
MaxTokens int `json:"maxTokens,omitempty"`
159163

160164
// Maximum completion tokens to generate. Sent as the OpenAI
161165
// `max_completion_tokens` request parameter (an upper bound on visible
162166
// output plus reasoning tokens). This is the parameter reasoning models
163167
// (GPT-5 / o-series) require in place of the deprecated maxTokens.
168+
// Mutually exclusive with maxTokens.
164169
// +optional
165170
// +kubebuilder:validation:Minimum=1
166171
MaxCompletionTokens int `json:"maxCompletionTokens,omitempty"`

‎helm/kagent-crds/templates/kagent.dev_modelconfigs.yaml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,13 +600,16 @@ spec:
600600
`max_completion_tokens` request parameter (an upper bound on visible
601601
output plus reasoning tokens). This is the parameter reasoning models
602602
(GPT-5 / o-series) require in place of the deprecated maxTokens.
603+
Mutually exclusive with maxTokens.
603604
minimum: 1
604605
type: integer
605606
maxTokens:
606607
description: |-
607608
Maximum tokens to generate. Sent as the OpenAI `max_tokens` request
608609
parameter, which is deprecated and rejected by reasoning models
609610
(GPT-5 / o-series). For those models set maxCompletionTokens instead.
611+
Mutually exclusive with maxCompletionTokens.
612+
minimum: 1
610613
type: integer
611614
"n":
612615
description: N value
@@ -663,6 +666,9 @@ spec:
663666
description: Top-p sampling parameter
664667
type: string
665668
type: object
669+
x-kubernetes-validations:
670+
- message: maxTokens and maxCompletionTokens are mutually exclusive
671+
rule: '!(has(self.maxTokens) && has(self.maxCompletionTokens))'
666672
provider:
667673
default: OpenAI
668674
description: The provider of the model

0 commit comments

Comments
 (0)