From 36243680b511261d793823434d2b743b60f309e7 Mon Sep 17 00:00:00 2001 From: Elisa Carrillo <46950428+queenellie873@users.noreply.github.com> Date: Thu, 22 Oct 2026 22:52:22 -0700 Subject: [PATCH] fix: strip code fences from AI JSON before parsing opportunities claude-sonnet-4-6 wraps its JSON in ```json fences, so refineOpportunity failed with "Failed to parse JSON from AI response." after a good page read. Move the fence strip from resume reviews (#897) into a shared stripCodeFence helper and use it in both places. Plain JSON parses the same as before. --- packages/core/src/infrastructure/ai.ts | 15 +++++++++++++++ packages/core/src/modules/opportunities.ts | 4 ++-- packages/core/src/modules/resume-reviews.ts | 9 ++------- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/core/src/infrastructure/ai.ts b/packages/core/src/infrastructure/ai.ts index 8ac1415c3..3c307f92b 100644 --- a/packages/core/src/infrastructure/ai.ts +++ b/packages/core/src/infrastructure/ai.ts @@ -264,6 +264,21 @@ export async function getChatCompletion({ return success(message); } +/** + * Removes a Markdown code fence (ie: ```json ... ```) from an AI response. + * Some models add one even when asked for only JSON, which breaks + * `JSON.parse`. Text without a fence is returned trimmed. + * + * @param text - The raw text from the AI response. + * @returns The text without the surrounding code fence. + */ +export function stripCodeFence(text: string): string { + return text + .trim() + .replace(/^```(?:json)?\s*/, '') + .replace(/\s*```$/, ''); +} + // "Rerank Documents" type RerankDocumentsOptions = Partial<{ diff --git a/packages/core/src/modules/opportunities.ts b/packages/core/src/modules/opportunities.ts index cafbd8514..fac58355b 100644 --- a/packages/core/src/modules/opportunities.ts +++ b/packages/core/src/modules/opportunities.ts @@ -9,7 +9,7 @@ import { db } from '@oyster/db'; import { ISO8601Date, nullableField } from '@oyster/types'; import { id } from '@oyster/utils'; -import { getChatCompletion } from '@/infrastructure/ai'; +import { getChatCompletion, stripCodeFence } from '@/infrastructure/ai'; import { job, registerWorker } from '@/infrastructure/bull'; import { type GetBullJobData, @@ -781,7 +781,7 @@ export async function refineOpportunity( let json: JSON; try { - json = JSON.parse(completionResult.data); + json = JSON.parse(stripCodeFence(completionResult.data)); } catch (e) { console.debug( 'Failed to parse JSON from AI response.', diff --git a/packages/core/src/modules/resume-reviews.ts b/packages/core/src/modules/resume-reviews.ts index bea943522..72f366ed0 100644 --- a/packages/core/src/modules/resume-reviews.ts +++ b/packages/core/src/modules/resume-reviews.ts @@ -6,7 +6,7 @@ import { z } from 'zod'; import { db, relativeTime } from '@oyster/db'; import { id } from '@oyster/utils'; -import { getChatCompletion } from '@/infrastructure/ai'; +import { getChatCompletion, stripCodeFence } from '@/infrastructure/ai'; import { job, registerWorker } from '@/infrastructure/bull'; import { type GetBullJobData, @@ -184,12 +184,7 @@ export async function reviewResume({ }); try { - const json = completionResult.data - .trim() - .replace(/^```(?:json)?\s*/, '') - .replace(/\s*```$/, ''); - - const object = JSON.parse(json); + const object = JSON.parse(stripCodeFence(completionResult.data)); const feedback = ResumeFeedback.parse(object); await db