Module: LLMs::Parsers::PartialJsonParser

Included in:
AnthropicChatResponseStreamParser, SSEChatResponseStreamParser
Defined in:
lib/llms/parsers/partial_json_parser.rb

Instance Method Summary collapse

Instance Method Details

#attempt_parse_json(json) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/llms/parsers/partial_json_parser.rb', line 6

def attempt_parse_json(json)
  parsed = nil
  corrected = false

  begin
    parsed = JSON.parse(json)
  rescue JSON::ParserError
    # Track unclosed delimiters
    unclosed = []
    in_string = false
    escape_next = false

    json.each_char.with_index do |char, i|
      if escape_next
        escape_next = false
        next
      end

      case char
      when '\\'
        escape_next = true
      when '"'
        unless escape_next
          if in_string
            if unclosed.last == :quote
              unclosed.pop
            end
            in_string = false
          else
            unclosed.push(:quote)
            in_string = true
          end
        end
      when '{'
        unclosed.push(:brace) unless in_string
      when '['
        unclosed.push(:bracket) unless in_string
      when '}'
        if !in_string && unclosed.last == :brace
          unclosed.pop
        end
      when ']'
        if !in_string && unclosed.last == :bracket
          unclosed.pop
        end
      end
    end

    # Build correction by closing delimiters in reverse order
    correction = unclosed.reverse.map do |type|
      case type
      when :quote then '"'
      when :brace then '}'
      when :bracket then ']'
      end
    end.join

    # Try parsing with correction
    begin
      corrected = true
      corrected_json = json + correction
      parsed = JSON.parse(corrected_json)
    rescue JSON::ParserError
      parsed = nil
    end
  end

  [parsed, corrected]
end