promplate / partial-json-parser

Parse partial JSON generated by LLM
https://promplate.dev/partial-json-parser
42 stars 1 forks source link

fail when exist `\r` / `\t` #6

Closed CNSeniorious000 closed 8 months ago

CNSeniorious000 commented 9 months ago
>>> from partial_json_parser import *
>>> parse_json("[0 \t\n , 1")
Traceback (most recent call last):
  File "...\Lib\site-packages\partial_json_parser\__init__.py", line 155, in parse_num
    return literal_eval(json_string[start:index])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\Python\Python311\Lib\ast.py", line 64, in literal_eval
    node_or_string = parse(node_or_string.lstrip(" \t"), mode='eval')
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\Python\Python311\Lib\ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<unknown>", line 2

IndentationError: unexpected indent

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "...\Lib\site-packages\partial_json_parser\__init__.py", line 15, in parse_json
    return _parse_json(json_string.strip(), Allow(allow_partial))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\Lib\site-packages\partial_json_parser\__init__.py", line 168, in _parse_json
    return parse_any()
           ^^^^^^^^^^^
  File "...\Lib\site-packages\partial_json_parser\__init__.py", line 46, in parse_any
    return parse_arr()
           ^^^^^^^^^^^
  File "...\Lib\site-packages\partial_json_parser\__init__.py", line 123, in parse_arr
    arr.append(parse_any())
               ^^^^^^^^^^^
  File "...\Lib\site-packages\partial_json_parser\__init__.py", line 65, in parse_any
    return parse_num()
           ^^^^^^^^^^^
  File "...\Lib\site-packages\partial_json_parser\__init__.py", line 159, in parse_num
    return literal_eval(json_string[start : json_string.rindex("e", index - 2)])
                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: substring not found
CNSeniorious000 commented 9 months ago

[!warning]

  1. The error message is ugly
  2. The same problem occurs in the JavaScript package (#1)
  3. There exists several useless skip_blank call in the code (at least removing them won't cause the tests to fail, which is bad)