From 1a5c4a2c28476900d41fa13a3ef3f49dbc9b976a Mon Sep 17 00:00:00 2001 From: berkcaputcu Date: Sat, 17 May 2025 13:32:25 -0400 Subject: [PATCH] fix(ollama): handle incomplete JSON chunks in stream Improve the `json_responses_chunk_handler` to gracefully handle cases where a JSON chunk is split across buffer boundaries. If a chunk does not end with '}', it is considered incomplete and buffered until the next chunk arrives. This prevents JSON parsing errors and ensures all responses are processed correctly. --- lib/langchain/llm/ollama.rb | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/langchain/llm/ollama.rb b/lib/langchain/llm/ollama.rb index 93870f5b8..302d5ebaf 100644 --- a/lib/langchain/llm/ollama.rb +++ b/lib/langchain/llm/ollama.rb @@ -287,10 +287,28 @@ def auth_headers end def json_responses_chunk_handler(&block) + incomplete_chunk_line = nil proc do |chunk, _size| chunk.split("\n").each do |chunk_line| - parsed_chunk = JSON.parse(chunk_line) - block.call(parsed_chunk) + if incomplete_chunk_line + chunk_line = incomplete_chunk_line + chunk_line + incomplete_chunk_line = nil + end + + parsed_chunk = begin + JSON.parse(chunk_line) + + # In some instance the chunk exceeds the buffer size and the JSON parser fails + rescue JSON::ParserError + unless chunk_line.end_with?("}") + incomplete_chunk_line = chunk_line + nil + else + raise + end + end + + block.call(parsed_chunk) unless parsed_chunk.nil? end end end