question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Native Python Function not returning correct result

See original GitHub issue

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: SK-Support@microsoft.com

Issue Analytics

  • State:open
  • Created 4 months ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
evchakicommented, May 18, 2023

@mkarle can you take a look at this.

0reactions
dluccommented, Jun 9, 2023

Team triage: improve function signature detection

Read more comments on GitHub >

github_iconTop Results From Across the Web

Return statement in python function not returning anything
This will show the result. Functions return a value, so you need to store them in variable.
Read more >
The Python return Statement: Usage and Best Practices
If you build a return statement without specifying a return value, then you'll be implicitly returning None . If you define a function...
Read more >
How does return() in Python work?
The return() statement, like in other programming languages ends the function call and returns the result to the caller. It is a key...
Read more >
Python return statement
A return statement is used to end the execution of the function call and “returns” the result (value of the expression following the...
Read more >
Function return values - Learn web development | MDN
We are returning the result of the calculation Math.floor(Math.random() * number) each time the function is called. This return value appears at ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found