Oh fun, I wrote a similar library in 2015 for Haskell. There is an annoying gotcha to deal with: there are sequences of valid characters that can be parsed incorrectly if you’re doing incremental chunks, namely if “0.0” is split across two input chunks you can get a token stream with two valid float literals rather than 1! Namely “0” and “.0”, which is just a really annoying wart of json float syntax.
Don't you need to wait for some kind of delimiter (like ",", "]", "}", newline, EOF) before parsing something else than a string?
JSON River – Parse JSON incrementally as it streams in
71–80 of 101 posts
Re: JSON River – Parse JSON incrementally as it streams in
#72Hi HN! Didn't expect this to be on the front page today! I should really release all the optimizations that've been landing lately, the version on github is about twice as fast as what's released on npm. I wrote it when I was doing prototyping on doing streaming rendering of UIs defined by JSON generated by LLMs. Using constrained generation you can essentially hand the model a JSON serializable type, and it will alw…
I've just published v1.0.1. It's about 2x faster, and should have no other observable changes. The speedup is mainly from avoiding allocation and string slicing as much as possible, plus an internal refactor to bind the parser and tokenizer more tightly together. Previously the parser would get an array of tokens each time it pushed data into the tokenizer. This was easy to write, but it meant we needed to allocate t…
Re: JSON River – Parse JSON incrementally as it streams in
#73Earlier quoted context omitted.
Doesn't it do exactly that? > As a consequence of 1 and 5, we only add a property to an object once we have the entire key and enough of the value to know that value's type.
Their example in the README is extremely misleading then. It indicates your stream output is name: A name: Al name: Ale name: Alex Which would suggest you are getting unfinished strings out in the stream.
Re: JSON River – Parse JSON incrementally as it streams in
#74Earlier quoted context omitted.
If you're building a UI that renders output from a streaming LLM you might get back something which looks like this: {"role": "assistant", "text": "Here's that Python code you aske Incomplete parsing with incomplete strings is still useful in order to render that to your end user while it's still streaming in.
incomplete strings could be fun in certain cases {"cleanup_cmd":"rm -rf /home/foo/.tmp" }
[1]: https://github.com/globalaiplatform/langdiff/tree/main/ts
Re: JSON River – Parse JSON incrementally as it streams in
#75Earlier quoted context omitted.
Doesn't it do exactly that? > As a consequence of 1 and 5, we only add a property to an object once we have the entire key and enough of the value to know that value's type.
Their example in the README is extremely misleading then. It indicates your stream output is name: A name: Al name: Ale name: Alex Which would suggest you are getting unfinished strings out in the stream.
Re: JSON River – Parse JSON incrementally as it streams in
#76The title made me think of Star Trek DS9 and Nog talking about The Great Material Continuum.
“Nog: The river will provide”
Re: JSON River – Parse JSON incrementally as it streams in
#77Earlier quoted context omitted.
I've just published v1.0.1. It's about 2x faster, and should have no other observable changes. The speedup is mainly from avoiding allocation and string slicing as much as possible, plus an internal refactor to bind the parser and tokenizer more tightly together. Previously the parser would get an array of tokens each time it pushed data into the tokenizer. This was easy to write, but it meant we needed to allocate t…
I want to ditch stream-json so hard (needs polyfills in browser, cumbersome to use), but I need only one feature: invoke callback by path (e.g. `user.posts` need to invoke for each post in array) only for complete objects. Is this something that json river can support?
There might be room for some helper functions in something like a 'jsonriver/helpers.js' module. I'll poke around at it.
Re: JSON River – Parse JSON incrementally as it streams in
#78This looks really nice. I think I could find a use for this. The title made me think of Star Trek DS9 and Nog talking about The Great Material Continuum. “Nog: The river will provide”
Re: JSON River – Parse JSON incrementally as it streams in
#79- trim off all trailing delimiters: },"
- then add on a fixed suffix: "]}
- then try parsing as a standard json. Ignore results if fails to parse.
This works since the schema I’m parsing had a fairly simple structure where everything of interest was at a specific depth in the hierarchy and values were all strings.
Re: JSON River – Parse JSON incrementally as it streams in
#80We used the streaming parser to create an index of the file locally {json key: (byte offset, byte size)} and then simply used http range queries to access the data we needed.
Here is the full write up about it:
https://dinesh.cloud/2022/streaming-json-for-fun-and-profit/
And here is the open sourced code: