Skip to content

Native Python Function not returning correct result #1069

Closed

Description

Describe the bug

I have written a native Python function, but it does not appear to be returning the expected value. Instead, it just returns its input

To Reproduce
Steps to reproduce the behavior:

I have a file native_function.py which contains:

from semantic_kernel.skill_definition import sk_function


class PythonGroundingSkill:
    @sk_function(
        description="Extract contents of <json_block> tags",
        name="ParseJsonBlock",
    )
    def extract_json_block_from_text(self, text: str):
        start_tag = "<json_block>"
        end_tag = "</json_block>"
        i_0 = text.find(start_tag) + len(start_tag)
        i_1 = text.find(end_tag)

        extracted_text = text[i_0:i_1]
        print(f"=~=~=~=\n{extracted_text}\n=~=~=~=\n")

        return extracted_text

I then call it through the following file:

import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import AzureTextCompletion, OpenAITextCompletion

kernel = sk.Kernel()

useAzureOpenAI = True

# Configure AI service used by the kernel
if useAzureOpenAI:
    deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()
    kernel.add_text_completion_service("dv", AzureTextCompletion(deployment, endpoint, api_key))
else:
    api_key, org_id = sk.openai_settings_from_dot_env()
    kernel.add_text_completion_service("dv", OpenAITextCompletion("text-davinci-003", api_key, org_id))

skills_directory = "../../skills"
groundingNativeFunctions = kernel.import_native_skill_from_directory(skills_directory, "GroundingSkill")


jsonblock_fetch = groundingNativeFunctions["ParseJsonBlock"]

function_result = jsonblock_fetch("something <json_block>[0, 1]</json_block> something else")

print(function_result.result)

I get output

=~=~=~=
[0, 1]
=~=~=~=

something <json_block>[0, 1]</json_block> something else

So internally, the function is doing the right thing (as seen from the =~=~ bits), but the returned value was the original string, not the return value of the function defined.

Expected behavior

If I return a value from a function, I would like to receive that value back.

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

Desktop (please complete the following information):

  • OS: Win11 / Python 3.11
  • IDE: [e.g. Visual Studio, VS Code]
  • NuGet Package Version [e.g. 0.1.0]

Additional context

Name: semantic-kernel
Version: 0.2.7.dev0
Summary:
Home-page:
Author: Microsoft
Author-email: [email protected]

Activity

  1. JTremb commented on May 18, 2023

    @JTremb
    Contributor

    I had similar issue today , can you try adding a return type hint ?
    e.g.

    def extract_json_block_from_text(self, text: str): -> str
  2. JTremb commented on May 18, 2023

    @JTremb
    Contributor

    Looking a bit more at this. When there is no return type it infers a DelegateTypes.Void.

    • It would probably be better to raise an exception in this case and infers Void when it's explicit.
    • DelegateTypes.Void should also not return $input
  3. riedgar-ms commented on May 18, 2023

    @riedgar-ms
    ContributorAuthor

    OK, so changing the signature to -> str did indeed let it get back the right information.

    However, when I try changing the return type to ->Union[Dict[str,Any], List[Any]] (which is what I'd like it to be long-term), it still doesn't work. Now, the function apparently isn't even in the dictionary. Why is this?

  4. riedgar-ms commented on May 18, 2023

    @riedgar-ms
    ContributorAuthor

    Looking a bit more at this. When there is no return type it infers a DelegateTypes.Void.

    • It would probably be better to raise an exception in this case and infers Void when it's explicit.
    • DelegateTypes.Void should also not return $input

    Given that type hints aren't usually required in Python and that -> None is allowable, I think raising an exception for a forgotten return value would be a very good idea.

  5. evchaki commented on May 18, 2023

    @evchaki
    Contributor

    Mark Karle (@mkarle) can you take a look at this.

  6. moved this to Future - Documentation in Python Semantic Kernelon Jun 9, 2023
  7. dluc commented on Jun 9, 2023

    @dluc
    Contributor

    Team triage: improve function signature detection

  8. moved this to Backlog - Documentation in Semantic Kernelon Jun 20, 2023
  9. moved this from Backlog - Documentation to No status in Semantic Kernelon Aug 10, 2023
  10. moved this from Bugs to Sprint: In Progress in Semantic Kernelon Sep 20, 2023
  11. aditi-l commented on Sep 22, 2023

    @aditi-l

    OK, so changing the signature to -> str did indeed let it get back the right information.

    However, when I try changing the return type to ->Union[Dict[str,Any], List[Any]] (which is what I'd like it to be long-term), it still doesn't work. Now, the function apparently isn't even in the dictionary. Why is this?

    Even I am facing the same issue. I want my native function to return dict and list values as well, but it only returns string values.

  12. mkarle commented on Sep 22, 2023

    @mkarle
    Contributor

    Right now you can only return None, str, and SKContext types. You can put dict and list values in the SKContext object though.

  13. moved this from Sprint: In Progress to Sprint: In Review in Semantic Kernelon Sep 23, 2023
  14. moved this from Backlog - Documentation to Sprint: Done in Python Semantic Kernelon Sep 27, 2023
  15. moved this from Sprint: In Review to Sprint: Done in Semantic Kernelon Sep 27, 2023
  16. AliRaza190 commented on Nov 2, 2023

    @AliRaza190

    Right now you can only return None, str, and SKContext types. You can put dict and list values in the SKContext object though.

    Can you give an example of how to use SKContext for returning a JSON or a dict object in Python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

pythonPull requests for the Python Semantic Kernel

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions