Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
Ettore Di Giacinto
2026-01-04 17:40:48 +00:00
parent a95422f4d1
commit e0e904ff98

View File

@@ -110,43 +110,60 @@ func SanitizeTools(tools Tools) Tools {
return tools
}
xlog.Debug("SanitizeTools: processing tools", "count", len(tools))
sanitized := make(Tools, 0, len(tools))
for _, tool := range tools {
// Create a copy of the tool to avoid modifying the original
sanitizedTool := Tool{
Type: tool.Type,
Function: tool.Function,
Type: tool.Type,
Function: Function{
Name: tool.Function.Name,
Description: tool.Function.Description,
Strict: tool.Function.Strict,
},
}
// Check if the tool has parameters
if sanitizedTool.Function.Parameters != nil {
// Check if parameters has a "properties" field
if properties, ok := sanitizedTool.Function.Parameters["properties"]; ok {
// Try to cast properties to a map
if propertiesMap, ok := properties.(map[string]interface{}); ok {
// Create a new map for sanitized properties
sanitizedProperties := make(map[string]interface{})
hasNullValues := false
// Deep copy and sanitize parameters
if tool.Function.Parameters != nil {
// Create a new Parameters map
sanitizedTool.Function.Parameters = make(map[string]interface{})
// Iterate through properties and remove null values
for key, value := range propertiesMap {
if value == nil {
// Convert null to empty object
sanitizedProperties[key] = map[string]interface{}{}
hasNullValues = true
xlog.Warn("Found null value in tool parameter properties, converting to empty object",
"tool", sanitizedTool.Function.Name,
"parameter", key)
} else {
// Preserve valid values
sanitizedProperties[key] = value
// Copy all parameters, sanitizing properties if present
for key, value := range tool.Function.Parameters {
if key == "properties" {
// Special handling for properties - sanitize null values
if propertiesMap, ok := value.(map[string]interface{}); ok {
// Create a new map for sanitized properties
sanitizedProperties := make(map[string]interface{})
// Iterate through properties and convert null values to empty objects
for propKey, propValue := range propertiesMap {
// Check for nil/null values (handles both Go nil and JSON null)
if propValue == nil {
// Convert null to empty object to prevent Jinja template errors
sanitizedProperties[propKey] = map[string]interface{}{}
xlog.Warn("Found null value in tool parameter properties, converting to empty object",
"tool", sanitizedTool.Function.Name,
"parameter", propKey)
} else {
// Check if value is a map/object - if so, ensure it's not null
if propValueMap, ok := propValue.(map[string]interface{}); ok {
// It's already a valid map, preserve it
sanitizedProperties[propKey] = propValueMap
} else {
// Preserve other valid values (strings, numbers, arrays, etc.)
sanitizedProperties[propKey] = propValue
}
}
}
}
// Update the properties if we found null values
if hasNullValues {
sanitizedTool.Function.Parameters["properties"] = sanitizedProperties
} else {
// If properties is not a map, preserve as-is
sanitizedTool.Function.Parameters[key] = value
}
} else {
// Copy other parameters as-is
sanitizedTool.Function.Parameters[key] = value
}
}
}