anthropic4s
GitHub

Reference Messages

messages.parse[A]

Asks for a reply shaped like A and decodes it (structured outputs).

Reference#

parse[A](request, options?)#

core · AnthropicClient.scala
def parse[A](request: MessageRequest, options: RequestOptions)(implicit
    schema: Schema[A],
    F: MonadThrow[F]
): F[Parsed[A]]
def parse[A](request: MessageRequest)(implicit schema: Schema[A], F: MonadThrow[F]): F[Parsed[A]]

Parameters

Returns

core · AnthropicClient.scala
final case class Parsed[A] private[anthropic4s] (value: A, message: Message)

The decoded value, with the message it came from (usage, stop reason, id).

Caveats

Usage#

A typed reply#

core · AnthropicClient.scala
final case class Invoice(@description("ISO 4217 code") currency: String, total: BigDecimal)

client.messages.parse[Invoice](request).map(_.value.total)
README.md
import anthropic4s.schema.description

sealed trait Difficulty
object Difficulty { case object Easy extends Difficulty; case object Hard extends Difficulty }

final case class Recipe(
    name: String,
    @description("Total time in minutes") minutes: Int,
    difficulty: Difficulty,
    pairing: Option[String]
)

client.messages.parse[Recipe](request).map(_.value)   // IO[Recipe]

Structured outputs explains the schema rules.

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.