PartialJson

org.llm4s.schema.PartialJson
@Experimental
object PartialJson

A tolerant parser for the JSON a model has written so far: given a prefix of a JSON document, it closes the open strings, arrays and objects and returns the value the prefix describes.

What the prefix does not finish is left out rather than guessed:

  • a property whose value has not started, or whose key is still being written, is absent;
  • a number is surfaced only once a character after it shows that it is complete, so 12.5 never shows as 1;
  • true, false and null are surfaced only once complete;
  • a string still being written is surfaced as its prefix (without an escape that has not finished), or left out with partialStrings = false.

So the value for a prefix is always a prefix of the value for the whole document: every property it has, the whole document has with the same or a longer value. Text after the first value, or after JSON that is not valid, is ignored: the value is what came before it. Nesting deeper than 512 levels is cut there, as BoundedJson refuses it. The parser never throws.

client.streamStructuredOf[A] uses it to turn a streamed reply into typed partial values.

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Value members

Concrete methods

def parse(text: String, partialStrings: Boolean): Option[Value]

The value described by text, a prefix of a JSON document, or None when no value has started yet (empty or blank text, or a number or literal still being written at the root).

The value described by text, a prefix of a JSON document, or None when no value has started yet (empty or blank text, or a number or literal still being written at the root).

Value parameters

partialStrings

whether a string still being written is surfaced as its prefix (the default) or left out

text

the JSON written so far

Attributes