fix(Structured Output Parser Node): Handle passed objects that do not match schema (#17774)

This commit is contained in:
jeanpaul
2025-07-29 13:20:12 +02:00
committed by GitHub
parent 04c1121075
commit 1fb78cb0ea
2 changed files with 28 additions and 1 deletions

View File

@@ -608,6 +608,33 @@ describe('OutputParserStructured', () => {
),
).rejects.toThrow('Required');
});
it('should raise schema fit error when passing in empty object', async () => {
const jsonExample = `{
"user": {
"name": "Alice",
"email": "alice@example.com",
"profile": {
"age": 30,
"city": "New York"
}
},
"tags": ["work", "important"]
}`;
thisArg.getNodeParameter.calledWith('schemaType', 0).mockReturnValueOnce('fromJson');
thisArg.getNodeParameter
.calledWith('jsonSchemaExample', 0)
.mockReturnValueOnce(jsonExample);
const { response } = (await outputParser.supplyData.call(thisArg, 0)) as {
response: N8nStructuredOutputParser;
};
// @ts-expect-error 2345
await expect(response.parse({})).rejects.toThrow(
"Model output doesn't fit required format",
);
});
});
describe('manual schema mode', () => {

View File

@@ -66,7 +66,7 @@ export class N8nStructuredOutputParser extends StructuredOutputParser<
if (e instanceof SyntaxError) {
nodeError.context.outputParserFailReason = 'Invalid JSON in model output';
} else if (
text.trim() === '{}' ||
(typeof text === 'string' && text.trim() === '{}') ||
(e instanceof z.ZodError &&
e.issues?.[0] &&
e.issues?.[0].code === 'invalid_type' &&