From e97864b80658345c1c0e67c86805cc20728099a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=B3ricz=20Gerg=C5=91?= Date: Mon, 11 Nov 2024 10:55:45 +0100 Subject: [PATCH] fix(scrapeURL/llmExtract): better schema normalization --- .../scrapeURL/transformers/llmExtract.ts | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/apps/api/src/scraper/scrapeURL/transformers/llmExtract.ts b/apps/api/src/scraper/scrapeURL/transformers/llmExtract.ts index fedea33e..22e2649b 100644 --- a/apps/api/src/scraper/scrapeURL/transformers/llmExtract.ts +++ b/apps/api/src/scraper/scrapeURL/transformers/llmExtract.ts @@ -19,6 +19,28 @@ export class LLMRefusalError extends Error { } function normalizeSchema(x: any): any { + if (typeof x !== "object" || x === null) return x; + + if (x["$defs"] !== null && typeof x["$defs"] === "object") { + x["$defs"] = Object.fromEntries(Object.entries(x["$defs"]).map(([name, schema]) => [name, normalizeSchema(schema)])); + } + + if (x && x.anyOf) { + x.anyOf = x.anyOf.map(x => normalizeSchema(x)); + } + + if (x && x.oneOf) { + x.oneOf = x.oneOf.map(x => normalizeSchema(x)); + } + + if (x && x.allOf) { + x.allOf = x.allOf.map(x => normalizeSchema(x)); + } + + if (x && x.not) { + x.not = normalizeSchema(x.not); + } + if (x && x.type === "object") { return { ...x,