JsonReply

org.llm4s.llmconnect.JsonReply
@Stable
object JsonReply

The JSON in a model's reply, found the way llm4s itself finds it (#2952): LLMClient.completeStructured, an agent built with withOutput and AgentResult.decodeOutput all read a reply through this, so an application that reads one too - a middleware judging an answer before the agent accepts it, say - reads the same JSON they do.

A reply is read leniently, because models wrap JSON in prose and fences: a surrounding Markdown code fence is stripped and, when what is left is not JSON itself, the first balanced {...} or [...] block that parses is taken (string and escape aware), skipping earlier bracket pairs in the prose and anything after the JSON. At most 16 candidate starts are tried, so the cost stays linear in practice. A document nested more than 512 levels deep is refused: a value that deep overflows the stack of whatever reads it.

Attributes

Source
JsonReply.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
JsonReply.type

Members list

Value members

Concrete methods

def extract(reply: String): String

The JSON text in reply, trimmed; when none is found, the trimmed reply itself, so the caller that parses it reports why it is not JSON.

The JSON text in reply, trimmed; when none is found, the trimmed reply itself, so the caller that parses it reports why it is not JSON.

Attributes

Source
JsonReply.scala
def parse(reply: String): Result[Value]

The JSON value in reply, read as extract finds it. A reply with no JSON in it, or whose JSON is null, is a ValidationError for the field output whose one violation is the problem as an agent reports it to the model, such as $: the answer is not JSON: ....

The JSON value in reply, read as extract finds it. A reply with no JSON in it, or whose JSON is null, is a ValidationError for the field output whose one violation is the problem as an agent reports it to the model, such as $: the answer is not JSON: ....

Attributes

Source
JsonReply.scala