Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions bridge_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2175,7 +2175,10 @@ func newMockServer(ctx context.Context, t *testing.T, files archiveFileMap, requ
}

// validateOpenAIChatCompletionRequest validates that an OpenAI chat completion request
// has all required fields. Returns an error if validation fails.
// has all required fields.
// According to OpenAI documentation https://platform.openai.com/docs/api-reference/chat/create,
// the "model" and "messages" fields are required.
// Returns an error if validation fails.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probably misunderstood something but his comment says that:

the "model" and "messages" fields are required.

While below for responses:

no fields are strictly required.

but the code here doesn't check this while in responses it does.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, so according to the OpenAI API documentation on chat creation model and messages are required fields. But according to the OpenAI API documentation on responses creation no fields are required, but AFAIK both the model and input fields are generally set. The purpose of these functions is to validate that AIBridge is sending valid request bodies

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but the code here doesn't check this while in responses it does.

Ah nvm it is checked just github collapses code without changes...

func validateOpenAIChatCompletionRequest(body []byte) error {
var req openai.ChatCompletionNewParams
if err := json.Unmarshal(body, &req); err != nil {
Expand All @@ -2198,24 +2201,43 @@ func validateOpenAIChatCompletionRequest(body []byte) error {
}

// validateOpenAIResponsesRequest validates that an OpenAI responses request
// has all required fields. Returns an error if validation fails.
// has all required fields.
// According to OpenAI documentation https://platform.openai.com/docs/api-reference/responses/create,
// no fields are strictly required. However, we check "model" and "input" fields
// as these should usually be set in request bodies.
// Returns an error if validation fails.
func validateOpenAIResponsesRequest(body []byte) error {
var reqBody map[string]any
if err := json.Unmarshal(body, &reqBody); err != nil {
return fmt.Errorf("request should be valid JSON: %w", err)
return fmt.Errorf("request should unmarshal into valid JSON: %w", err)
}

// Verify required fields for OpenAI responses
// Note: Using map here since there's no specific SDK type for responses endpoint
model, ok := reqBody["model"]
if !ok || model == "" {
return fmt.Errorf("model field is required but missing or empty")
// Collect all validation errors
var errs []string

// Validate model field exists
model, hasModel := reqBody["model"].(string)
if !hasModel || model == "" {
errs = append(errs, "model field is required but empty or missing")
}

// Validate input field exists
if _, hasInput := reqBody["input"]; !hasInput {
errs = append(errs, "input field is required but missing")
}

if len(errs) > 0 {
return fmt.Errorf("validation failed: %s", strings.Join(errs, "; "))
}
return nil
}

// validateAnthropicMessagesRequest validates that an Anthropic messages request
// has all required fields. Returns an error if validation fails.
// has all required fields.
// According to the Anthropic Go SDK https://github.com/anthropics/anthropic-sdk-go,
// the "model", "messages", and "max_tokens" fields are required, as indicated by
// the `required` struct tags in MessageNewParams.
// Returns an error if validation fails.
func validateAnthropicMessagesRequest(body []byte) error {
var req anthropic.MessageNewParams
if err := json.Unmarshal(body, &req); err != nil {
Expand Down