StreamedField

org.llm4s.schema.StreamedField
See theStreamedField companion object
@Experimental
final class StreamedField

One string field of a structured answer, read as the answer streams (#2921): feed it each piece of text as it arrives - an agent's AgentEvents.TextDelta, a client's StreamedChunk - and it returns what the field gained, so an application can show, say, a narration field as the model writes it without parsing incomplete JSON itself.

val narration = StreamedField("narration")
agent.stream(threadId, query) {
 case AgentEvents.ModelCallStarted(_) => narration.reset()
 case AgentEvents.TextDelta(d)        => print(narration.feed(d.text))
 case _                               => ()
}

The text so far is read with PartialJson, from its first { or [ (" for a string answer), so a code fence or prose before the JSON is skipped. An escape split across two pieces is held back until it is whole. The field's text only grows: what feed returns is always the text after what it returned before. A path of more than one key reads a nested field (StreamedField("scene", "description")); the field of a union answer is under its answer envelope.

'''A new answer.''' Each model call streams an answer of its own: a corrected answer after a failed output check, a retry, the next step after a tool call, the next turn. Call reset on every AgentEvents.ModelCallStarted, and clear what was shown then. (A delta's attempt cannot tell: each model call counts its attempts from 1.)

Each feed parses the text so far again, so the work for an answer is quadratic in its length: fine for an answer of a few tens of kilobytes. Not thread-safe: feed it from the one thread that receives the stream.

Value parameters

path

the keys from the answer's root to the string field

Attributes

Companion
object
Source
StreamedField.scala
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def feed(delta: String): String

Adds delta to the answer so far and returns what the field gained with it: empty when it gained nothing (the field has not started, is complete, or the delta was elsewhere in the answer).

Adds delta to the answer so far and returns what the field gained with it: empty when it gained nothing (the field has not started, is complete, or the delta was elsewhere in the answer).

Attributes

Source
StreamedField.scala

Whether the field's closing quote has arrived, so its text is final.

Whether the field's closing quote has arrived, so its text is final.

Attributes

Source
StreamedField.scala
def reset(): Unit

Forgets the answer so far, for a new answer.

Forgets the answer so far, for a new answer.

Attributes

Source
StreamedField.scala
def value: String

The field's text so far; empty before it starts.

The field's text so far; empty before it starts.

Attributes

Source
StreamedField.scala