RAGGuardrail

org.llm4s.agent.guardrails.rag.RAGGuardrail
See theRAGGuardrail companion object

Base trait for RAG-specific guardrails that need retrieval context.

RAGGuardrail extends OutputGuardrail with additional context-aware validation methods. This allows guardrails to validate responses against the retrieved chunks from a RAG pipeline, enabling:

  • Grounding checks: Verify the response is supported by retrieved context
  • Context relevance: Check if retrieved chunks are relevant to the query
  • Source attribution: Ensure citations are accurate and present

RAG guardrails should implement validateWithContext for full RAG validation. The standard validate method provides a fallback for non-RAG usage.

Example usage:

// Create a grounding guardrail
val grounding = GroundingGuardrail(llmClient, threshold = 0.8)

// Validate with RAG context
val context = RAGContext(
 query = "What is photosynthesis?",
 retrievedChunks = Seq("Plants convert sunlight...", "Chlorophyll absorbs...")
)
grounding.validateWithContext("Photosynthesis converts light to energy", context)

Attributes

See also

GroundingGuardrail for factuality validation

ContextRelevanceGuardrail for chunk relevance checks

SourceAttributionGuardrail for citation verification

Companion
object
Graph
Supertypes
trait Guardrail[String]
class Object
trait Matchable
class Any
Known subtypes

Members list

Value members

Abstract methods

def validateWithContext(output: String, context: RAGContext): Result[String]

Validate an output with full RAG context.

Validate an output with full RAG context.

This is the primary validation method for RAG guardrails. It receives the response along with the original query and retrieved chunks, allowing for context-aware validation.

Value parameters

context

The RAG context containing query and retrieved chunks

output

The generated response to validate

Attributes

Returns

Right(output) if valid, Left(error) if validation fails

Concrete methods

def transformWithContext(output: String, context: RAGContext): String

Optional transformation with context.

Optional transformation with context.

Override to modify the output based on RAG context (e.g., add citations). Default implementation returns output unchanged.

Value parameters

context

The RAG context

output

The validated output

Attributes

Returns

The transformed output

Inherited methods

def andThen(other: Guardrail[String]): Guardrail[String]

Compose this guardrail with another sequentially.

Compose this guardrail with another sequentially.

The second guardrail runs only if this one passes.

Value parameters

other

The guardrail to run after this one

Attributes

Returns

A composite guardrail that runs both in sequence

Inherited from:
Guardrail
def description: Option[String]

Optional description of what this guardrail validates.

Optional description of what this guardrail validates.

Attributes

Inherited from:
Guardrail
def transform(output: String): String

Optional: Transform the output after validation. Default is identity (no transformation).

Optional: Transform the output after validation. Default is identity (no transformation).

Value parameters

output

The validated output

Attributes

Returns

The transformed output

Inherited from:
OutputGuardrail

Inherited and Abstract methods

def name: String

Name of this guardrail for logging and error messages.

Name of this guardrail for logging and error messages.

Attributes

Inherited from:
Guardrail
def validate(value: String): Result[String]

Validate a value.

Validate a value.

This is a PURE FUNCTION - no side effects allowed. Same input always produces same output.

Value parameters

value

The value to validate

Attributes

Returns

Right(value) if valid, Left(error) if invalid

Inherited from:
Guardrail