Skip to content

fix(python): keep falsy values in FunctionResult.__str__ - #14849

Open
DZH166 wants to merge 1 commit into
microsoft:mainfrom
DZH166:fix/keep-falsy-values-in-functionresult-str
Open

DZH166 wants to merge 1 commit into
microsoft:mainfrom
DZH166:fix/keep-falsy-values-in-functionresult-str

Conversation

@DZH166

@DZH166 DZH166 commented Nov 21, 2026

Copy link
Copy Markdown

Problem

FunctionResult.__str__ wraps the whole conversion in if self.value:, so any falsy but valid
result is turned into an empty string. A kernel function that legitimately returns 0
(a count), False (a predicate) or 0.0 therefore stringifies to "":

class Counter:
    @kernel_function(name="count_matches")
    def count_matches(self, text: str) -> int:
        return text.count("needle")

result = await kernel.invoke(plugin_name="counter", function_name="count_matches", text="no hits here")
result.value   # 0
str(result)    # ""   <- expected "0"

This is silent data loss on the public invoke path: prompt rendering, semantic_kernel.text.aggregate_chunked_results
and any host that turns a FunctionResult into text all see an empty string instead of the value.
The method's own docstring says it calls str() on the value, and the prompt template engine already
renders 0 / False correctly for plain variables, so a function result was the only place where a
zero disappeared.

Fix

Special-case only None (which is legitimately empty). The list and dict branches stay as they were,
except that empty containers keep returning "" instead of raising IndexError on self.value[0],
which the old if self.value: guard was also covering.

Tests

New test test_function_result_str_keeps_falsy_values in python/tests/unit/functions/test_function_result.py,
parametrized over 0, False, 0.0 and ""; the existing test_function_result_str_empty_value
still covers the None case, and test_function_result_str_with_conversion_error still covers the
FunctionResultError path.

Measured counts (Python 3.13, offline):

  • pytest tests/unit/functions/test_function_result.py -> 13 passed
  • pytest tests/unit/functions (excluding test_kernel_plugins.py) -> 197 passed
  • pytest tests/unit/contents -> 438 passed

tests/unit/functions/test_kernel_plugins.py reports 31 passed, 3 failed both before and after this
change; the 3 failures are a pre-existing ImportError: cannot import name 'Spec' from 'openapi_core'
from the locally installed openapi_core version and are unrelated to this patch.

@DZH166
DZH166 requested a review from a team as a code owner November 21, 2026 13:50
Copilot AI balanced review requested due to automatic review settings November 21, 2026 13:50
@DZH166
DZH166 deployed to github-app-auth November 21, 2026 13:50 — with GitHub Actions Active

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The regression test lacks empty-list and empty-dictionary cases, leaving the new guards unprotected against regressions that could reintroduce FunctionResultError for empty containers.

1 open issue
What changed in this PR

Fixes Python FunctionResult.__str__ so valid falsy results remain visible when kernel function results are converted to text.

Changes:

  • Preserves string representations of 0, False, and 0.0.
  • Keeps empty-string behavior for None, empty lists, and empty dictionaries.
  • Adds parameterized regression tests for falsy scalar values.
File Description
python/​tests/​unit/​functions/​test_function_result.py Adds falsy-value string conversion tests.
python/​semantic_kernel/​functions/​function_result.py Replaces the truthiness guard with explicit handling of None and empty containers.

Review effort: Balanced

assert str(result) == ""


@pytest.mark.parametrize(("value", "expected"), [(0, "0"), (False, "False"), (0.0, "0.0"), ("", "")])

This branch was successfully deployed

1 active deployment
github-app-auth — 6794395d Deployed Nov 21, 2026 by DZH166 via add_label #29781
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants