Skip to content

bug(go): tool null inputSchema handler to avoid validation error #2863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
36 changes: 36 additions & 0 deletions go/core/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,45 @@ func (a *ActionDef[In, Out, Stream]) Run(ctx context.Context, input In, cb Strea
})
}

// Handle inputSchema null from UI
func inputSchemaNullHandler(input []byte) ([]byte, error) {
if len(input) == 0 {
return []byte{}, nil
}
var rawInput any
if err := json.Unmarshal(input, &rawInput); err != nil {
return nil, NewError(INTERNAL, fmt.Sprintf("failed to unmarshal JSON: %v", err))
}

if rawInputMap, ok := rawInput.(map[string]interface{}); ok {
if tools, ok := rawInputMap["tools"].([]interface{}); ok {
for _, t := range tools {
if tool, ok := t.(map[string]interface{}); ok {
if inputSchema, exists := tool["inputSchema"]; exists && inputSchema == nil {
delete(tool, "inputSchema")
}
}
}
}
}
// Check if the "tools" key exists and if its value is a slice of interfaces.

output, err := json.Marshal(rawInput)
if err != nil {
return nil, NewError(INTERNAL, fmt.Sprintf("failed to marshal JSON: %v", err))
}
return output, nil
}

// RunJSON runs the action with a JSON input, and returns a JSON result.
func (a *ActionDef[In, Out, Stream]) RunJSON(ctx context.Context, input json.RawMessage, cb StreamCallback[json.RawMessage]) (json.RawMessage, error) {
// Validate input before unmarshaling it because invalid or unknown fields will be discarded in the process.
input, err := inputSchemaNullHandler(input)

if err != nil {
return nil, err
}

if err := base.ValidateJSON(input, a.inputSchema); err != nil {
return nil, NewError(INVALID_ARGUMENT, err.Error())
}
Expand Down
Loading