Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion python/semantic_kernel/functions/function_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ class FunctionResult(KernelBaseModel):

def __str__(self) -> str:
"""Get the string representation of the result."""
if self.value:
if self.value is not None:
try:
if isinstance(self.value, list):
if not self.value:
return ""
return (
str(self.value[0])
if isinstance(self.value[0], KernelContent)
Expand All @@ -48,6 +50,8 @@ def __str__(self) -> str:
if isinstance(self.value, dict):
# TODO (eavanvalkenburg): remove this once function result doesn't include input args
# This is so an integration test can pass.
if not self.value:
return ""
return str(list(self.value.values())[-1])
return str(self.value)
except Exception as e:
Expand Down
19 changes: 19 additions & 0 deletions python/tests/unit/functions/test_function_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ def test_function_result_str_empty_value():
assert str(result) == ""


@pytest.mark.parametrize(
("value", "expected"),
[
(0, "0"),
(0.0, "0.0"),
(False, "False"),
("", ""),
([], ""),
({}, ""),
],
)
def test_function_result_str_with_falsy_value(value, expected):
result = FunctionResult(
function=KernelFunctionMetadata(name="test_function", is_prompt=False, is_asynchronous=False),
value=value,
)
assert str(result) == expected


def test_function_result_str_with_conversion_error():
class Unconvertible:
def __str__(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,30 @@ async def my_function(myVar: str) -> str:
assert result == "foo-BAR-baz"


async def test_it_renders_code_with_falsy_results(kernel: Kernel):
arguments = KernelArguments()

@kernel_function(name="subtract")
def subtract(a: int, b: int) -> int:
return a - b

@kernel_function(name="is_even")
def is_even(a: int) -> bool:
return a % 2 == 0

kernel.add_function("math", KernelFunction.from_method(subtract, "math"))
kernel.add_function("math", KernelFunction.from_method(is_even, "math"))

arguments["x"] = 5
arguments["y"] = 5
arguments["z"] = 3
template = "The result is: {{math.subtract a=$x b=$y}} and even={{math.is_even a=$z}}"
target = create_kernel_prompt_template(template, allow_dangerously_set_content=True)
result = await target.render(kernel, arguments)

assert result == "The result is: 0 and even=False"


async def test_it_renders_code_error(kernel: Kernel):
arguments = KernelArguments()

Expand Down
Loading