diff --git a/api/types.go b/api/types.go index 4f2fb9c93..1615fce6f 100644 --- a/api/types.go +++ b/api/types.go @@ -323,7 +323,7 @@ type ToolFunctionParameters struct { Type string `json:"type"` Defs any `json:"$defs,omitempty"` Items any `json:"items,omitempty"` - Required []string `json:"required"` + Required []string `json:"required,omitempty"` Properties map[string]ToolProperty `json:"properties"` } diff --git a/api/types_test.go b/api/types_test.go index 5053c1621..a4e6e9e54 100644 --- a/api/types_test.go +++ b/api/types_test.go @@ -298,6 +298,43 @@ func TestToolFunction_UnmarshalJSON(t *testing.T) { } } +func TestToolFunctionParameters_MarshalJSON(t *testing.T) { + tests := []struct { + name string + input ToolFunctionParameters + expected string + }{ + { + name: "simple object with string property", + input: ToolFunctionParameters{ + Type: "object", + Required: []string{"name"}, + Properties: map[string]ToolProperty{ + "name": {Type: PropertyType{"string"}}, + }, + }, + expected: `{"type":"object","required":["name"],"properties":{"name":{"type":"string"}}}`, + }, + { + name: "no required", + input: ToolFunctionParameters{ + Type: "object", + Properties: map[string]ToolProperty{ + "name": {Type: PropertyType{"string"}}, + }, + }, + expected: `{"type":"object","properties":{"name":{"type":"string"}}}`, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + data, err := json.Marshal(test.input) + require.NoError(t, err) + assert.Equal(t, test.expected, string(data)) + }) + } +} func TestToolCallFunction_IndexAlwaysMarshals(t *testing.T) { fn := ToolCallFunction{ Name: "echo",