Serialization Utilities¶
trulens_eval.utils.serial
¶
Serialization utilities.
TODO: Lens class: can we store just the python AST instead of building up our own "Step" classes to hold the same data? We are already using AST for parsing.
Attributes¶
JSON_BASES
module-attribute
¶
Tuple of JSON-able base types.
Can be used in isinstance
checks.
JSON_BASES_T
module-attribute
¶
Alias for JSON-able base types.
JSON
module-attribute
¶
Alias for (non-strict) JSON-able data (Any
= JSON
).
If used with type argument, that argument indicates what the JSON represents and can be desererialized into.
Formal JSON must be a dict
at the root but non-strict here means that the root
can be a basic type or a sequence as well.
JSON_STRICT
module-attribute
¶
Alias for (strictly) JSON-able data.
Python object that is directly mappable to JSON.
Classes¶
JSONized
¶
SerialModel
¶
Bases: BaseModel
Trulens-specific additions on top of pydantic models. Includes utilities to help serialization mostly.
Step
¶
Lens
¶
Bases: BaseModel
, Sized
, Hashable
Lenses into python objects.
Example
path = Lens().record[5]['somekey']
obj = ... # some object that contains a value at `obj.record[5]['somekey]`
value_at_path = path.get(obj) # that value
new_obj = path.set(obj, 42) # updates the value to be 42 instead
collect
and special attributes¶
Some attributes hold special meaning for lenses. Attempting to access them will produce a special lens instead of one that looks up that attribute.
Example
path = Lens().record[:]
obj = dict(record=[1, 2, 3])
value_at_path = path.get(obj) # generates 3 items: 1, 2, 3 (not a list)
path_collect = path.collect()
value_at_path = path_collect.get(obj) # generates a single item, [1, 2, 3] (a list)
Functions¶
existing_prefix
¶
Get the Lens representing the longest prefix of the path that exists in the given object.
of_string
staticmethod
¶
Convert a string representing a python expression into a Lens.
set_or_append
¶
If obj
at path self
is None or does not exist, sets it to a list
containing only the given val
. If it already exists as a sequence,
appends val
to that sequence as a list. If it is set but not a sequence,
error is thrown.
Functions¶
model_dump
¶
Return the dict/model_dump of the given pydantic instance regardless of it being v2 or v1.
leaf_queries
¶
Get all queries for the given object that select all of its leaf values.
all_queries
¶
Get all queries for the given object.