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

Python: Bug: Kernel functions cannot have optional arguments. #7297

Closed
q-stefanmuscalu opened this issue Jul 16, 2024 · 1 comment · Fixed by #7367
Closed

Python: Bug: Kernel functions cannot have optional arguments. #7297

q-stefanmuscalu opened this issue Jul 16, 2024 · 1 comment · Fixed by #7367
Assignees
Labels
bug Something isn't working python Pull requests for the Python Semantic Kernel

Comments

@q-stefanmuscalu
Copy link

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 q-stefanmuscalu added the bug Something isn't working label Jul 16, 2024
@markwallace-microsoft markwallace-microsoft added python Pull requests for the Python Semantic Kernel triage labels Jul 16, 2024
@moonbox3 moonbox3 self-assigned this Jul 19, 2024
@moonbox3 moonbox3 removed the triage label Jul 19, 2024
@moonbox3
Copy link
Contributor

github-merge-queue bot pushed a commit that referenced this issue Jul 23, 2024
### Motivation and Context

We have some gaps in how we build the JSON schema for function calling
related to Union and Optional types. There is also some work to do
related to handling `Optional` versus `param | None = None`.

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

### Description

This PR improves the JSON schema building for the Union and Optional
types. It adds additional tests and improves some existing tests.
- Closes #7297 
- Closes #7298

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [X] The code builds clean without any errors or warnings
- [X] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [X] All unit tests pass, and I have added new tests where possible
- [X] I didn't break anyone 😄
@github-project-automation github-project-automation bot moved this to Sprint: Done in Semantic Kernel Jul 23, 2024
LudoCorporateShark pushed a commit to LudoCorporateShark/semantic-kernel that referenced this issue Aug 25, 2024
### Motivation and Context

We have some gaps in how we build the JSON schema for function calling
related to Union and Optional types. There is also some work to do
related to handling `Optional` versus `param | None = None`.

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

### Description

This PR improves the JSON schema building for the Union and Optional
types. It adds additional tests and improves some existing tests.
- Closes microsoft#7297 
- Closes microsoft#7298

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [X] The code builds clean without any errors or warnings
- [X] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [X] All unit tests pass, and I have added new tests where possible
- [X] I didn't break anyone 😄
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working python Pull requests for the Python Semantic Kernel
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants