Overview
The OutputTypes enumeration defines the three possible output types for Guard validation results: STRING, LIST, and DICT.
Description
This module provides the OutputTypes enum which extends both str and Enum. It is used throughout the Guardrails framework to classify the expected shape of validated LLM output. The enum provides:
- Three members:
STRING ("str"), LIST ("list"), and DICT ("dict").
get static method: A safe accessor that returns the matching OutputTypes member or a default value, handling None and invalid keys gracefully.
__from_json_schema__ class method: Derives the output type from a JSON Schema definition by inspecting the type, allOf, oneOf, and anyOf fields. Defaults to STRING as a fallback.
The module also defines the OT TypeVar (TypeVar("OT", str, List, Dict)) for generic typing of output values.
Usage
Use OutputTypes whenever you need to determine or specify the expected output format for a Guard. It is used by action handlers (like apply_refrain) to produce correctly typed empty values, and by the core validation pipeline to route output processing.
Code Reference
Source Location
- Repository: Guardrails
- File:
guardrails/classes/output_type.py
- Lines: 1-63
Signature
OT = TypeVar("OT", str, List, Dict)
class OutputTypes(str, Enum):
STRING = "str"
LIST = "list"
DICT = "dict"
@staticmethod
def get(key: Optional[Union[str, "OutputTypes"]], default=None):
@classmethod
def __from_json_schema__(cls, json_schema: Dict[str, Any]) -> "OutputTypes":
Import
from guardrails.classes.output_type import OutputTypes, OT
I/O Contract
Enum Members
| Member |
Value |
Description
|
STRING |
"str" |
Output is a plain string.
|
LIST |
"list" |
Output is a list.
|
DICT |
"dict" |
Output is a dictionary.
|
get
| Parameter |
Type |
Description
|
key |
Optional[Union[str, OutputTypes]] |
The key to look up. Can be a string name, an OutputTypes member, or None.
|
default |
Any |
Value to return if the key does not match any member. Defaults to None.
|
| Return Type |
Description
|
Optional[OutputTypes] |
The matching OutputTypes member, or the default value.
|
__from_json_schema__
| Parameter |
Type |
Description
|
json_schema |
Dict[str, Any] |
A JSON Schema dictionary to derive the output type from.
|
| Return Type |
Description
|
OutputTypes |
The inferred output type based on the schema's type, allOf, oneOf, or anyOf fields. Falls back to STRING.
|
JSON Schema Mapping
| JSON Schema Type |
OutputTypes Result
|
string |
STRING
|
object |
DICT
|
array |
LIST
|
Has allOf |
DICT
|
Has oneOf |
Recursively resolves first sub-schema
|
Has anyOf |
Recursively resolves first sub-schema
|
| (fallback) |
STRING
|
Usage Examples
from guardrails.classes.output_type import OutputTypes
# Direct member access
output_type = OutputTypes.DICT
print(output_type.value) # "dict"
# Safe lookup with get
output_type = OutputTypes.get("STRING")
# output_type: OutputTypes.STRING
output_type = OutputTypes.get(None, default=OutputTypes.STRING)
# output_type: OutputTypes.STRING
# Derive from JSON Schema
schema = {"type": "object", "properties": {"name": {"type": "string"}}}
output_type = OutputTypes.__from_json_schema__(schema)
# output_type: OutputTypes.DICT
# Array schema
schema = {"type": "array", "items": {"type": "string"}}
output_type = OutputTypes.__from_json_schema__(schema)
# output_type: OutputTypes.LIST
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.