Skip to content

Commit eb7a382

Browse files
behzadspRosenberg96
authored andcommitted
add GPT-5 model constants and reasoning validation (sashabaranov#1062)
1 parent 14fffa9 commit eb7a382

File tree

4 files changed

+162
-4
lines changed

4 files changed

+162
-4
lines changed

chat_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,126 @@ func TestO3ModelsChatCompletionsBetaLimitations(t *testing.T) {
334334
}
335335
}
336336

337+
func TestGPT5ModelsChatCompletionsBetaLimitations(t *testing.T) {
338+
tests := []struct {
339+
name string
340+
in openai.ChatCompletionRequest
341+
expectedError error
342+
}{
343+
{
344+
name: "log_probs_unsupported",
345+
in: openai.ChatCompletionRequest{
346+
MaxCompletionTokens: 1000,
347+
LogProbs: true,
348+
Model: openai.GPT5,
349+
},
350+
expectedError: openai.ErrReasoningModelLimitationsLogprobs,
351+
},
352+
{
353+
name: "set_temperature_unsupported",
354+
in: openai.ChatCompletionRequest{
355+
MaxCompletionTokens: 1000,
356+
Model: openai.GPT5Mini,
357+
Messages: []openai.ChatCompletionMessage{
358+
{
359+
Role: openai.ChatMessageRoleUser,
360+
},
361+
{
362+
Role: openai.ChatMessageRoleAssistant,
363+
},
364+
},
365+
Temperature: float32(2),
366+
},
367+
expectedError: openai.ErrReasoningModelLimitationsOther,
368+
},
369+
{
370+
name: "set_top_unsupported",
371+
in: openai.ChatCompletionRequest{
372+
MaxCompletionTokens: 1000,
373+
Model: openai.GPT5Nano,
374+
Messages: []openai.ChatCompletionMessage{
375+
{
376+
Role: openai.ChatMessageRoleUser,
377+
},
378+
{
379+
Role: openai.ChatMessageRoleAssistant,
380+
},
381+
},
382+
Temperature: float32(1),
383+
TopP: float32(0.1),
384+
},
385+
expectedError: openai.ErrReasoningModelLimitationsOther,
386+
},
387+
{
388+
name: "set_n_unsupported",
389+
in: openai.ChatCompletionRequest{
390+
MaxCompletionTokens: 1000,
391+
Model: openai.GPT5ChatLatest,
392+
Messages: []openai.ChatCompletionMessage{
393+
{
394+
Role: openai.ChatMessageRoleUser,
395+
},
396+
{
397+
Role: openai.ChatMessageRoleAssistant,
398+
},
399+
},
400+
Temperature: float32(1),
401+
TopP: float32(1),
402+
N: 2,
403+
},
404+
expectedError: openai.ErrReasoningModelLimitationsOther,
405+
},
406+
{
407+
name: "set_presence_penalty_unsupported",
408+
in: openai.ChatCompletionRequest{
409+
MaxCompletionTokens: 1000,
410+
Model: openai.GPT5,
411+
Messages: []openai.ChatCompletionMessage{
412+
{
413+
Role: openai.ChatMessageRoleUser,
414+
},
415+
{
416+
Role: openai.ChatMessageRoleAssistant,
417+
},
418+
},
419+
PresencePenalty: float32(0.1),
420+
},
421+
expectedError: openai.ErrReasoningModelLimitationsOther,
422+
},
423+
{
424+
name: "set_frequency_penalty_unsupported",
425+
in: openai.ChatCompletionRequest{
426+
MaxCompletionTokens: 1000,
427+
Model: openai.GPT5Mini,
428+
Messages: []openai.ChatCompletionMessage{
429+
{
430+
Role: openai.ChatMessageRoleUser,
431+
},
432+
{
433+
Role: openai.ChatMessageRoleAssistant,
434+
},
435+
},
436+
FrequencyPenalty: float32(0.1),
437+
},
438+
expectedError: openai.ErrReasoningModelLimitationsOther,
439+
},
440+
}
441+
442+
for _, tt := range tests {
443+
t.Run(tt.name, func(t *testing.T) {
444+
config := openai.DefaultConfig("whatever")
445+
config.BaseURL = "http://localhost/v1"
446+
client := openai.NewClientWithConfig(config)
447+
ctx := context.Background()
448+
449+
_, err := client.CreateChatCompletion(ctx, tt.in)
450+
checks.HasError(t, err)
451+
msg := fmt.Sprintf("CreateChatCompletion should return wrong model error, returned: %s", err)
452+
checks.ErrorIs(t, err, tt.expectedError, msg)
453+
})
454+
}
455+
}
456+
337457
func TestChatRequestOmitEmpty(t *testing.T) {
338458
data, err := json.Marshal(openai.ChatCompletionRequest{
339459
// We set model b/c it's required, so omitempty doesn't make sense

completion.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ const (
4949
GPT4Dot1Nano20250414 = "gpt-4.1-nano-2025-04-14"
5050
GPT4Dot5Preview = "gpt-4.5-preview"
5151
GPT4Dot5Preview20250227 = "gpt-4.5-preview-2025-02-27"
52+
GPT5 = "gpt-5"
53+
GPT5Mini = "gpt-5-mini"
54+
GPT5Nano = "gpt-5-nano"
55+
GPT5ChatLatest = "gpt-5-chat-latest"
5256
GPT3Dot5Turbo0125 = "gpt-3.5-turbo-0125"
5357
GPT3Dot5Turbo1106 = "gpt-3.5-turbo-1106"
5458
GPT3Dot5Turbo0613 = "gpt-3.5-turbo-0613"
@@ -142,6 +146,10 @@ var disabledModelsForEndpoints = map[string]map[string]bool{
142146
GPT4Dot1Mini20250414: true,
143147
GPT4Dot1Nano: true,
144148
GPT4Dot1Nano20250414: true,
149+
GPT5: true,
150+
GPT5Mini: true,
151+
GPT5Nano: true,
152+
GPT5ChatLatest: true,
145153
},
146154
chatCompletionsSuffix: {
147155
CodexCodeDavinci002: true,

completion_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,32 @@ func TestCompletionWithGPT4oModels(t *testing.T) {
300300
})
301301
}
302302
}
303+
304+
// TestCompletionWithGPT5Models Tests that GPT5 models are not supported for completion endpoint.
305+
func TestCompletionWithGPT5Models(t *testing.T) {
306+
config := openai.DefaultConfig("whatever")
307+
config.BaseURL = "http://localhost/v1"
308+
client := openai.NewClientWithConfig(config)
309+
310+
models := []string{
311+
openai.GPT5,
312+
openai.GPT5Mini,
313+
openai.GPT5Nano,
314+
openai.GPT5ChatLatest,
315+
}
316+
317+
for _, model := range models {
318+
t.Run(model, func(t *testing.T) {
319+
_, err := client.CreateCompletion(
320+
context.Background(),
321+
openai.CompletionRequest{
322+
MaxTokens: 5,
323+
Model: model,
324+
},
325+
)
326+
if !errors.Is(err, openai.ErrCompletionUnsupportedModel) {
327+
t.Fatalf("CreateCompletion should return ErrCompletionUnsupportedModel for %s model, but returned: %v", model, err)
328+
}
329+
})
330+
}
331+
}

reasoning_validator.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,22 @@ var (
2828
ErrReasoningModelLimitationsOther = errors.New("this model has beta-limitations, temperature, top_p and n are fixed at 1, while presence_penalty and frequency_penalty are fixed at 0") //nolint:lll
2929
)
3030

31-
// ReasoningValidator handles validation for o-series model requests.
31+
// ReasoningValidator handles validation for reasoning model requests.
3232
type ReasoningValidator struct{}
3333

34-
// NewReasoningValidator creates a new validator for o-series models.
34+
// NewReasoningValidator creates a new validator for reasoning models.
3535
func NewReasoningValidator() *ReasoningValidator {
3636
return &ReasoningValidator{}
3737
}
3838

39-
// Validate performs all validation checks for o-series models.
39+
// Validate performs all validation checks for reasoning models.
4040
func (v *ReasoningValidator) Validate(request ChatCompletionRequest) error {
4141
o1Series := strings.HasPrefix(request.Model, "o1")
4242
o3Series := strings.HasPrefix(request.Model, "o3")
4343
o4Series := strings.HasPrefix(request.Model, "o4")
44+
gpt5Series := strings.HasPrefix(request.Model, "gpt-5")
4445

45-
if !o1Series && !o3Series && !o4Series {
46+
if !o1Series && !o3Series && !o4Series && !gpt5Series {
4647
return nil
4748
}
4849

0 commit comments

Comments
 (0)