anthropic4s
GitHub

Reference Structured data

Schema[A]

The JSON Schema of a type with its circe codec. parse and Tool use it; case classes and sealed traits get one automatically.

Reference#

Schema[A]#

schema · Schema.scala
trait Schema[A]
def node: SchemaNode
def decoder: Decoder[A]
def encoder: Encoder[A]
def jsonSchema: JsonObject
def strict: ValidatedNel[SchemaIssue, JsonObject]
def emap[B](f: A => Either[String, B])(g: B => A): Schema[B]
def imap[B](f: A => B)(g: B => A): Schema[B]
def withCodec(d: Decoder[A], e: Encoder[A]): Schema[A]

Derivation#

schema · SchemaDerivation.scala
implicit def derived[A]: Schema[A]

Caveats

Annotations#

schema · SchemaNode.scala
final class description(val text: String) extends StaticAnnotation
final class name(val value: String) extends StaticAnnotation
final class discriminator(val field: String) extends StaticAnnotation

Hand-written schemas#

schema · Schema.scala
def apply[A](implicit schema: Schema[A]): Schema[A]
def instance[A](n: SchemaNode, d: Decoder[A], e: Encoder[A]): Schema[A]
def unchecked[A](schema: JsonObject)(implicit d: Decoder[A], e: Encoder[A]): Schema[A]

Caveats

Usage#

Describing a type#

schema · Schema.scala
final case class Book(@description("Full title") title: String, year: Option[Int], genre: Genre)

client.messages.parse[Book](request)   // no instance to declare

With Play JSON formats, use PlaySchema instead.

Every signature and example on this page is copied, character for character, from the public source of anthropic4s and its Scaladoc. The complete API, every type included, is in the Scaladoc of each module.