SchemaOf

org.llm4s.schema.SchemaOf
See theSchemaOf companion object
@Experimental
trait SchemaOf[A]

The JSON schema of A, derived from the type itself so that it cannot drift from it.

case class Invoice(vendor: String, amount: Double, lines: List[String], note: Option[String]) derives SchemaOf

SchemaOf[Invoice].toJsonSchema()    // the schema `completeStructured` sends

The schema describes what uPickle's derived ReadWriter reads and writes (field names, Option as the value or null, an enum as its case name, a BigDecimal as a string), so a reply that matches the schema reads back into A. The module's tests check that round trip for every supported shape.

Supported: String, Int, Long, Double, Float, Boolean, BigDecimal, BigInt; Option; List, Seq, Vector and Set of a supported type; case classes whose fields are supported; enums and sealed hierarchies of singletons (as a string with the case names, or a case's @upickle.implicits.key); and sum types, enums and sealed traits whose cases have fields (as a JSON Schema anyOf of one object per case, each with the discriminator uPickle writes: "$type", or the trait's @upickle.implicits.key, holding the case's tag); and named tuples, (city: String, days: Int), as an object with one property per element (see SchemaOf.namedTuple). Not supported, and refused at compile time with a message naming the type: Map (core's ObjectSchema has a fixed set of properties and cannot describe map values), recursive types (they need a JSON Schema $ref), a sealed hierarchy nested in another or with a generic case, and Option directly inside Option and tuples without names (uPickle writes both as JSON arrays). Other types need a given SchemaOf of their own: see SchemaOf.string and SchemaOf.stringEnum. A hand-written given is used as it is, so it can also describe a type the derivation would refuse.

A recursion that runs through hand-written givens which themselves call SchemaOf.derived (for mutually recursive P and Q, given SchemaOf[P] = SchemaOf.derived[P] and given SchemaOf[Q] = SchemaOf.derived[Q]) cannot be seen at compile time, because the check does not look inside a hand-written given. Building that schema (schema, definition, toJsonSchema) throws an IllegalStateException naming the recursive type instead, on first use; it never hangs.

A generic case class may be nested in itself at most 16 deep: W[W[...[Int]]] with 16 Ws is described, with 17 it is taken for a recursion. Spelled out in a field's type, the deeper nesting is refused at compile time; reached through the generic derives SchemaOf instance (SchemaOf[W[...[Int]]]) it is refused when the schema is built, with the same IllegalStateException.

A sum type with fields is core's AnyOfSchema: one alternative per case. A tool's arguments may hold one, but cannot be one, since a tool's arguments are a JSON object.

Type parameters

A

the described type

Attributes

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

Members list

Value members

Abstract methods

def schema(description: String): SchemaDefinition[A]

The schema, described to the model by description.

The schema, described to the model by description.

Attributes

Throws
java.lang.IllegalStateException

for a derived instance whose type turns out to be recursive, or nests a generic case class in itself more than 16 deep (see SchemaOf)

Source
SchemaOf.scala

The name of the type, used to describe it when it has no description.

The name of the type, used to describe it when it has no description.

Attributes

Source
SchemaOf.scala

Concrete methods

The description of the type when nothing more specific is given.

The description of the type when nothing more specific is given.

Attributes

Source
SchemaOf.scala

The schema of A as core's schema model, described by the type's description or name: what completeStructuredOf sends, and what a tool takes as its parameters (ToolBuilder[R](name, text, SchemaOf[A].definition)).

The schema of A as core's schema model, described by the type's description or name: what completeStructuredOf sends, and what a tool takes as its parameters (ToolBuilder[R](name, text, SchemaOf[A].definition)).

Attributes

Throws
Source
SchemaOf.scala

Whether a field of this type may be absent or null: true for Option.

Whether a field of this type may be absent or null: true for Option.

Attributes

Source
SchemaOf.scala
def jsonSchema(description: String, strict: Boolean): Value

The JSON Schema, described to the model by description: schema(description).toJsonSchema(strict).

The JSON Schema, described to the model by description: schema(description).toJsonSchema(strict).

Value parameters

strict

as in toJsonSchema

Attributes

Throws
Source
SchemaOf.scala
final def toJsonSchema(strict: Boolean): Value

The JSON Schema document, for every derived type including sum types with fields.

The JSON Schema document, for every derived type including sum types with fields.

Value parameters

strict

as in SchemaDefinition.toJsonSchema: true marks every property required, which is what completeStructured sends

Attributes

Throws
Source
SchemaOf.scala

The type's own description, if it has one.

The type's own description, if it has one.

Attributes

Source
SchemaOf.scala