Skip to content

Commit 2864ac2

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

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-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: 30 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,35 @@ 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+
const string configJson = """
128+
{
129+
"_ldMeta": {"versionKey": "1", "enabled": true},
130+
"model": {},
131+
"prompt": [
132+
{
133+
"content": "This is a {{ malformed }]} prompt",
134+
"role": "System"
135+
}
136+
]
137+
}
138+
""";
139+
140+
141+
mockClient.Setup(x =>
142+
x.JsonVariation("foo", It.IsAny<Context>(), It.IsAny<LdValue>())).Returns(LdValue.Parse(configJson));
143+
144+
mockClient.Setup(x => x.GetLogger()).Returns(mockLogger.Object);
145+
146+
var client = new LdAiClient(mockClient.Object);
147+
var tracker = client.ModelConfig("foo", Context.New("key"), LdAiConfig.Disabled);
148+
Assert.False(tracker.Config.Enabled);
149+
}
120150

121151
[Fact]
122152
public void TestInterpolationWithBasicContext()

0 commit comments

Comments
 (0)