Skip to content
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

Fix OpenAI error when properties is empty in function call : object s… #419

Merged
merged 1 commit into from
Jun 30, 2023

Conversation

royalrick
Copy link
Contributor

when properties is empty in function call, OpenAI API will return error:

  • status: 400
  • msg: object schema missing properties

@codecov
Copy link

codecov bot commented Jun 30, 2023

Codecov Report

Merging #419 (8c7c234) into master (9c99f36) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master     #419   +/-   ##
=======================================
  Coverage   96.95%   96.95%           
=======================================
  Files          17       17           
  Lines         690      690           
=======================================
  Hits          669      669           
  Misses         15       15           
  Partials        6        6           

@@ -27,7 +27,7 @@ type Definition struct {
// one element, where each element is unique. You will probably only use this with strings.
Enum []string `json:"enum,omitempty"`
// Properties describes the properties of an object, if the schema type is Object.
Properties map[string]Definition `json:"properties,omitempty"`
Properties map[string]Definition `json:"properties"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@royalrick Thanks for kind PR! Even if you remove 'omitempty', an error will be returned due to OpenAI API's specification where Properties are required.

Copy link
Contributor Author

@royalrick royalrick Jun 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove 'omitempty' and init properties like make(map[string]any), it will works.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! It seems like initializing properties as an empty object and sending it results in success.

failed:

$ curl https://api.openai.com/v1/chat/completions -u :$OPENAI_API_KEY -H 'Content-Type: application/json' -d '{
                "model": "gpt-3.5-turbo-0613",
                "messages": [
                  {"role": "user", "content": "What is the weather like in Boston?"}
                ],
                "functions": [
                  {
                    "name": "get_current_weather",
                    "parameters": {
                      "type": "object"
                    }
                  }
                ]
              }'
{
  "error": {
    "message": [
      "Invalid schema for function 'get_current_weather': In context=(), object schema missing properties"
    ],
    "type": "invalid_request_error",
    "param": null,
    "code": null
  }
}

success:

$ curl https://api.openai.com/v1/chat/completions -u :$OPENAI_API_KEY -H 'Content-Type: application/json' -d '{
                "model": "gpt-3.5-turbo-0613",
                "messages": [
                  {"role": "user", "content": "What is the weather like in Boston?"}
                ],
                "functions": [
                  {
                    "name": "get_current_weather",
                    "parameters": {
                      "type": "object",
                      "properties": {}
                    }
                  }
                ]
              }'
{
  "id": "chatcmpl-***************************************",
  "object": "chat.completion",
  "created": 1688129746,
  "model": "gpt-3.5-turbo-0613",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": null,
        "function_call": {
          "name": "get_current_weather",
          "arguments": "{}"
        }
      },
      "finish_reason": "function_call"
    }
  ],
  "usage": {
    "prompt_tokens": 40,
    "completion_tokens": 8,
    "total_tokens": 48
  }
}

@vvatanabe
Copy link
Collaborator

@royalrick Thanks for fixing it!

@vvatanabe vvatanabe merged commit 177c143 into sashabaranov:master Jun 30, 2023
3 checks passed
@danielchristianschroeter
Copy link

danielchristianschroeter commented Jul 7, 2023

I get now a "Invalid schema for function 'get_weather_forecast': None is not of type 'object'". Did you tested it with filled properties?

[]openai.FunctionDefinition{
		{
			Name:        "get_weather_forecast",
			Description: "Retrieve a weather forecast for a specific location.",
			Parameters: jsonschema.Definition{
				Type: jsonschema.Object,
				Properties: map[string]jsonschema.Definition{
					"location": {
						Type:        jsonschema.String,
						Description: "The location for which to retrieve the weather forecast (city and country separated by a space).",
					},
				},
				Required: []string{"location"},
			},
		},
	}

@royalrick
Copy link
Contributor Author

This may be a mistake, you can temporarily solve it like this:

		Parameters: &jsonschema.Definition{
			Type: jsonschema.Object,
			Properties: map[string]jsonschema.Definition{
				"name": {
					Description: "Appropriate role-based name (_GPT)",
					Type:        jsonschema.String,
					Properties:  make(map[string]jsonschema.Definition),
				},
				"role": {
					Description: "Role description",
					Type:        jsonschema.String,
					Properties:  make(map[string]jsonschema.Definition),
				},
				"goals": {
					Description: "Efficient goals set step by step to complete task",
					Type:        jsonschema.Array,
					Items: &jsonschema.Definition{
						Type:        jsonschema.String,
						Description: "Goals",
						Properties:  make(map[string]jsonschema.Definition),
					},
					Properties: make(map[string]jsonschema.Definition),
				},
			},
			Required: []string{"name", "role", "goals"},
		},

@vvatanabe
Copy link
Collaborator

@danielchristianschroeter @royalrick Fixed #431. Thanks for the report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants