Overview
The BaseFormatter abstract class defines the interface for wrapping LLM callables with input/output manipulation logic, and PassthroughFormatter provides a no-op implementation.
Description
This module provides the formatter abstraction layer for Guardrails. A formatter takes an LLM callable and wraps it, enabling pre-processing of inputs or post-processing of outputs before they reach the validation pipeline. This is used for techniques such as JSON constrained-decoding.
Two classes are defined:
BaseFormatter (ABC): Declares two abstract methods:
wrap_callable: Wraps a synchronous PromptCallableBase into an ArbitraryCallable.
wrap_async_callable: Wraps a synchronous PromptCallableBase into an AsyncPromptCallableBase for async execution contexts.
PassthroughFormatter: A concrete implementation that returns the original callable unchanged (no-op). This is the default formatter when no special input/output manipulation is needed.
Usage
Use BaseFormatter as the base class when implementing custom formatters that need to modify LLM inputs or outputs (e.g., constraining the LLM to produce valid JSON). Use PassthroughFormatter as the default when no formatting is required.
Code Reference
Source Location
- Repository: Guardrails
- File:
guardrails/formatters/base_formatter.py
- Lines: 1-32
Signature
class BaseFormatter(ABC):
@abstractmethod
def wrap_callable(self, llm_callable: PromptCallableBase) -> ArbitraryCallable: ...
@abstractmethod
def wrap_async_callable(
self, llm_callable: PromptCallableBase
) -> AsyncPromptCallableBase: ...
class PassthroughFormatter(BaseFormatter):
def wrap_callable(self, llm_callable: PromptCallableBase):
return llm_callable # Noop
def wrap_async_callable(self, llm_callable: PromptCallableBase):
return llm_callable # Noop
Import
from guardrails.formatters.base_formatter import BaseFormatter, PassthroughFormatter
I/O Contract
wrap_callable
| Parameter |
Type |
Description
|
llm_callable |
PromptCallableBase |
The synchronous LLM callable to wrap.
|
| Return Type |
Description
|
ArbitraryCallable |
The wrapped callable with any input/output transformations applied.
|
wrap_async_callable
| Parameter |
Type |
Description
|
llm_callable |
PromptCallableBase |
The LLM callable to wrap for async execution.
|
| Return Type |
Description
|
AsyncPromptCallableBase |
The wrapped async callable with any input/output transformations applied.
|
PassthroughFormatter Behavior
| Method |
Behavior
|
wrap_callable |
Returns the input llm_callable unchanged.
|
wrap_async_callable |
Returns the input llm_callable unchanged.
|
Usage Examples
from guardrails.formatters.base_formatter import BaseFormatter, PassthroughFormatter
from guardrails.llm_providers import PromptCallableBase, ArbitraryCallable
# Using the PassthroughFormatter (no-op)
formatter = PassthroughFormatter()
original_callable = my_llm_callable # some PromptCallableBase instance
wrapped = formatter.wrap_callable(original_callable)
# wrapped is the same object as original_callable
# Implementing a custom formatter
class JsonConstrainedFormatter(BaseFormatter):
def wrap_callable(self, llm_callable: PromptCallableBase) -> ArbitraryCallable:
# Add JSON constrained decoding logic
def wrapped_fn(*args, **kwargs):
result = llm_callable(*args, **kwargs)
# Post-process to ensure valid JSON
return result
return ArbitraryCallable(wrapped_fn)
def wrap_async_callable(self, llm_callable: PromptCallableBase):
# Async version of the wrapper
async def wrapped_fn(*args, **kwargs):
result = await llm_callable(*args, **kwargs)
return result
return wrapped_fn
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.