api: add omitempty to required tool function parameter type (#12989)

This commit is contained in:
Jeffrey Morgan
2025-11-06 14:08:55 -08:00
committed by GitHub
parent 73a0cafc1e
commit 30fcc71983
6 changed files with 45 additions and 37 deletions

View File

@@ -323,7 +323,7 @@ type ToolFunctionParameters struct {
Type string `json:"type"` Type string `json:"type"`
Defs any `json:"$defs,omitempty"` Defs any `json:"$defs,omitempty"`
Items any `json:"items,omitempty"` Items any `json:"items,omitempty"`
Required []string `json:"required"` Required []string `json:"required,omitempty"`
Properties map[string]ToolProperty `json:"properties"` Properties map[string]ToolProperty `json:"properties"`
} }

View File

@@ -298,6 +298,44 @@ 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) { func TestToolCallFunction_IndexAlwaysMarshals(t *testing.T) {
fn := ToolCallFunction{ fn := ToolCallFunction{
Name: "echo", Name: "echo",

View File

@@ -484,13 +484,7 @@ func TestChatMiddleware(t *testing.T) {
Function: api.ToolFunction{ Function: api.ToolFunction{
Name: "get_weather", Name: "get_weather",
Description: "Get the current weather", Description: "Get the current weather",
Parameters: struct { Parameters: api.ToolFunctionParameters{
Type string `json:"type"`
Defs any `json:"$defs,omitempty"`
Items any `json:"items,omitempty"`
Required []string `json:"required"`
Properties map[string]api.ToolProperty `json:"properties"`
}{
Type: "object", Type: "object",
Required: []string{"location"}, Required: []string{"location"},
Properties: map[string]api.ToolProperty{ Properties: map[string]api.ToolProperty{

View File

@@ -363,7 +363,7 @@ func TestChatDebugRenderOnly(t *testing.T) {
DebugRenderOnly: true, DebugRenderOnly: true,
}, },
expectDebug: true, expectDebug: true,
expectTemplate: "[{\"type\":\"function\",\"function\":{\"name\":\"get_weather\",\"description\":\"Get weather information\",\"parameters\":{\"type\":\"\",\"required\":null,\"properties\":null}}}]user: Get the weather\n", expectTemplate: "[{\"type\":\"function\",\"function\":{\"name\":\"get_weather\",\"description\":\"Get weather information\",\"parameters\":{\"type\":\"\",\"properties\":null}}}]user: Get the weather\n",
}, },
} }

View File

@@ -485,13 +485,7 @@ func TestGenerateChat(t *testing.T) {
Function: api.ToolFunction{ Function: api.ToolFunction{
Name: "get_weather", Name: "get_weather",
Description: "Get the current weather", Description: "Get the current weather",
Parameters: struct { Parameters: api.ToolFunctionParameters{
Type string `json:"type"`
Defs any `json:"$defs,omitempty"`
Items any `json:"items,omitempty"`
Required []string `json:"required"`
Properties map[string]api.ToolProperty `json:"properties"`
}{
Type: "object", Type: "object",
Required: []string{"location"}, Required: []string{"location"},
Properties: map[string]api.ToolProperty{ Properties: map[string]api.ToolProperty{
@@ -585,13 +579,7 @@ func TestGenerateChat(t *testing.T) {
Function: api.ToolFunction{ Function: api.ToolFunction{
Name: "get_weather", Name: "get_weather",
Description: "Get the current weather", Description: "Get the current weather",
Parameters: struct { Parameters: api.ToolFunctionParameters{
Type string `json:"type"`
Defs any `json:"$defs,omitempty"`
Items any `json:"items,omitempty"`
Required []string `json:"required"`
Properties map[string]api.ToolProperty `json:"properties"`
}{
Type: "object", Type: "object",
Required: []string{"location"}, Required: []string{"location"},
Properties: map[string]api.ToolProperty{ Properties: map[string]api.ToolProperty{

View File

@@ -26,13 +26,7 @@ func getTestTools() []api.Tool {
Function: api.ToolFunction{ Function: api.ToolFunction{
Name: "get_weather", Name: "get_weather",
Description: "Get the current weather in a given location", Description: "Get the current weather in a given location",
Parameters: struct { Parameters: api.ToolFunctionParameters{
Type string `json:"type"`
Defs any `json:"$defs,omitempty"`
Items any `json:"items,omitempty"`
Required []string `json:"required"`
Properties map[string]api.ToolProperty `json:"properties"`
}{
Type: "object", Type: "object",
Required: []string{"location"}, Required: []string{"location"},
Properties: map[string]api.ToolProperty{ Properties: map[string]api.ToolProperty{
@@ -49,13 +43,7 @@ func getTestTools() []api.Tool {
Function: api.ToolFunction{ Function: api.ToolFunction{
Name: "calculate", Name: "calculate",
Description: "Calculate a mathematical expression", Description: "Calculate a mathematical expression",
Parameters: struct { Parameters: api.ToolFunctionParameters{
Type string `json:"type"`
Defs any `json:"$defs,omitempty"`
Items any `json:"items,omitempty"`
Required []string `json:"required"`
Properties map[string]api.ToolProperty `json:"properties"`
}{
Type: "object", Type: "object",
Required: []string{"expression"}, Required: []string{"expression"},
Properties: map[string]api.ToolProperty{ Properties: map[string]api.ToolProperty{