Skip to content

Serde

serde

(De/re)serialization interfaces for saving Cost Model objects to file and loading them back

ISerializable

Bases: Protocol

from_dict classmethod

from_dict(**kwargs)

Load an instance of this class from a JSON-able dictionary representation

Source code in llmeter/callbacks/cost/serde.py
26
27
28
29
@classmethod
def from_dict(cls, **kwargs) -> TSerializable:
    """Load an instance of this class from a JSON-able dictionary representation"""
    ...

to_dict

to_dict()

Represent this object with a fully JSON-serializable dictionary

Source code in llmeter/callbacks/cost/serde.py
22
23
24
def to_dict(self) -> dict:
    """Represent this object with a fully JSON-serializable dictionary"""
    ...

JSONableBase

A base class for speeding up implementation of JSON-serializable objects

from_dict classmethod

from_dict(raw, alt_classes={}, **kwargs)

Initialize an instance of this class from a plain dict (with optional extra kwargs)

Parameters:

Name Type Description Default
raw dict

A plain Python dict, for example loaded from a JSON file

required
alt_classes Dict[str, TJSONable]

By default, this method will only use the class of the current object (i.e. cls). If you want to support loading of subclasses, provide a mapping from your raw dict's _type field to class, for example {cls.__name__: cls}.

{}
**kwargs Any

Optional extra keyword arguments to pass to the constructor

{}
Source code in llmeter/callbacks/cost/serde.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
@classmethod
def from_dict(
    cls: Type[TJSONable],
    raw: dict,
    alt_classes: Dict[str, TJSONable] = {},
    **kwargs: Any,
) -> TJSONable:
    """Initialize an instance of this class from a plain dict (with optional extra kwargs)

    Args:
        raw: A plain Python dict, for example loaded from a JSON file
        alt_classes: By default, this method will only use the class of the current object
            (i.e. `cls`). If you want to support loading of subclasses, provide a mapping
            from your raw dict's `_type` field to class, for example `{cls.__name__: cls}`.
        **kwargs: Optional extra keyword arguments to pass to the constructor
    """
    if alt_classes:
        return from_dict_with_class_map(
            raw=raw,
            class_map={cls.__name__: cls, **alt_classes},
            **kwargs,
        )
    else:
        return from_dict_with_class(raw=raw, cls=cls, **kwargs)

from_file classmethod

from_file(input_path, **kwargs)

Initialize an instance of this class from a (local or Cloud) JSON file Args: input_path: The path to the JSON data file. **kwargs: Optional extra keyword arguments to pass to from_dict()

Source code in llmeter/callbacks/cost/serde.py
146
147
148
149
150
151
152
153
154
155
@classmethod
def from_file(cls: Type[TJSONable], input_path: os.PathLike, **kwargs) -> TJSONable:
    """Initialize an instance of this class from a (local or Cloud) JSON file
    Args:
        input_path: The path to the JSON data file.
        **kwargs: Optional extra keyword arguments to pass to `from_dict()`
    """
    input_path = Path(input_path)
    with input_path.open("r") as f:
        return cls.from_json(f.read(), **kwargs)

from_json classmethod

from_json(json_string, **kwargs)

Initialize an instance of this class from a JSON string (with optional extra kwargs)

Parameters:

Name Type Description Default
json_string str

A string containing valid JSON data

required
**kwargs Any

