trulens.core.utils.serial¶
trulens.core.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
¶
JSON-encoded data the can be deserialized into a given type T
.
This class is meant only for type annotations. Any
serialization/deserialization logic is handled by different classes, usually
subclasses of pydantic.BaseModel
.
Functions¶
__get_pydantic_core_schema__
classmethod
¶
__get_pydantic_core_schema__(
source_type: Any, handler: GetCoreSchemaHandler
) -> CoreSchema
Make pydantic treat this class same as a dict
.
Step
¶
GetAttribute
¶
Bases: StepItemOrAttribute
An attribute lookup step as in someobject.someattribute
.
GetItem
¶
Bases: StepItemOrAttribute
An item lookup step as in someobject["somestring"]
.
GetItemOrAttribute
¶
Bases: StepItemOrAttribute
A step in a path lens that selects an item or an attribute.
Note
TruLens allows looking up elements within sequences if the subelements have the item or attribute. We issue warning if this is ambiguous (looking up in a sequence of more than 1 element).
SerialModel
¶
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.
LensedDict
¶
Functions¶
is_strict_json
¶
Determine if the given object is JSON-able, strictly.
Strict JSON starts as a dictionary at the root.
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.