diff --git a/src/lib/models/providers/ollama/ollamaLLM.ts b/src/lib/models/providers/ollama/ollamaLLM.ts index 9bf5139..3bcd3cc 100644 --- a/src/lib/models/providers/ollama/ollamaLLM.ts +++ b/src/lib/models/providers/ollama/ollamaLLM.ts @@ -11,6 +11,7 @@ import { Ollama, Tool as OllamaTool, Message as OllamaMessage } from 'ollama'; import { parse } from 'partial-json'; import crypto from 'crypto'; import { Message } from '@/lib/types'; +import { repairJson } from '@toolsycc/json-repair'; type OllamaConfig = { baseURL: string; @@ -205,7 +206,13 @@ class OllamaLLM extends BaseLLM { }); try { - return input.schema.parse(JSON.parse(response.message.content)) as T; + return input.schema.parse( + JSON.parse( + repairJson(response.message.content, { + extractJson: true, + }) as string, + ), + ) as T; } catch (err) { throw new Error(`Error parsing response from Ollama: ${err}`); } diff --git a/src/lib/models/providers/openai/openaiLLM.ts b/src/lib/models/providers/openai/openaiLLM.ts index dfad274..5ae1538 100644 --- a/src/lib/models/providers/openai/openaiLLM.ts +++ b/src/lib/models/providers/openai/openaiLLM.ts @@ -18,6 +18,7 @@ import { ChatCompletionToolMessageParam, } from 'openai/resources/index.mjs'; import { Message } from '@/lib/types'; +import { repairJson } from '@toolsycc/json-repair'; type OpenAIConfig = { apiKey: string; @@ -213,7 +214,13 @@ class OpenAILLM extends BaseLLM { if (response.choices && response.choices.length > 0) { try { - return input.schema.parse(response.choices[0].message.parsed) as T; + return input.schema.parse( + JSON.parse( + repairJson(response.choices[0].message.content!, { + extractJson: true, + }) as string, + ), + ) as T; } catch (err) { throw new Error(`Error parsing response from OpenAI: ${err}`); }