Commit 006a5d9
.Net: fix: fall back to ToString() when logging function results with unregistered types (#13884)
Fixes #13681
## Problem
When a `KernelFunction` returns a value whose runtime type is not
registered in `AbstractionsJsonContext` (e.g.
`Microsoft.Extensions.AI.TextContent` returned by an MCP tool via
`.AsKernelFunction()`), the function result logging in
AOT/source-generation mode throws `NotSupportedException` during JSON
serialization. The exception is caught but produces an unhelpful log
entry:
```
Function SomePlugin-SomeTool result: Failed to log function result value
System.NotSupportedException: JsonTypeInfo metadata for type 'Microsoft.Extensions.AI.TextContent' was not provided by TypeInfoResolver of type 'Microsoft.SemanticKernel.AbstractionsJsonContext'.
```
## Solution
Add a `ToString()` fallback in the `NotSupportedException` catch block
of `LogFunctionResultValueInternal`. When JSON serialization fails
because the runtime type is absent from the source-generated context,
the logger now calls `resultValue?.Value?.ToString()` to produce useful
output instead of the generic error message.
## Testing
- Added `ItShouldFallBackToToStringWhenJsonSerializationIsNotSupported`
unit test that uses a restricted source-generated
`JsonSerializerContext` (containing only `object` and
`IDictionary<string, object?>`) to simulate the AOT scenario, verifying
that `ToString()` output appears in the log instead of the failure
message.
- All existing `ItShouldLogFunctionResultOfAnyType` test cases are
unchanged.
---------
Co-authored-by: octo-patch <octo-patch@github.com>
Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: SergeyMenshykh <sergemenshikh@gmail.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent 6e1ab9d commit 006a5d9
2 files changed
Lines changed: 55 additions & 2 deletions
File tree
- dotnet/src
- SemanticKernel.Abstractions/Functions
- SemanticKernel.UnitTests/Functions
Lines changed: 16 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
161 | 161 | | |
162 | 162 | | |
163 | 163 | | |
164 | | - | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
165 | 180 | | |
166 | 181 | | |
167 | 182 | | |
| |||
Lines changed: 39 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
13 | | - | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
48 | 49 | | |
49 | 50 | | |
50 | 51 | | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
51 | 80 | | |
52 | 81 | | |
53 | 82 | | |
54 | 83 | | |
55 | 84 | | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
56 | 94 | | |
0 commit comments