Skip to content

Commit 02d52ea

Browse files
.Net: Handle rendering a list of strings in a SK prompt (#11780)
### Motivation and Context Convert lists of strings to a string value correctly ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [ ] The code builds clean without any errors or warnings - [ ] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄
1 parent 3dbfb44 commit 02d52ea

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

dotnet/src/SemanticKernel.Core/PromptTemplate/KernelPromptTemplate.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Diagnostics;
6+
using System.Globalization;
67
using System.Linq;
78
using System.Text;
89
using System.Threading;
@@ -99,11 +100,11 @@ private async Task<string> RenderAsync(List<Block> blocks, Kernel kernel, Kernel
99100
switch (block)
100101
{
101102
case ITextRendering staticBlock:
102-
blockResult = InternalTypeConverter.ConvertToString(staticBlock.Render(arguments), kernel.Culture);
103+
blockResult = ConvertToString(staticBlock.Render(arguments), kernel.Culture);
103104
break;
104105

105106
case ICodeRendering dynamicBlock:
106-
blockResult = InternalTypeConverter.ConvertToString(await dynamicBlock.RenderCodeAsync(kernel, arguments, cancellationToken).ConfigureAwait(false), kernel.Culture);
107+
blockResult = ConvertToString(await dynamicBlock.RenderCodeAsync(kernel, arguments, cancellationToken).ConfigureAwait(false), kernel.Culture);
107108
break;
108109

109110
default:
@@ -187,5 +188,13 @@ private static bool ShouldEncodeTags(bool disableTagEncoding, HashSet<string> sa
187188
return !disableTagEncoding && block is not TextBlock;
188189
}
189190

191+
private static string? ConvertToString(object? value, CultureInfo? culture = null)
192+
{
193+
if (value is null) { return null; }
194+
195+
return value is List<string> stringList
196+
? string.Join("\n", stringList)
197+
: InternalTypeConverter.ConvertToString(value, culture);
198+
}
190199
#endregion
191200
}

0 commit comments

Comments
 (0)