Skip to content

Commit b4cc2a9

Browse files
committed
Record decision choice answers as strings
1 parent 4ee7395 commit b4cc2a9

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎packages/server-utils/src/ai/openai/mapping.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ function toJevAnswerFormat(answer: unknown): unknown {
7171
const options = answer.probabilities.filter(isObjectLike);
7272
const probabilities = Object.fromEntries(options.map(option => [String(option.value), option.probability]));
7373
if (answer.type === 'choice') {
74-
return { ...answer, probabilities };
74+
// Choice values can be booleans, but Sentry renders a choice as a string, like its `probabilities` keys.
75+
return { ...answer, choice: String(answer.choice), probabilities };
7576
}
7677

7778
const legend = Object.fromEntries(

‎packages/server-utils/test/ai/lib/tracing/openai.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,42 @@ describe('instrumentOpenAiClient span names', () => {
118118
{ type: 'evaluation', answers: { wants_refund: { type: 'predicate', probability: 0.97 } } },
119119
]);
120120
});
121+
122+
it('records a boolean decision choice as a string', async () => {
123+
const endedSpans = setupClient('stream');
124+
const instrumented = instrumentOpenAiClient({
125+
decisions: {
126+
create: vi.fn().mockResolvedValue({
127+
model: 'gpt-6-luna',
128+
answers: [
129+
{
130+
type: 'choice',
131+
name: 'is_spam',
132+
choice: true,
133+
probabilities: [
134+
{ value: true, probability: 0.91 },
135+
{ value: false, probability: 0.09 },
136+
],
137+
confidence: 0.91,
138+
},
139+
],
140+
}),
141+
},
142+
});
143+
144+
await instrumented.decisions.create({
145+
model: 'gpt-6-luna',
146+
input: 'Win a free cruise!',
147+
questions: [{ type: 'choice', name: 'is_spam', choices: [{ value: true }, { value: false }] }],
148+
});
149+
150+
expect(JSON.parse(spanToStaticSpanJSON(endedSpans[0]!).data[GEN_AI_OUTPUT_MESSAGES] as string)).toEqual([
151+
{
152+
type: 'evaluation',
153+
answers: {
154+
is_spam: { type: 'choice', choice: 'true', probabilities: { true: 0.91, false: 0.09 }, confidence: 0.91 },
155+
},
156+
},
157+
]);
158+
});
121159
});

0 commit comments

Comments
 (0)