Python: native functions reconstruct already-typed dataclass arguments
Describe the bug
Passing an existing dataclass instance to a native kernel function annotated with that class fails before the function runs. Passing the equivalent dictionary works. This also breaks passing an object returned by one native function to another.
To Reproduce
import asyncio
from dataclasses import dataclass
from semantic_kernel import Kernel
from semantic_kernel.functions import KernelArguments, kernel_function
@dataclass
class Customer:
customer_id: str
name: str
@kernel_function
def greet(customer: Customer) -> str:
return f"{customer.customer_id}: {customer.name}"
async def main():
kernel = Kernel()
function = kernel.add_function(plugin_name="customers", function=greet)
print((await kernel.invoke(function, arguments=KernelArguments(
customer={"customer_id": "C-123", "name": "Ada"}
))).value) # C-123: Ada
print((await kernel.invoke(function, arguments=KernelArguments(
customer=Customer("C-123", "Ada")
))).value) # fails
asyncio.run(main())
Expected behavior
Both calls return C-123: Ada.
Actual behavior
The second call raises KernelInvokeException, caused by Customer.__init__() missing 1 required positional argument: 'name'. _parse_parameter calls Customer(existing_customer) even though the argument already has the requested type.
A one-field dataclass can silently nest the original object into its field instead. Lists of typed instances also hit the constructor fallback. Passing a dictionary is a workaround.
Platform
Python 3.12.15, Semantic Kernel 1.45.0 on current main 25501830b9483ad6b23955dee340c8590fa54152, Pydantic 2.13.5, macOS ARM. No AI model or service is involved.
Additional context
PR #14466 handles generic/union validation but leaves this concrete-class fallback unchanged. Would preserving already-typed custom instances be the intended behavior? Happy to contribute a fix and regression tests once the approach is agreed.
I'll take on a small fix and regression tests for preserving already-typed concrete arguments. I'll keep Pydantic revalidation and existing dictionary/list conversion behavior intact.