Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 6, 2025

Description

Fixes #[issue_number] - This PR resolves an issue where non-string KernelArguments were being incorrectly converted to strings when passed as function parameters in prompt templates, causing type-related errors.

Problem

When using function calls in Semantic Kernel Prompt Templates with injected KernelArguments, all argument values were being converted to strings via VarBlock.render() before being passed to functions. This caused issues when functions expected non-string types:

@kernel_function
def process_list(self, items: list) -> str:
    return f"List length: {len(items)}"

# Template usage
template = "{{ plugin.process_list items=$my_list }}"
arguments = KernelArguments(my_list=[1, 2, 3, 4, 5])

# BEFORE: Function received ['[', '1', ',', ' ', '2', ',', ' ', '3', ',', ' ', '4', ',', ' ', '5', ']']
# (15 characters, not 5 items - because str([1,2,3,4,5]) was iterated character-by-character)

# AFTER: Function receives [1, 2, 3, 4, 5]
# (Correct list with 5 items)

Solution

Made minimal changes to preserve argument types when passing to functions:

  1. named_arg_block.py: Modified NamedArgBlock.render() to return the actual value from KernelArguments instead of the string representation for variable references. This preserves types for named arguments like arg=$var.

  2. code_block.py: Updated _enrich_function_arguments() to handle VarBlock positional arguments by directly retrieving values from KernelArguments. This preserves types for positional arguments like $var.

The fix ensures that:

  • Variable references in templates ($myvar) render as strings when used directly in output
  • Variable references passed as function arguments preserve their original types
  • Both named (arg=$var) and positional ($var) argument styles work correctly

Type Coverage

Now properly supports:

  • ✅ Integers, floats, booleans
  • ✅ Lists, dictionaries, sets
  • ✅ None values
  • ✅ Custom objects
  • ✅ Mixed type arguments
  • ✅ String arguments (backward compatible)

Testing

  • Added comprehensive test suite (test_non_string_arguments.py) with 10 test cases
  • All 669 existing prompt template and template engine tests pass
  • Verified backward compatibility with string arguments and literal values

Breaking Changes

None - this fix is fully backward compatible. String arguments continue to work as before.

Original prompt

This section details on the original issue you should resolve

<issue_title>Python: Bug: [Python] - Using Non-String KernelArguments in Prompt Template function parameters fails</issue_title>
<issue_description>Describe the bug
When using Function Calls in a Semantic Kernel Prompt Template, and naming an injected KernelArgument to be passed to the function.
The argument is converted to a str() by the VarBlock causing the Function Invocation to fail if it expects the real type of the argument.

To Reproduce
Steps to reproduce the behavior:

  1. Create a Kernel Function that accepts a non string parameter
  2. Inject the parameter as a Kernel Argument
  3. Create a Prompt Template calling that Kernel Function with the argument
  4. Render the Prompt Template

Expected behavior
Function is called with the correct argument type. The return value from the function is what's rendered as a string in the final template.

Platform

  • Language: Python
  • Source: PIP 1.36.0</issue_description>

<agent_instructions>This issue if for Python only.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

Fixes #13199

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] Python: Bug: [Python] - Using Non-String KernelArguments in Prompt Template function parameters fails Python: Fix non-string KernelArguments type preservation in prompt template function parameters Oct 6, 2025
Copilot finished work on behalf of markwallace-microsoft October 6, 2025 14:29
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.

Python: Bug: [Python] - Using Non-String KernelArguments in Prompt Template function parameters fails

2 participants