semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

Python: Bug: Kernel functions cannot have optional arguments.

Open q-stefanmuscalu opened this issue 1 year ago • 1 comments

Describe the bug I want to write a kernel function with an optional argument. It does not work because semantic kernel ignores optional arguments when calling the /chat/completions endpoint.

To Reproduce Steps to reproduce the behavior:

  1. Define a kernel function like this:
class SearchPlugin():

    @kernel_function(
        name="Search",
        description="Search for people",
    )
    def find_person(
        self,
        first_name: str,
        last_name: str,
        country_code: Optional[str]
    ) -> str:
        return "nothing found"
  1. Ask the agent to search for john doe
  2. Observe the error saying An error occurred while invoking the function SearchPlugin-Search: SearchPlugin.find_person() missing 1 required positional argument: 'country_code'"

The error happens because the metadata sent to the LLM is missing the country_code argument:

{
    "type": "function",
    "function": {
        "name": "SearchPlugin-Search",
        "description": "Search for people",
        "parameters": {
            "type": "object",
            "properties": {
                "first_name": {
                    "type": "string"
                },
                "last_name": {
                    "type": "string"
                }
            },
            "required": [
                "first_name",
                "last_name"
            ]
        }
    }
}

Expected behavior I would expect the metadata to look like this

{
    "type": "function",
    "function": {
        "name": "SearchPlugin-Search",
        "description": "Search for people",
        "parameters": {
            "type": "object",
            "properties": {
                "first_name": {
                    "type": "string"
                },
                "last_name": {
                    "type": "string"
                },
                "country_code": {
                    "type": "string"
                }
            },
            "required": [
                "first_name",
                "last_name"
            ]
        }
    }
}

Screenshots If applicable, add screenshots to help explain your problem.

Platform

  • OS: Mac
  • IDE: PyCharm
  • Language: Python
  • Source: semantic kernel 1.2.0, python 3.11, Azure Open AI

Additional context Add any other context about the problem here.

q-stefanmuscalu avatar Jul 16 '24 16:07 q-stefanmuscalu

This issue will be fixed by removing the if param.is_required condition here: https://github.com/microsoft/semantic-kernel/blob/0fda22e8f547e100e4dd9b8b1307142af42a251a/python/semantic_kernel/connectors/ai/function_calling_utils.py#L39C98-L39C115.

moonbox3 avatar Jul 19 '24 14:07 moonbox3