trulens.feedback.schema_validator¶
trulens.feedback.schema_validator
¶
Classes¶
SchemaValidator
¶
Bases: WithClassInfo, SerialModel
Non-LLM feedback functions for validating LLM output against a schema.
Accepts either a JSON schema dict (requires jsonschema) or a Pydantic
model class. Each method returns 1.0 when the output is valid and 0.0
otherwise, along with a metadata dict that contains any validation errors.
Example β JSON schema dict:
from trulens.feedback.schema_validator import SchemaValidator
from trulens.core.metric.metric import Metric
schema = {
"type": "object",
"properties": {"answer": {"type": "string"}},
"required": ["answer"],
}
validator = SchemaValidator(schema=schema)
f = Metric(validator.validate_json).on_output()
Example β Pydantic model:
import pydantic
from trulens.feedback.schema_validator import SchemaValidator
from trulens.core.metric.metric import Metric
class MyOutput(pydantic.BaseModel):
answer: str
score: float
validator = SchemaValidator(schema=MyOutput)
f = Metric(validator.validate_json).on_output()
Attributes¶
tru_class_info
instance-attribute
¶
tru_class_info: Class
Class information of this pydantic object for use in deserialization.
Using this odd key to not pollute attribute names in whatever class we mix this into. Should be the same as CLASS_INFO.
Functions¶
__init__
¶
__init__(schema: _SchemaType, **kwargs)
Create a SchemaValidator.
| PARAMETER | DESCRIPTION |
|---|---|
schema
|
Either a JSON schema dict or a Pydantic
TYPE:
|
validate_json
¶
Validate that output is valid JSON conforming to the schema.
Returns 1.0 when valid, 0.0 otherwise. The accompanying
metadata dict always contains an "explanation" key describing the
outcome (or the first validation error).
| PARAMETER | DESCRIPTION |
|---|---|
output
|
The string to validate. It must be parseable as JSON.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
A |
dict[str, str]
|
infrastructure. |
validate_json_partial
¶
validate_json_partial(
output: str, required_keys: list | None = None
) -> tuple[float, dict[str, str]]
Validate that output is valid JSON and optionally check for keys.
This is a lighter-weight check: it verifies that output parses as a JSON object and, when required_keys is provided, that each key is present at the top level. The full schema is not consulted, making this useful for streaming or partial outputs.
| PARAMETER | DESCRIPTION |
|---|---|
output
|
The string to validate.
TYPE:
|
required_keys
|
Optional list of keys that must exist at the top level of the parsed object.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[float, dict[str, str]]
|
A |
__repr__
¶
__repr__() -> str
Safe repr that handles circular references.
Pydantic's default __repr__ does not guard against
circular references among model instances, which leads to
RecursionError (see GitHub issue #1862). This override
uses the same formatted_objects context-variable that
__rich_repr__ uses so that already-visited objects are
replaced with a short placeholder instead of recursing
infinitely.
load
staticmethod
¶
load(obj, *args, **kwargs)
Deserialize/load this object using the class information in tru_class_info to lookup the actual class that will do the deserialization.
model_validate
classmethod
¶
model_validate(*args, **kwargs) -> Any
Deserialized a jsonized version of the app into the instance of the class it was serialized from.
Note
This process uses extra information stored in the jsonized object and handled by WithClassInfo.