Skip to content

Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.

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

Jeffrey Morgan30fcc7198349
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..187012db2 100644
--- a/api/types_test.go
+++ b/api/types_test.go
@@ -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) {
fn := ToolCallFunction{
Name: "echo",
diff --git a/middleware/openai_test.go b/middleware/openai_test.go
index a78ee8b91..e5cb245b0 100644
--- a/middleware/openai_test.go
+++ b/middleware/openai_test.go
@@ -484,13 +484,7 @@ func TestChatMiddleware(t *testing.T) {
Function: api.ToolFunction{
Name: "get_weather",
Description: "Get the current weather",
- Parameters: struct {
- 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"`
- }{
+ Parameters: api.ToolFunctionParameters{
Type: "object",
Required: []string{"location"},
Properties: map[string]api.ToolProperty{
diff --git a/server/routes_debug_test.go b/server/routes_debug_test.go
index bf822c68b..6f9104c39 100644
--- a/server/routes_debug_test.go
+++ b/server/routes_debug_test.go
@@ -363,7 +363,7 @@ func TestChatDebugRenderOnly(t *testing.T) {
DebugRenderOnly: 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",
},
}
diff --git a/server/routes_generate_test.go b/server/routes_generate_test.go
index ecd1d2d63..ed5922f2d 100644
--- a/server/routes_generate_test.go
+++ b/server/routes_generate_test.go
@@ -485,13 +485,7 @@ func TestGenerateChat(t *testing.T) {
Function: api.ToolFunction{
Name: "get_weather",
Description: "Get the current weather",
- Parameters: struct {
- 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"`
- }{
+ Parameters: api.ToolFunctionParameters{
Type: "object",
Required: []string{"location"},
Properties: map[string]api.ToolProperty{
@@ -585,13 +579,7 @@ func TestGenerateChat(t *testing.T) {
Function: api.ToolFunction{
Name: "get_weather",
Description: "Get the current weather",
- Parameters: struct {
- 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"`
- }{
+ Parameters: api.ToolFunctionParameters{
Type: "object",
Required: []string{"location"},
Properties: map[string]api.ToolProperty{
diff --git a/server/routes_harmony_streaming_test.go b/server/routes_harmony_streaming_test.go
index 8e58ad962..1fb41ff48 100644
--- a/server/routes_harmony_streaming_test.go
+++ b/server/routes_harmony_streaming_test.go
@@ -26,13 +26,7 @@ func getTestTools() []api.Tool {
Function: api.ToolFunction{
Name: "get_weather",
Description: "Get the current weather in a given location",
- Parameters: struct {
- 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"`
- }{
+ Parameters: api.ToolFunctionParameters{
Type: "object",
Required: []string{"location"},
Properties: map[string]api.ToolProperty{
@@ -49,13 +43,7 @@ func getTestTools() []api.Tool {
Function: api.ToolFunction{
Name: "calculate",
Description: "Calculate a mathematical expression",
- Parameters: struct {
- 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"`
- }{
+ Parameters: api.ToolFunctionParameters{
Type: "object",
Required: []string{"expression"},
Properties: map[string]api.ToolProperty{