Skip to content

Commit 4e32ae0

Browse files
committed
fix: catch exceptions thrown by template interpolation
1 parent 9c9dc7c commit 4e32ae0

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

pkgs/sdk/server-ai/src/LdAiClient.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,27 @@ public ILdAiConfigTracker ModelConfig(string key, Context context, LdAiConfig de
7373
}
7474

7575

76-
var prompt =
77-
parsed.Prompt?.Select(m => new LdAiConfig.Message(InterpolateTemplate(m.Content, mergedVariables), m.Role));
76+
var prompt = new List<LdAiConfig.Message>();
77+
78+
if (parsed.Prompt != null)
79+
{
80+
for (var i = 0; i < parsed.Prompt.Count; i++)
81+
{
82+
try
83+
{
84+
var content = InterpolateTemplate(parsed.Prompt[i].Content, mergedVariables);
85+
prompt.Add(new LdAiConfig.Message(content, parsed.Prompt[i].Role));
86+
}
87+
catch (Exception ex)
88+
{
89+
_logger.Error($"AI model config prompt has malformed message at index {i}: {ex.Message} (returning default config)");
90+
return new LdAiConfigTracker(_client, key, defaultValue, context);
91+
}
92+
}
93+
}
7894

7995
return new LdAiConfigTracker(_client, key, new LdAiConfig(parsed.Meta?.Enabled ?? false, prompt, parsed.Meta, parsed.Model), context);
96+
8097
}
8198

8299
/// <summary>

pkgs/sdk/server-ai/test/InterpolationTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Text.Json;
34
using System.Text.Json.Serialization;
@@ -117,6 +118,37 @@ public void TestInterpolationWithArraySectionWorks()
117118
Assert.Equal("hello world ! ", result);
118119
}
119120

121+
[Fact]
122+
public void TestInterpolationMalformed()
123+
{
124+
var mockClient = new Mock<ILaunchDarklyClient>();
125+
var mockLogger = new Mock<ILogger>();
126+
127+
128+
// The replacement is done this way because to use string.Format, we'd need to escape the curly braces.
129+
var configJson = """
130+
{
131+
"_ldMeta": {"versionKey": "1", "enabled": true},
132+
"model": {},
133+
"prompt": [
134+
{
135+
"content": "This is a {{ malformed }]} prompt",
136+
"role": "System"
137+
}
138+
]
139+
}
140+
""";
141+
142+
143+
mockClient.Setup(x =>
144+
x.JsonVariation("foo", It.IsAny<Context>(), It.IsAny<LdValue>())).Returns(LdValue.Parse(configJson));
145+
146+
mockClient.Setup(x => x.GetLogger()).Returns(mockLogger.Object);
147+
148+
var client = new LdAiClient(mockClient.Object);
149+
var tracker = client.ModelConfig("foo", Context.New("key"), LdAiConfig.Disabled, null);
150+
Assert.False(tracker.Config.Enabled);
151+
}
120152

121153
[Fact]
122154
public void TestInterpolationWithBasicContext()

0 commit comments

Comments
 (0)