diff --git a/lib/langchain/llm/ollama.rb b/lib/langchain/llm/ollama.rb index a0d902cb5..ed8c6b180 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 + if chunk_line.end_with?("}") + raise + else + incomplete_chunk_line = chunk_line + nil + end + end + + block.call(parsed_chunk) unless parsed_chunk.nil? end end end diff --git a/lib/langchain/tool/weather.rb b/lib/langchain/tool/weather.rb index ad9d8dfc9..1a3564810 100644 --- a/lib/langchain/tool/weather.rb +++ b/lib/langchain/tool/weather.rb @@ -58,6 +58,11 @@ def fetch_current_weather(city:, state_code:, country_code:, units:) return tool_response(content: location_response) if location_response.is_a?(String) # Error occurred location = location_response.first + if !location && country_code.nil? + return tool_response(content: "Location not found. Please provide a valid country code too and try again.") + else + return tool_response(content: "Location not found") unless location + end return tool_response(content: "Location not found") unless location params = params.merge(lat: location["lat"], lon: location["lon"]).except(:q) diff --git a/lib/langchain/version.rb b/lib/langchain/version.rb index debbf1ab5..04890e95c 100644 --- a/lib/langchain/version.rb +++ b/lib/langchain/version.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true module Langchain - VERSION = "0.19.5" + VERSION = "0.19.5-fix" Version = VERSION end diff --git a/spec/lib/langchain/tool/weather_spec.rb b/spec/lib/langchain/tool/weather_spec.rb index fea14a7c1..df01382f9 100644 --- a/spec/lib/langchain/tool/weather_spec.rb +++ b/spec/lib/langchain/tool/weather_spec.rb @@ -55,7 +55,7 @@ it "returns an error message" do result = weather_tool.get_current_weather(city: city, state_code: state_code) expect(result).to be_a(Langchain::ToolResponse) - expect(result.content).to eq("Location not found") + expect(result.content).to start_with("Location not found") end end