partial-json-parser-js icon indicating copy to clipboard operation
partial-json-parser-js copied to clipboard

handling of markdown json

Open flazouh opened this issue 1 year ago • 3 comments

sometimes openai returns ```json { "key": "value" } ``` does this lib handles this use case ?

flazouh avatar Nov 20 '24 15:11 flazouh

Nope. But there are two workarounds:

  • Use json mode.
  • Detect JSON boundary using regex like
    ```json\n(.+?)(?:\n```|`{0,3}\Z)
    

CNSeniorious000 avatar Nov 20 '24 16:11 CNSeniorious000

ok thanks you, nice lib btw. was gonna implement it myself and thought someone must have had the idea already

flazouh avatar Nov 20 '24 22:11 flazouh

You can find the first { symbol and the last } in the response. If } is not found or does not match {, then you can determine that the JSON is partial and can be handled by this library.

yar2001 avatar Dec 12 '24 03:12 yar2001