chore: Add telemetry on structured output errors (no-changelog) (#16389)

This commit is contained in:
Eugene
2025-06-17 08:58:15 +02:00
committed by GitHub
parent 549a541219
commit a953218b9d
4 changed files with 477 additions and 1 deletions

View File

@@ -31,6 +31,7 @@ export class N8nStructuredOutputParser extends StructuredOutputParser<
const { index } = this.context.addInputData(NodeConnectionTypes.AiOutputParser, [
[{ json: { action: 'parse', text } }],
]);
try {
const jsonString = text.includes('```') ? text.split(/```(?:json)?/)[1] : text;
const json = JSON.parse(jsonString.trim());
@@ -61,6 +62,24 @@ export class N8nStructuredOutputParser extends StructuredOutputParser<
},
);
// Add additional context to the error
if (e instanceof SyntaxError) {
nodeError.context.outputParserFailReason = 'Invalid JSON in model output';
} else if (
text.trim() === '{}' ||
(e instanceof z.ZodError &&
e.issues?.[0] &&
e.issues?.[0].code === 'invalid_type' &&
e.issues?.[0].path?.[0] === 'output' &&
e.issues?.[0].expected === 'object' &&
e.issues?.[0].received === 'undefined')
) {
nodeError.context.outputParserFailReason = 'Model output wrapper is an empty object';
} else if (e instanceof z.ZodError) {
nodeError.context.outputParserFailReason =
'Model output does not match the expected schema';
}
logAiEvent(this.context, 'ai-output-parsed', {
text,
response: e.message ?? e,