Element, IDType, and ID¶
Overview¶
This module contains classes and functions that handle UUID-based IDs,
simple identity checks, and a foundational Element
class built on
Pydantic. With these abstractions, other parts of the system can reliably
create and reference unique objects by ID, track metadata, and manage
timestamps.
IDType¶
- class lionagi.protocols.generic.element.IDType¶
A lightweight wrapper around a UUID (preferably version 4). It ensures valid UUIDv4 strings can be created or parsed, and supports hashing/equality so instances can be used as dictionary keys or set members.
Attributes¶
- _idUUID
The wrapped UUID object.
Notes¶
Use
create()
to generate a random version-4 UUID.Use
validate()
to convert user inputs (strings, UUIDs, or existingIDType
objects) into a properIDType
.
Methods¶
- __init__(id: UUID) None ¶
Initializes the wrapper with a given UUID (ideally v4).
- classmethod validate(value: str | UUID | IDType) IDType ¶
Validates and converts a string, UUID, or existing IDType into a proper
IDType
. Raiseslionagi._errors.IDError
if the input is invalid.
- __str__() str ¶
Returns the string representation of the underlying UUID.
- __repr__() str ¶
Developer-friendly representation for debugging, e.g.
IDType(XXXX)
.
- __eq__(other: Any) bool ¶
Checks equality based on the underlying UUID.
- __hash__() int ¶
Returns a hash so
IDType
can serve as a dictionary key.
Element¶
- class lionagi.protocols.generic.element.Element¶
Inherits from:
pydantic.BaseModel
,lionagi.protocols._concepts.Observable
A Pydantic-based class that provides a unique ID, a creation timestamp, and a flexible metadata dictionary. This forms a foundation for many other LionAGI components that need to be both observable and serializable.
Attributes¶
- idIDType
A unique identifier (UUIDv4). Defaults to a newly generated one.
- created_atfloat
A float-based Unix timestamp. Defaults to current time.
- metadatadict
A dictionary for storing additional user-defined data.
Methods¶
- classmethod class_name(full: bool = False) str ¶
Returns either the class name (e.g.,
Element
) or the fully qualified path (e.g.,lionagi.protocols.generic.element.Element
) depending on thefull
flag.
- to_dict() dict ¶
Converts this Element into a dictionary, appending the
"lion_class"
field to metadata for class identification. Fields that arelionagi.utils.UNDEFINED
are omitted.
Notes¶
The Pydantic validators ensure that
id
is always a validIDType
andcreated_at
is a float-based timestamp.Instances of
Element
are hashable and can be used as dictionary keys (hashing by theirid
).
validate_order¶
- lionagi.protocols.generic.element.validate_order(order: Any) list[IDType] ¶
Validates and flattens an ordering structure into a list of
IDType
objects. Accepts various input forms—like a singleElement
, a list of items, or a mapping (dictionary) of items—and ensures a consistent flat list of IDs.Parameters¶
Returns¶
- list[IDType]
A flat list of validated
IDType
objects.
Raises¶
- ValueError
If any item cannot be converted to an
IDType
or if inconsistent types are encountered.
Examples¶
Basic usage with a single
Element
:element = SomeElementSubclass() order_list = validate_order(element) # order_list is now [element.id]
Passing a nested list of elements or strings:
items = [element1, "7acf8c04-3c6b-4edf-b221-e90c88f24fc0", [element2, element3]] order_list = validate_order(items) # returns a flat list of IDType objects
ID¶
- class lionagi.protocols.generic.element.ID(Generic[E])¶
A utility class for extracting IDs from various inputs and checking if an object qualifies as an ID. Useful in code that must handle Elements, raw strings (representing UUIDs), or
IDType
objects interchangeably.Type Parameters¶
Aliases¶
- IDTypeAlias
Refers to
IDType
.- ItemTypeAlias
Can be an instance of
E
orElement
.- RefTypeAlias
Can be an
IDType
, an Element, or a UUID string.- IDSeqTypeAlias
Sequence of
IDType
or anOrdering[E]
.- ItemSeqTypeAlias
Sequence of type
E
or aCollective[E]
.- RefSeqTypeAlias
A union of
ItemSeq
, a sequence of references, orOrdering[E]
.
Methods¶
Example¶
from uuid import uuid4 from lionagi.protocols.generic.element import ID, Element, IDType class MyElement(Element): pass elem = MyElement() print(ID.get_id(elem)) # -> IDType(...) print(ID.is_id(uuid4())) # -> True
File Location¶
Source File:
lionagi/protocols/generic/element.py
Copyright (c) 2023 - 2024, HaiyangLi <quantocean.li at gmail dot com>
SPDX-License-Identifier: Apache-2.0