syntax

org.llm4s.syntax
@Stable
object syntax

The extension methods of llm4s, all in one object and none in scope until imported:

import org.llm4s.syntax.*          // all of them
import org.llm4s.syntax.toResult   // or one, by name

Try("42".toInt).toResult                         // Result[Int]
headers.get("x-request-id").toResult(NotFoundError("no request id", "x-request-id"))
new IllegalStateException("boom").toLLMError     // LLMError
result.recover { case _: RateLimitError => "fallback" }.tap(println)

import org.llm4s.types.* brings in none of them, so it does not add toResult to every Try, Option and Future in the file.

Attributes

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

Members list

Extensions

Extensions

extension [A](future: Future[A])

The future's value as a Right, or its failure as a Left, mapped by Safety.fromThrowable: the returned future never fails because the original did. This is the one Future to AsyncResult conversion.

The future's value as a Right, or its failure as a Left, mapped by Safety.fromThrowable: the returned future never fails because the original did. This is the one Future to AsyncResult conversion.

Attributes

Source
syntax.scala
extension [A](option: Option[A])
def toResult(ifEmpty: => LLMError): Result[A]

Right with the option's value, or Left(ifEmpty) for None.

Right with the option's value, or Left(ifEmpty) for None.

Value parameters

ifEmpty

by-name: evaluated only when the option is None

Attributes

Source
syntax.scala
extension [A](result: Result[A])
def mapError(f: LLMError => LLMError): Result[A]

Transforms the error value without changing the success type. Leaves Right values untouched.

Transforms the error value without changing the success type. Leaves Right values untouched.

Value parameters

f

transforms the LLMError; must return an LLMError

Attributes

Source
syntax.scala
def recover[B >: A](pf: PartialFunction[LLMError, B]): Result[B]

Converts a matching Left into a Right using the supplied partial function.

Converts a matching Left into a Right using the supplied partial function.

If the partial function is not defined for the error, the original Left is returned unchanged. Right values pass through without invoking pf.

Value parameters

pf

partial function from LLMError to a recovery value

Attributes

Source
syntax.scala
def recoverWith[B >: A](pf: PartialFunction[LLMError, Result[B]]): Result[B]

Converts a matching Left into a new Result using the supplied partial function.

Converts a matching Left into a new Result using the supplied partial function.

Useful when the recovery operation can itself fail. If the partial function is not defined for the error, the original Left is returned unchanged. Right values pass through without invoking pf.

Value parameters

pf

partial function from LLMError to a new Result[B]

Attributes

Source
syntax.scala
def tap(effect: A => Unit): Result[A]

Executes effect for its side effect when the result is a Right, then returns the original result unchanged. Useful for logging or metrics without breaking a for-comprehension chain.

Executes effect for its side effect when the result is a Right, then returns the original result unchanged. Useful for logging or metrics without breaking a for-comprehension chain.

Value parameters

effect

side-effecting function invoked on success; its return value is discarded

Attributes

Source
syntax.scala
def tapError(effect: LLMError => Unit): Result[A]

Executes effect for its side effect when the result is a Left, then returns the original result unchanged. Useful for logging errors without altering the failure path.

Executes effect for its side effect when the result is a Left, then returns the original result unchanged. Useful for logging errors without altering the failure path.

Value parameters

effect

side-effecting function invoked on failure; its return value is discarded

Attributes

Source
syntax.scala
extension [A](t: Try[A])
def toResult: Result[A]

The Try's value as a Right, or its exception as a Left, mapped by Safety.fromThrowable.

The Try's value as a Right, or its exception as a Left, mapped by Safety.fromThrowable.

Attributes

Source
syntax.scala
extension (t: Throwable)

This exception as an LLMError: Safety.fromThrowable.

This exception as an LLMError: Safety.fromThrowable.

Attributes

Source
syntax.scala