diff --git a/src/lib/deepseek.ts b/src/lib/deepseek.ts index f7341a1..26b5f91 100644 --- a/src/lib/deepseek.ts +++ b/src/lib/deepseek.ts @@ -402,6 +402,20 @@ export function truncateToMaxWords( // ── Appels DeepSeek ────────────────────────────────────────────────────── +/** + * Strip d'éventuels fences markdown autour d'une réponse JSON. + * + * DeepSeek wrappe parfois sa sortie dans des blocs ```json ... ``` malgré + * `response_format: { type: "json_object" }`. On retire ces fences avant + * `JSON.parse` pour éviter `SyntaxError: Expected property name`. + */ +function sanitizeJsonContent(raw: string): string { + return raw + .replace(/^\s*```(?:json)?\s*/i, "") + .replace(/\s*```\s*$/i, "") + .trim(); +} + async function callDeepSeek( system: string, user: string, @@ -444,7 +458,7 @@ async function callDeepSeek( throw new Error("DeepSeek API: réponse vide"); } - return content; + return sanitizeJsonContent(content); } catch (err) { const kind = err instanceof Error && err.name === "TimeoutError"