Skip to content

Commit f7ec32e

Browse files
committed
fix: strip markdown code blocks from AI responses
Removes markdown code block wrappers that may surround AI-generated content before parsing. This prevents deserialization errors when the AI model returns valid JSON wrapped in triple backticks with language identifiers. Extracts the actual content from patterns like ```json\n{...}\n``` to ensure clean parsing of the response data.
1 parent fc49766 commit f7ec32e

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

server/api/link/ai.get.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,12 @@ export default eventHandler(async (event) => {
6161

6262
const response = await AI.run(aiModel as keyof AiModels, { messages }) as AiChatResponse
6363

64-
return destr(response.response ?? response.choices?.[0]?.message?.content)
64+
let content = response.response ?? response.choices?.[0]?.message?.content ?? ''
65+
// Strip markdown code block wrapper (e.g. ```json\n{...}\n```)
66+
const codeBlockMatch = content.match(/```\w*\n([^`]+)```/)
67+
if (codeBlockMatch?.[1]) {
68+
content = codeBlockMatch[1].trim()
69+
}
70+
71+
return destr(content)
6572
})

0 commit comments

Comments
 (0)