Optional extra keyword arguments to pass to `from_dict()``

{}
Source code in llmeter/callbacks/cost/serde.py
157
158
159
160
161
162
163
164
165
@classmethod
def from_json(cls: Type[TJSONable], json_string: str, **kwargs: Any) -> TJSONable:
    """Initialize an instance of this class from a JSON string (with optional extra kwargs)

    Args:
        json_string: A string containing valid JSON data
        **kwargs: Optional extra keyword arguments to pass to `from_dict()``
    """
    return cls.from_dict(json.loads(json_string), **kwargs)

to_dict

to_dict(**kwargs)

Save the state of the object to a JSON-dumpable dictionary (with optional extra kwargs)

Implementers of this method should ensure that the returned dict is fully JSON-compatible: Mapping any child fields from Python classes to dicts if necessary, avoiding any circular references, etc.

Source code in llmeter/callbacks/cost/serde.py
167
168
169
170
171
172
173
174
def to_dict(self, **kwargs) -> dict:
    """Save the state of the object to a JSON-dumpable dictionary (with optional extra kwargs)

    Implementers of this method should ensure that the returned dict is fully JSON-compatible:
    Mapping any child fields from Python classes to dicts if necessary, avoiding any circular
    references, etc.
    """
    return to_dict_recursive_generic(self, **kwargs)

to_file

to_file(output_path, indent=4, default=str, **kwargs)

Save the state of the object to a (local or Cloud) JSON file

Parameters:

Name Type Description Default
output_path PathLike

The path where the configuration file will be saved.

required
indent int | str | None

Optional indentation passed through to to_json() and therefore json.dumps()

4
default Callable[[Any], Any] | None

Optional function to convert non-JSON-serializable objects to strings, passed through to to_json() and therefore to json.dumps()

str
**kwargs Any

Optional extra keyword arguments to pass to to_json()

{}

Returns:

Name Type Description
output_path UPath

Universal Path representation of the target file.

Source code in llmeter/callbacks/cost/serde.py
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
def to_file(
    self,
    output_path: os.PathLike,
    indent: int | str | None = 4,
    default: Callable[[Any], Any] | None = str,
    **kwargs: Any,
) -> Path:
    """Save the state of the object to a (local or Cloud) JSON file

    Args:
        output_path: The path where the configuration file will be saved.
        indent: Optional indentation passed through to `to_json()` and therefore `json.dumps()`
        default: Optional function to convert non-JSON-serializable objects to strings, passed
            through to `to_json()` and therefore to `json.dumps()`
        **kwargs: Optional extra keyword arguments to pass to `to_json()`

    Returns:
        output_path: Universal Path representation of the target file.
    """
    output_path = Path(output_path)
    output_path.parent.mkdir(parents=True, exist_ok=True)
    with output_path.open("w") as f:
        f.write(self.to_json(indent=indent, default=default, **kwargs))
    return output_path

to_json

to_json(**kwargs)

Serialize this object to JSON, with optional kwargs passed through to json.dumps()

Source code in llmeter/callbacks/cost/serde.py
201
202
203
def to_json(self, **kwargs) -> str:
    """Serialize this object to JSON, with optional kwargs passed through to `json.dumps()`"""
    return json.dumps(self.to_dict(), **kwargs)

from_dict_with_class

from_dict_with_class(raw, cls, **kwargs)

Initialize an instance of a class from a plain dict (with optional extra kwargs) If the input dictionary contains a _type key, and this doesn't match the provided cls.__name__, a warning will be logged. Args: raw: A plain Python dict, for example loaded from a JSON file cls: The class to create an instance of **kwargs: Optional extra keyword arguments to pass to the constructor

Source code in llmeter/callbacks/cost/serde.py
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def from_dict_with_class(raw: dict, cls: Type[TFromDict], **kwargs) -> TFromDict:
    """Initialize an instance of a class from a plain dict (with optional extra kwargs)
    If the input dictionary contains a `_type` key, and this doesn't match the provided
    `cls.__name__`, a warning will be logged.
    Args:
        raw: A plain Python dict, for example loaded from a JSON file
        cls: The class to create an instance of
        **kwargs: Optional extra keyword arguments to pass to the constructor
    """
    raw_args = {k: v for k, v in raw.items()}
    raw_type = raw_args.pop("_type", None)
    if raw_type is not None and raw_type != cls.__name__:
        logger.warning(
            "from_dict: _type '%s' doesn't match class '%s' being loaded. %s"
            % (raw_type, cls.__name__, raw)
        )
    return cls(**raw_args, **kwargs)

from_dict_with_class_map

from_dict_with_class_map(raw, class_map, **kwargs)

Initialize an instance of a class from a plain dict (with optional extra kwargs) Args: raw: A plain Python dict which must contain a _type key classes: A mapping from _type string to class to create an instance of **kwargs: Optional extra keyword arguments to pass to the constructor

Source code in llmeter/callbacks/cost/serde.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
def from_dict_with_class_map(
    raw: dict, class_map: Dict[str, Type[TFromDict]], **kwargs
) -> TFromDict:
    """Initialize an instance of a class from a plain dict (with optional extra kwargs)
    Args:
        raw: A plain Python dict which must contain a `_type` key
        classes: A mapping from `_type` string to class to create an instance of
        **kwargs: Optional extra keyword arguments to pass to the constructor
    """
    if "_type" not in raw:
        raise ValueError("from_dict_with_class_map: No _type in raw dict: %s" % raw)
    if raw["_type"] not in class_map:
        raise ValueError(
            "Object _type '%s' not found in provided class_map %s"
            % (raw["_type"], class_map)
        )
    return from_dict_with_class(raw, class_map[raw["_type"]], **kwargs)

is_dataclass_instance

is_dataclass_instance(obj)

Check whether obj is an instance of any dataclass See: https://docs.python.org/3/library/dataclasses.html#dataclasses.is_dataclass

Source code in llmeter/callbacks/cost/serde.py
32
33
34
35
36
def is_dataclass_instance(obj):
    """Check whether `obj` is an instance of any dataclass
    See: https://docs.python.org/3/library/dataclasses.html#dataclasses.is_dataclass
    """
    return is_dataclass(obj) and not isinstance(obj, type)

to_dict_recursive_generic

to_dict_recursive_generic(obj, **kwargs)

Convert a vaguely dataclass-like object (with maybe IJSONable fields) to a JSON-ready dict The output dict is augmented with _type storing the __class__.__name__ of the provided obj. Args: obj: The object to convert **kwargs: Optional extra parameters to insert in the output dictionary

Source code in llmeter/callbacks/cost/serde.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def to_dict_recursive_generic(obj: object, **kwargs) -> dict:
    """Convert a vaguely dataclass-like object (with maybe IJSONable fields) to a JSON-ready dict
    The output dict is augmented with `_type` storing the `__class__.__name__` of the provided
    `obj`.
    Args:
        obj: The object to convert
        **kwargs: Optional extra parameters to insert in the output dictionary
    """
    obj_classname = obj.__class__.__name__
    result = {} if obj_classname == "dict" else {"_type": obj.__class__.__name__}
    if hasattr(obj, "__dict__"):
        # We *don't* use dataclass asdict() here because we want our custom behaviour instead of its
        # recursion:
        result.update(obj.__dict__)
    elif (
        isinstance(obj, dict)
        or hasattr(obj, "__keys__")
        and hasattr(obj, "__getitem__")
    ):
        result.update(obj)
    else:
        result.update({k: getattr(obj, k) for k in dir(obj)})
    result.update(kwargs)
    for k, v in result.items():
        if hasattr(v, "to_dict"):
            result[k] = v.to_dict()
        elif isinstance(v, dict):
            result[k] = to_dict_recursive_generic(v)
        elif isinstance(v, (list, tuple)):
            result[k] = [to_dict_recursive_generic(item) for item in v]
        elif isinstance(v, (date, datetime, time)):
            result[k] = v.isoformat()
    return result