Common Module Overviewยถ

The Swiss Army Knife of AI Infrastructureยถ

๐Ÿงฐ Common Module

Battle-tested utilities that make AI systems smarter by default

The Common module provides an extraordinary collection of intelligent mixins, advanced data structures, and performance utilities that form the backbone of the Haive ecosystem.

Architecture Overviewยถ

digraph common_architecture { rankdir=TB; node [shape=box, style="rounded,filled", fillcolor=lightblue]; edge [color=darkblue]; subgraph cluster_mixins { label="Mixin System"; style=filled; fillcolor=lightyellow; IdentifierMixin [label="IdentifierMixin\n(Unique IDs)"]; TimestampMixin [label="TimestampMixin\n(Created/Updated)"]; VersionMixin [label="VersionMixin\n(Semantic Versioning)"]; ObservableMixin [label="ObservableMixin\n(Event System)"]; CacheableMixin [label="CacheableMixin\n(Result Caching)"]; } subgraph cluster_structures { label="Data Structures"; style=filled; fillcolor=lightgreen; Tree [label="Tree\n(Hierarchical Data)"]; Graph [label="Graph\n(Network Structure)"]; NamedList [label="NamedList\n(Enhanced Lists)"]; NestedDict [label="NestedDict\n(Deep Access)"]; } subgraph cluster_types { label="Type System"; style=filled; fillcolor=lightcoral; TypeInference [label="Type Inference\n(Runtime Detection)"]; ProtocolCheck [label="Protocol Checking\n(Interface Validation)"]; TypeGuards [label="Type Guards\n(Safe Narrowing)"]; } Component [label="Your Component", shape=ellipse, fillcolor=white]; Component -> IdentifierMixin [label="inherits"]; Component -> TimestampMixin [label="inherits"]; Component -> Tree [label="uses"]; Component -> TypeInference [label="validates with"]; }

Mixin Architectureยถ

Class Hierarchyยถ

Core Mixinsยถ

class haive.core.common.mixins.IdentifierMixin(*, id=<factory>, name=None)[source]ยถ

Bases: BaseModel

Mixin that adds unique identification to any Pydantic model.

This mixin provides both UUID-based identification and human-readable naming capabilities. It automatically generates UUIDs, validates provided IDs, and offers convenience methods for working with the identifiers.

Parameters:
  • data (Any)

  • id (str)

  • name (str | None)

idยถ

A UUID string that uniquely identifies the object.

Type:

str

nameยถ

An optional human-readable name for the object.

Type:

str | None

short_idยถ

First 8 characters of the UUID (computed).

Return type:

str

display_nameยถ

User-friendly name for display (computed).

Return type:

str

uuid_objยถ

UUID object representation of the ID (computed).

Return type:

uuid.UUID

has_custom_nameยถ

Whether a custom name is set (computed).

Return type:

bool

Example Usage:

from haive.core.common.mixins import IdentifierMixin

class MyComponent(IdentifierMixin):
    def __init__(self, name: str):
        super().__init__()
        self.name = name
        # self.id is automatically generated

component = MyComponent("example")
print(component.id)  # e.g., "MyComponent_7f3d8a9b"
classmethod validate_id(v)[source]ยถ

Ensure ID is a valid UUID string.

Parameters:

v (str) โ€“ The ID string to validate.

Returns:

The validated ID string, or a new UUID if invalid.

Return type:

str

classmethod validate_name(v)[source]ยถ

Validate and clean the name field.

Parameters:

v (str | None) โ€“ The name string to validate.

Returns:

The cleaned name string, or None if empty after cleaning.

Return type:

str | None

clear_name()[source]ยถ

Clear the name.

Return type:

None

identifier_info()[source]ยถ

Get comprehensive identifier information.

Returns:

A dictionary containing all identifier-related information.

Return type:

dict[str, str]

initialize_uuid_obj()[source]ยถ

Initialize UUID object after model validation.

Returns:

Self, with the _uuid_obj private attribute initialized.

Return type:

Self

matches_id(id_or_name)[source]ยถ

Check if this object matches the given ID or name.

This method checks if the provided string matches this objectโ€™s full ID, short ID, or name (case-insensitive).

Parameters:

id_or_name (str) โ€“ The ID or name string to check against.

Returns:

True if thereโ€™s a match, False otherwise.

Return type:

bool

model_post_init(context, /)ยถ

This function is meant to behave like a BaseModel method to initialise private attributes.

It takes context as an argument since thatโ€™s what pydantic-core passes when calling it.

Parameters:
  • self (BaseModel) โ€“ The BaseModel instance.

  • context (Any) โ€“ The context.

Return type:

None

regenerate_id()[source]ยถ

Generate a new ID and return it.

This method creates a new UUID, updates the ID field, and returns the new ID string.

Returns:

The newly generated UUID string.

Return type:

str

set_name(name)[source]ยถ

Set the name with validation.

Parameters:

name (str) โ€“ The new name to set.

Return type:

None

property display_name: strยถ

Display name (uses name if available, otherwise short_id).

Returns:

The human-readable name if set, otherwise โ€œObject-{short_id}โ€.

Return type:

str

property has_custom_name: boolยถ

Whether this object has a custom name (not auto-generated).

Returns:

True if a non-empty name is set, False otherwise.

Return type:

bool

id: strยถ
model_config: ClassVar[ConfigDict] = {}ยถ

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

name: str | Noneยถ
property short_id: strยถ

Short version of the ID (first 8 characters).

Returns:

The first 8 characters of the UUID string.

Return type:

str

property uuid_obj: UUIDยถ

UUID object representation of the ID.

Returns:

The UUID object corresponding to the ID string.

Return type:

uuid.UUID

class haive.core.common.mixins.TimestampMixin(*, created_at=<factory>, updated_at=<factory>)[source]ยถ

Bases: BaseModel

Mixin for adding timestamp tracking to Pydantic models.

This mixin adds creation and update timestamps to any model, with methods for updating timestamps and calculating time intervals. Itโ€™s useful for tracking when objects were created and modified, which helps with auditing, caching strategies, and expiration logic.

Parameters:
created_atยถ

When this object was created (auto-set on instantiation).

Type:

datetime.datetime

updated_atยถ

When this object was last updated (initially same as created_at).

Type:

datetime.datetime

Automatic Timestamp Management:

class TrackedEntity(TimestampMixin):
    def update_data(self, new_data):
        self.data = new_data
        self.mark_updated()  # Updates timestamp

entity = TrackedEntity()
print(entity.created_at)  # Creation time
print(entity.updated_at)  # Last update time
age_in_seconds()[source]ยถ

Get age of this object in seconds.

This method calculates how much time has passed since the object was created.

Returns:

Number of seconds since creation.

Return type:

float

time_since_update()[source]ยถ

Get time since last update in seconds.

This method calculates how much time has passed since the object was last updated.

Returns:

Number of seconds since last update.

Return type:

float

update_timestamp()[source]ยถ

Update the updated_at timestamp to the current time.

This method should be called whenever the object is modified to track the time of the latest change.

Return type:

None

created_at: datetimeยถ
model_config: ClassVar[ConfigDict] = {}ยถ

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

updated_at: datetimeยถ

Data Structuresยถ

Tree Structureยถ

Usage Example:

Performance Patternsยถ

Caching Strategyยถ

        sequenceDiagram
    participant Client
    participant Component
    participant Cache
    participant Computation

    Client->>Component: request(data)
    Component->>Cache: check(data_hash)
    alt Cache Hit
        Cache-->>Component: cached_result
        Component-->>Client: return cached_result
    else Cache Miss
        Component->>Computation: compute(data)
        Computation-->>Component: result
        Component->>Cache: store(data_hash, result)
        Component-->>Client: return result
    end
    

Implementation:

from haive.core.common.mixins import CacheableMixin
from haive.core.common.decorators import memoize

class SmartProcessor(CacheableMixin):
    @memoize(maxsize=1000, ttl=3600)
    def analyze(self, text: str) -> Dict[str, float]:
        """Expensive analysis cached for 1 hour."""
        # Complex NLP processing
        return {"sentiment": 0.8, "confidence": 0.95}

Type System Enhancementsยถ

Type Inference Flowยถ

digraph type_inference { rankdir=LR; node [shape=box]; Data [label="Raw Data\n{...}"]; Analyzer [label="Type Analyzer", fillcolor=lightblue, style=filled]; Schema [label="Type Schema\nTypedDict", fillcolor=lightgreen, style=filled]; Validator [label="Runtime\nValidator", fillcolor=lightyellow, style=filled]; Data -> Analyzer [label="analyze"]; Analyzer -> Schema [label="generate"]; Schema -> Validator [label="create"]; Validator -> Data [label="validate", style=dashed]; }

Advanced Usage Patternsยถ

Event-Driven Architectureยถ

from haive.core.common.mixins import ObservableMixin

class WorkflowEngine(ObservableMixin):
    """Event-driven workflow with observable state changes."""

    def __init__(self):
        super().__init__()
        self.state = "idle"

        # Wire event handlers
        self.on("state_change", self._log_transition)
        self.on("error", self._handle_error)
        self.on("complete", self._cleanup)

    def process(self, data):
        self._transition("processing")
        try:
            result = self._execute(data)
            self._transition("complete")
            self.emit("complete", {"result": result})
            return result
        except Exception as e:
            self._transition("error")
            self.emit("error", {"exception": e, "data": data})
            raise

    def _transition(self, new_state):
        old_state = self.state
        self.state = new_state
        self.emit("state_change", {
            "from": old_state,
            "to": new_state
        })

Composition Patternยถ

from haive.core.common.mixins import (
    IdentifierMixin,
    TimestampMixin,
    VersionMixin,
    ObservableMixin,
    SerializationMixin
)

class IntelligentAgent(
    IdentifierMixin,
    TimestampMixin,
    VersionMixin,
    ObservableMixin,
    SerializationMixin
):
    """Agent with multiple intelligent behaviors."""

    version = "1.0.0"

    def __init__(self, name: str):
        super().__init__()  # Initialize all mixins
        self.name = name
        self.knowledge = Tree[str]("root")

        # Emit creation event
        self.emit("agent_created", {
            "id": self.id,
            "name": name,
            "version": self.version
        })

    def learn(self, fact: str, category: str = "general"):
        """Add knowledge and track changes."""
        node = self.knowledge.add_child_to(fact, category)
        self.mark_updated()  # From TimestampMixin
        self.bump_version("patch")  # From VersionMixin
        self.emit("learned", {"fact": fact, "category": category})

    def to_dict(self) -> Dict[str, Any]:
        """Serialize to dictionary (from SerializationMixin)."""
        return {
            **super().to_dict(),  # Include mixin fields
            "name": self.name,
            "knowledge": self.knowledge.to_dict()
        }

Performance Benchmarksยถ

Mixin Performance Characteristicsยถ

Operation

Time Complexity

Space Complexity

Notes

Mixin Initialization

O(1)

O(1)

< 0.1ms overhead

ID Generation

O(1)

O(1)

UUID-based

Event Emission

O(n)

O(1)

n = number of listeners

Tree Operations

O(log n)

O(n)

Balanced tree assumed

Cache Lookup

O(1)

O(k)

k = cache size

API Referenceยถ

Mixinsยถ

๐Ÿงฉ Mixins - Intelligent Component Superpowers System

THE MOLECULAR BUILDING BLOCKS OF AI EXCELLENCE

Welcome to Mixins - the revolutionary collection of intelligent, composable behaviors that transform ordinary classes into extraordinary AI components. This isnโ€™t just multiple inheritance; itโ€™s a sophisticated composition system where every mixin is a specialized capability that makes your components smarter, more reliable, and enterprise-ready by default.

โšก REVOLUTIONARY MIXIN INTELLIGENCEยถ

Mixins represent a paradigm shift from manual feature implementation to intelligent capability composition where sophisticated behaviors are injected seamlessly into any class:

๐Ÿง  Self-Configuring Behaviors: Mixins that automatically adapt to their host class ๐Ÿ”„ Zero-Conflict Composition: Intelligent inheritance resolution and method chaining โšก Performance Optimization: Built-in caching, lazy loading, and resource management ๐Ÿ“Š Enterprise-Grade Observability: Automatic logging, metrics, and monitoring ๐ŸŽฏ Type-Safe Integration: Full Pydantic compatibility with intelligent field merging

๐ŸŒŸ CORE MIXIN CATEGORIESยถ

1. Identity & Lifecycle Mixins ๐Ÿ†”

Fundamental behaviors for object identity and lifecycle management:

Examples

>>> from haive.core.common.mixins import (
>>> IdentifierMixin, TimestampMixin, VersionMixin, MetadataMixin
>>> )
>>>
>>> class IntelligentAgent(
>>> IdentifierMixin,     # Unique IDs with collision detection
>>> TimestampMixin,      # Created/updated/accessed tracking
>>> VersionMixin,        # Semantic versioning with migrations
>>> MetadataMixin        # Rich metadata with indexing
>>> ):
>>> def __init__(self, name: str):
>>> super().__init__()
>>> self.name = name
>>> # Automatic capabilities:
>>> # - self.id: Unique identifier (UUID with prefix)
>>> # - self.created_at: ISO timestamp of creation
>>> # - self.version: Semantic version ("1.0.0")
>>> # - self.metadata: Indexed metadata storage
>>>
>>> # Enhanced instantiation
>>> agent = IntelligentAgent("research_assistant")
>>> assert agent.id.startswith("agent_")  # Automatic prefixing
>>> assert agent.created_at <= datetime.now()  # Timestamp validation
>>> assert agent.version == "1.0.0"  # Default version
2. State Management Mixins ๐Ÿ—„๏ธ

Advanced state handling with intelligent persistence:

>>> from haive.core.common.mixins import (
>>> StateMixin, StateInterfaceMixin, CheckpointerMixin
>>> )
>>>
>>> class StatefulProcessor(
>>> StateMixin,           # Core state management
>>> StateInterfaceMixin,  # Advanced state operations
>>> CheckpointerMixin     # Automatic checkpointing
>>> ):
>>> def __init__(self):
>>> super().__init__()
>>> # Automatic capabilities:
>>> # - State validation and serialization
>>> # - Automatic dirty tracking
>>> # - Checkpoint creation and restoration
>>> # - State migration support
>>>
>>> def process(self, data):
>>> # State automatically tracked
>>> self.state.update({"last_processed": data})
>>>
>>> # Automatic checkpoint creation
>>> if self.should_checkpoint():
>>> self.create_checkpoint("pre_processing")
>>>
>>> result = complex_processing(data)
>>>
>>> # State automatically persisted
>>> self.state.finalize_update()
>>> return result

For complete examples and advanced patterns, see the documentation.

class haive.core.common.mixins.CheckpointerMixin[source]ยถ

Bases: BaseModel

Mixin that provides checkpointing capabilities for stateful graph execution.

This mixin adds methods for running stateful graph executions with checkpointing support, including automatic state restoration, thread management, and proper configuration handling for both synchronous and asynchronous execution patterns.

The mixin expects the host class to provide: - persistence: Optional[CheckpointerConfig] - Configuration for the checkpointer - checkpoint_mode: str - Mode of checkpointing (โ€œsyncโ€, โ€œasyncโ€, or โ€œnoneโ€) - runnable_config: RunnableConfig - Base configuration for runnables - input_schema, state_schema (optional) - Schemas for input and state validation - app or compile() method - The LangGraph compiled application

Parameters:

data (Any)

None publicly, but requires the above attributes from the host class.
async arun(input_data, thread_id=None, config=None, **kwargs)[source]ยถ

Async run with checkpointer support.

This method runs a graph execution asynchronously with checkpointing support, automatically handling state restoration and persistence.

Parameters:
  • input_data (Any) โ€“ The input data for the execution.

  • thread_id (str | None) โ€“ Optional thread ID for state tracking.

  • config (RunnableConfig | None) โ€“ Optional configuration override.

  • **kwargs โ€“ Additional configuration parameters.

Returns:

The result of the graph execution.

Return type:

Any

async astream(input_data, thread_id=None, stream_mode='values', config=None, **kwargs)[source]ยถ

Async stream with checkpointer support.

This method streams graph execution results asynchronously with checkpointing support, automatically handling state restoration and persistence.

Parameters:
  • input_data (Any) โ€“ The input data for the execution.

  • thread_id (str | None) โ€“ Optional thread ID for state tracking.

  • stream_mode (str) โ€“ The streaming mode to use (values, actions, etc.).

  • config (RunnableConfig | None) โ€“ Optional configuration override.

  • **kwargs โ€“ Additional configuration parameters.

Yields:

Execution chunks as they become available.

Return type:

AsyncGenerator[dict[str, Any], None]

get_checkpointer(async_mode=False)[source]ยถ

Get the appropriate checkpointer.

Parameters:

async_mode (bool) โ€“ Whether to return the async checkpointer.

Returns:

The appropriate checkpointer instance or None if not available.

Return type:

Any

model_post_init(context, /)ยถ

This function is meant to behave like a BaseModel method to initialise private attributes.

It takes context as an argument since thatโ€™s what pydantic-core passes when calling it.

Parameters:
  • self (BaseModel) โ€“ The BaseModel instance.

  • context (Any) โ€“ The context.

Return type:

None

run(input_data, thread_id=None, config=None, **kwargs)[source]ยถ

Run with checkpointer support.

This method runs a graph execution with checkpointing support, automatically handling state restoration and persistence.

Parameters:
  • input_data (Any) โ€“ The input data for the execution.

  • thread_id (str | None) โ€“ Optional thread ID for state tracking.

  • config (RunnableConfig | None) โ€“ Optional configuration override.

  • **kwargs โ€“ Additional configuration parameters.

Returns:

The result of the graph execution.

Return type:

Any

stream(input_data, thread_id=None, stream_mode='values', config=None, **kwargs)[source]ยถ

Stream with checkpointer support.

This method streams graph execution results with checkpointing support, automatically handling state restoration and persistence.

Parameters:
  • input_data (Any) โ€“ The input data for the execution.

  • thread_id (str | None) โ€“ Optional thread ID for state tracking.

  • stream_mode (str) โ€“ The streaming mode to use (values, actions, etc.).

  • config (RunnableConfig | None) โ€“ Optional configuration override.

  • **kwargs โ€“ Additional configuration parameters.

Returns:

A generator yielding execution chunks.

Return type:

Generator[dict[str, Any], None, None]

model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}ยถ

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

haive.core.common.mixins.EngineMixinยถ

alias of EngineStateMixin

Parameters:

data (Any)

class haive.core.common.mixins.GetterMixin[source]ยถ

Bases: Generic[T]

A mixin providing rich lookup and filtering capabilities for collections.

This mixin can be added to any collection class that implements _get_items() to provide powerful querying capabilities. It works with both dictionary-like objects and objects with attributes.

The mixin is generic over type T, which represents the type of items in the collection. This enables proper type hinting when using the mixinโ€™s methods.

None directly, but requires subclasses to implement _get_items()
field_values(field_name)[source]ยถ

Get all values for a specific field across items.

This method collects the values of a specific field or attribute from all items in the collection.

Parameters:

field_name (str) โ€“ Field name to collect.

Returns:

List of field values (None for items where field doesnโ€™t exist).

Return type:

list[Any]

Example

# Get all user IDs
user_ids = users.field_values("id")
filter(**kwargs)[source]ยถ

Filter items by multiple attribute criteria.

This method finds all items that match all of the specified attribute criteria (logical AND of all criteria).

Parameters:

**kwargs โ€“ Field name and value pairs to match.

Returns:

List of matching items (empty list if none found).

Return type:

list[T]

Example

# Find all admin users with status='active'
active_admins = collection.filter(role="admin", status="active")
find(predicate)[source]ยถ

Find first item matching a custom predicate function.

This method finds the first item for which the predicate function returns True.

Parameters:

predicate (Callable[[T], bool]) โ€“ Function that takes an item and returns a boolean.

Returns:

First matching item or None if none found.

Return type:

T | None

Example

# Find first user with name longer than 10 characters
user = users.find(lambda u: len(u.name) > 10)
find_all(predicate)[source]ยถ

Find all items matching a custom predicate function.

This method finds all items for which the predicate function returns True.

Parameters:

predicate (Callable[[T], bool]) โ€“ Function that takes an item and returns a boolean.

Returns:

List of matching items (empty list if none found).

Return type:

list[T]

Example

# Find all premium users with subscription expiring in 7 days
from datetime import datetime, timedelta
next_week = datetime.now() + timedelta(days=7)
expiring = users.find_all(
    lambda u: u.is_premium and u.expires_at.date() == next_week.date()
)
first(**kwargs)[source]ยถ

Get first item matching criteria.

This is a convenience method that combines filter() with returning the first result only.

Parameters:

**kwargs โ€“ Field name and value pairs to match.

Returns:

First matching item or None if none found.

Return type:

T | None

Example

# Find first active admin user
admin = users.first(role="admin", status="active")
get_all_by_attr(attr_name, value)[source]ยถ

Get all items where attribute equals value.

This method finds all items in the collection where the specified attribute matches the given value.

Parameters:
  • attr_name (str) โ€“ Attribute name to check.

  • value (Any) โ€“ Value to match.

Returns:

List of matching items (empty list if none found).

Return type:

list[T]

get_by_attr(attr_name, value, default=None)[source]ยถ

Get first item where attribute equals value.

This method finds the first item in the collection where the specified attribute matches the given value.

Parameters:
  • attr_name (str) โ€“ Attribute name to check.

  • value (Any) โ€“ Value to match.

  • default (T | None) โ€“ Default value if not found.

Returns:

First matching item or default if none found.

Return type:

T | None

get_by_type(type_cls)[source]ยถ

Get all items of specified type.

This method finds all items that are instances of the specified type.

Parameters:

type_cls (type) โ€“ Type to match.

Returns:

List of matching items (empty list if none found).

Return type:

list[T]

Example

# Get all TextMessage instances
text_messages = messages.get_by_type(TextMessage)
class haive.core.common.mixins.IdMixin(*, id=<factory>)[source]ยถ

Bases: BaseModel

Mixin for adding basic ID generation and management capabilities.

This mixin adds a UUID-based ID field to any Pydantic model, with methods for regenerating the ID and creating instances with specific IDs.

Parameters:
  • data (Any)

  • id (str)

idยถ

A UUID string that uniquely identifies the object.

Type:

str

classmethod with_id(id_value, **kwargs)[source]ยถ

Create an instance with a specific ID.

This class method provides a convenient way to create an instance with a predetermined ID value.

Parameters:
  • id_value (str) โ€“ The ID value to use.

  • **kwargs โ€“ Additional attributes for the instance.

Returns:

A new instance with the specified ID.

regenerate_id()[source]ยถ

Generate a new UUID and return it.

This method replaces the current ID with a new UUID.

Returns:

The newly generated UUID string.

Return type:

str

id: strยถ
model_config: ClassVar[ConfigDict] = {}ยถ

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class haive.core.common.mixins.IdentifierMixin(*, id=<factory>, name=None)[source]ยถ

Bases: BaseModel

Mixin that adds unique identification to any Pydantic model.

This mixin provides both UUID-based identification and human-readable naming capabilities. It automatically generates UUIDs, validates provided IDs, and offers convenience methods for working with the identifiers.

Parameters:
  • data (Any)

  • id (str)

  • name (str | None)

idยถ

A UUID string that uniquely identifies the object.

Type:

str

nameยถ

An optional human-readable name for the object.

Type:

str | None

short_idยถ

First 8 characters of the UUID (computed).

Return type:

str

display_nameยถ

User-friendly name for display (computed).

Return type:

str

uuid_objยถ

UUID object representation of the ID (computed).

Return type:

uuid.UUID

has_custom_nameยถ

Whether a custom name is set (computed).

Return type:

bool

classmethod validate_id(v)[source]ยถ

Ensure ID is a valid UUID string.

Parameters:

v (str) โ€“ The ID string to validate.

Returns:

The validated ID string, or a new UUID if invalid.

Return type:

str

classmethod validate_name(v)[source]ยถ

Validate and clean the name field.

Parameters:

v (str | None) โ€“ The name string to validate.

Returns:

The cleaned name string, or None if empty after cleaning.

Return type:

str | None

clear_name()[source]ยถ

Clear the name.

Return type:

None

identifier_info()[source]ยถ

Get comprehensive identifier information.

Returns:

A dictionary containing all identifier-related information.

Return type:

dict[str, str]

initialize_uuid_obj()[source]ยถ

Initialize UUID object after model validation.

Returns:

Self, with the _uuid_obj private attribute initialized.

Return type:

Self

matches_id(id_or_name)[source]ยถ

Check if this object matches the given ID or name.

This method checks if the provided string matches this objectโ€™s full ID, short ID, or name (case-insensitive).

Parameters:

id_or_name (str) โ€“ The ID or name string to check against.

Returns:

True if thereโ€™s a match, False otherwise.

Return type:

bool

model_post_init(context, /)ยถ

This function is meant to behave like a BaseModel method to initialise private attributes.

It takes context as an argument since thatโ€™s what pydantic-core passes when calling it.

Parameters:
  • self (BaseModel) โ€“ The BaseModel instance.

  • context (Any) โ€“ The context.

Return type:

None

regenerate_id()[source]ยถ

Generate a new ID and return it.

This method creates a new UUID, updates the ID field, and returns the new ID string.

Returns:

The newly generated UUID string.

Return type:

str

set_name(name)[source]ยถ

Set the name with validation.

Parameters:

name (str) โ€“ The new name to set.

Return type:

None

property display_name: strยถ

Display name (uses name if available, otherwise short_id).

Returns:

The human-readable name if set, otherwise โ€œObject-{short_id}โ€.

Return type:

str

property has_custom_name: boolยถ

Whether this object has a custom name (not auto-generated).

Returns:

True if a non-empty name is set, False otherwise.

Return type:

bool

id: strยถ
model_config: ClassVar[ConfigDict] = {}ยถ

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

name: str | Noneยถ
property short_id: strยถ

Short version of the ID (first 8 characters).

Returns:

The first 8 characters of the UUID string.

Return type:

str

property uuid_obj: UUIDยถ

UUID object representation of the ID.

Returns:

The UUID object corresponding to the ID string.

Return type:

uuid.UUID

class haive.core.common.mixins.MCPMixin(**data)[source]ยถ

Bases: BaseModel

Mixin for adding MCP (Model Context Protocol) support to configurations.

This mixin provides seamless integration with MCP servers, enabling: - Automatic discovery and wrapping of MCP tools - Resource loading and caching from MCP servers - Prompt template management - Enhanced system prompts with MCP information

The mixin is designed to work with ToolRouteMixin for proper tool routing and can be combined with other mixins in the configuration hierarchy.

Parameters:

data (Any)

mcp_configยถ

Optional MCP configuration for server connections

Type:

MCPConfig | None

mcp_resourcesยถ

List of discovered MCP resources

Type:

list[haive.core.common.mixins.mcp_mixin.MCPResource]

mcp_promptsยถ

Dictionary of MCP prompt templates

Type:

dict[str, haive.core.common.mixins.mcp_mixin.MCPPromptTemplate]

auto_discover_mcp_toolsยถ

Whether to automatically discover MCP tools

Type:

bool

inject_mcp_resourcesยถ

Whether to inject resources into context

Type:

bool

use_mcp_promptsยถ

Whether to use MCP prompts for enhancement

Type:

bool

async call_mcp_prompt(prompt_name, arguments=None)[source]ยถ

Call an MCP prompt to get formatted messages.

Parameters:
  • prompt_name (str) โ€“ Name of the prompt (can include server prefix)

  • arguments (dict[str, Any] | None) โ€“ Arguments to pass to the prompt

Returns:

List of message dictionaries with role and content

Raises:

ValueError โ€“ If prompt not found or MCP not initialized

Return type:

list[dict[str, str]]

cleanup_mcp()[source]ยถ

Clean up MCP resources.

This should be called when the configuration is no longer needed to properly close MCP connections.

Return type:

None

enhance_system_prompt_with_mcp(base_prompt='')[source]ยถ

Enhance a system prompt with MCP information.

Adds information about available MCP resources and operations to help the LLM understand what capabilities are available.

Parameters:

base_prompt (str) โ€“ Base system prompt to enhance

Returns:

Enhanced system prompt including MCP resources and capabilities

Return type:

str

get_mcp_prompts()[source]ยถ

Get all loaded MCP prompt templates.

Returns:

Dictionary of prompt templates by name

Return type:

dict[str, MCPPromptTemplate]

async get_mcp_resource_content(uri)[source]ยถ

Fetch content for an MCP resource.

Parameters:

uri (str) โ€“ Resource URI

Returns:

Resource content

Raises:

ValueError โ€“ If MCP manager not initialized or resource not found

Return type:

Any

get_mcp_resources()[source]ยถ

Get all loaded MCP resources.

Returns:

List of MCP resources

Return type:

list[MCPResource]

get_mcp_tools()[source]ยถ

Get all discovered MCP tools.

Returns:

List of MCP tool wrappers

Return type:

list[MCPToolWrapper]

model_post_init(context, /)ยถ

This function is meant to behave like a BaseModel method to initialise private attributes.

It takes context as an argument since thatโ€™s what pydantic-core passes when calling it.

Parameters:
  • self (BaseModel) โ€“ The BaseModel instance.

  • context (Any) โ€“ The context.

Return type:

None

async setup_mcp()[source]ยถ

Initialize MCP integration.

Sets up the MCP manager, discovers tools, loads resources, and configures prompts based on the MCP configuration.

This method should be called after creating the configuration but before using any MCP features.

Return type:

None

auto_discover_mcp_tools: boolยถ
inject_mcp_resources: boolยถ
mcp_config: MCPConfig | Noneยถ
mcp_prompts: dict[str, MCPPromptTemplate]ยถ
mcp_resources: list[MCPResource]ยถ
model_config: ClassVar[ConfigDict] = {}ยถ

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

use_mcp_prompts: boolยถ
class haive.core.common.mixins.MetadataMixin(*, metadata=<factory>)[source]ยถ

Bases: BaseModel

Mixin for adding flexible metadata storage capabilities.

This mixin provides a dictionary for storing arbitrary key-value pairs as metadata, along with methods for adding, retrieving, updating, and removing metadata entries.

Parameters:
metadataยถ

Dictionary containing arbitrary metadata.

Type:

dict[str, Any]

add_metadata(key, value)[source]ยถ

Add a metadata key-value pair.

Parameters:
  • key (str) โ€“ The metadata key.

  • value (Any) โ€“ The value to store.

Return type:

None

clear_metadata()[source]ยถ

Clear all metadata.

This method removes all metadata entries, resulting in an empty metadata dictionary.

Return type:

None

get_metadata(key, default=None)[source]ยถ

Get metadata value by key.

Parameters:
  • key (str) โ€“ The metadata key to retrieve.

  • default (Any) โ€“ Value to return if key doesnโ€™t exist.

Returns:

The metadata value or the default value if key doesnโ€™t exist.

Return type:

Any

has_metadata(key)[source]ยถ

Check if metadata key exists.

Parameters:

key (str) โ€“ The metadata key to check.

Returns:

True if the key exists in metadata, False otherwise.

Return type:

bool

remove_metadata(key)[source]ยถ

Remove and return metadata value.

Parameters:

key (str) โ€“ The metadata key to remove.

Returns:

The removed value, or None if key doesnโ€™t exist.

Return type:

Any

update_metadata(updates)[source]ยถ

Update multiple metadata fields.

Parameters:

updates (dict[str, Any]) โ€“ Dictionary containing key-value pairs to update.

Return type:

None

metadata: dict[str, Any]ยถ
model_config: ClassVar[ConfigDict] = {}ยถ

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class haive.core.common.mixins.RichLoggerMixin(*, debug=False)[source]ยถ

Bases: BaseModel

Mixin that provides rich console logging capabilities.

This mixin adds a configurable logger with rich formatting to any Pydantic model. It creates a logger named after the class, configures it with Richโ€™s handler for pretty console output, and provides convenience methods for different log levels with appropriate styling.

Parameters:
  • data (Any)

  • debug (bool)

debugยถ

Boolean flag to control debug output visibility.

Type:

bool

model_post_init(context, /)ยถ

This function is meant to behave like a BaseModel method to initialise private attributes.

It takes context as an argument since thatโ€™s what pydantic-core passes when calling it.

Parameters:
  • self (BaseModel) โ€“ The BaseModel instance.

  • context (Any) โ€“ The context.

Return type:

None

debug: boolยถ
property logger: Loggerยถ

Get or create logger with rich handler.

This property lazily initializes a logger with a Rich handler, creating it only when first accessed. The logger is named using the module and class name for proper log categorization.

Returns:

Configured logging.Logger instance with Rich formatting.

Return type:

logging.Logger

model_config: ClassVar[ConfigDict] = {}ยถ

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class haive.core.common.mixins.SecureConfigMixin[source]ยถ

Bases: object

A mixin to provide secure and flexible configuration for API keys.

This mixin enables: 1. Dynamic API key resolution from multiple sources 2. Secure storage using SecretStr 3. Environment variable fallbacks based on provider type 4. Validation and error reporting

The mixin implements a field validator for the โ€˜api_keyโ€™ field that attempts to resolve the key from environment variables if not explicitly provided, based on the โ€˜providerโ€™ field. It also provides a safe method to retrieve the key value with appropriate error handling.

api_keyยถ

A SecretStr containing the API key.

providerยถ

The API provider name (used to determine environment variable).

get_api_key()[source]ยถ

Safely retrieve the API key with improved error handling.

This method attempts to retrieve the API key value from the SecretStr field, with comprehensive error handling and helpful log messages for troubleshooting. In development environments, it can return fake test keys for testing purposes.

Returns:

The API key as a string, or None if not available or invalid.

Return type:

str | None

class haive.core.common.mixins.SerializationMixin[source]ยถ

Bases: BaseModel

Mixin for enhanced serialization and deserialization capabilities.

This mixin provides methods for converting Pydantic models to dictionaries and JSON strings, and for creating models from dictionaries and JSON strings. It handles private fields (starting with underscore) appropriately.

When combined with other mixins like IdMixin, TimestampMixin, etc., it provides a complete solution for model persistence.

Parameters:

data (Any)

classmethod from_dict(data)[source]ยถ

Create instance from dictionary.

This class method creates a model instance from a dictionary, using Pydanticโ€™s validation.

Parameters:

data (dict[str, Any]) โ€“ Dictionary containing model data.

Returns:

New model instance.

classmethod from_json(json_str)[source]ยถ

Create instance from JSON string.

This class method creates a model instance from a JSON string, parsing the JSON and then using from_dict().

Parameters:

json_str (str) โ€“ JSON string containing model data.

Returns:

New model instance.

to_dict(exclude_private=True)[source]ยถ

Convert to dictionary with options.

This method converts the model to a dictionary, with the option to exclude private fields (those starting with an underscore).

Parameters:

exclude_private (bool) โ€“ Whether to exclude private fields.

Returns:

Dictionary representation of the model.

Return type:

dict[str, Any]

to_json(exclude_private=True, **kwargs)[source]ยถ

Convert to JSON string.

This method converts the model to a JSON string, with options for controlling the JSON serialization.

Parameters:
  • exclude_private (bool) โ€“ Whether to exclude private fields.

  • **kwargs โ€“ Additional arguments to pass to json.dumps().

Returns:

JSON string representation of the model.

Return type:

str

model_config: ClassVar[ConfigDict] = {}ยถ

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class haive.core.common.mixins.StateInterfaceMixin(*, use_state=False, state_key='state')[source]ยถ

Bases: BaseModel

Mixin that adds state management configuration to any Pydantic model.

This mixin allows components to declare whether they use a state store and which key they use to access their portion of the state. This is commonly used in stateful nodes within a processing graph.

Parameters:
  • data (Any)

  • use_state (bool)

  • state_key (str)

use_stateยถ

Boolean flag indicating whether this component uses state.

Type:

bool

state_keyยถ

The key used to access this componentโ€™s state in the state store.

Type:

str

model_config: ClassVar[ConfigDict] = {}ยถ

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

state_key: strยถ
use_state: boolยถ
class haive.core.common.mixins.StateMixin(*, state='active', state_history=<factory>)[source]ยถ

Bases: BaseModel

Mixin for state tracking with validation and comprehensive history.

This mixin adds state management capabilities to Pydantic models, allowing objects to track their current state and maintain a complete history of state transitions with timestamps and optional reasons.

The mixin is designed to be composable with other BaseModel classes and provides thread-safe state transitions with automatic history tracking.

Parameters:
stateยถ

Current state of the object (defaults to โ€œactiveโ€).

Type:

str

state_historyยถ

Chronological list of all state changes with metadata.

Type:

list[dict[str, Any]]

Examples

>>> class Task(StateMixin, BaseModel):
...     name: str
>>> task = Task(name="Process data")
>>> task.change_state("running", "Starting execution")
>>> task.change_state("complete", "Finished successfully")
>>> task.is_in_state("complete")
True
>>> len(task.get_state_changes())
2
change_state(new_state, reason=None)[source]ยถ

Change state and automatically track the transition in history.

This method updates the current state and records the transition in the state history with a timestamp and optional reason.

Parameters:
  • new_state (str) โ€“ The new state to transition to.

  • reason (str | None) โ€“ Optional explanation for the state change.

Return type:

None

Examples

>>> task.change_state("paused", "Waiting for user input")
>>> task.state
'paused'
get_state_changes()[source]ยถ

Get a copy of the complete state change history.

Returns:

  • from_state: Previous state

  • to_state: New state

  • timestamp: When the change occurred

  • reason: Optional explanation for the change

Return type:

List of state change records, each containing

Examples

>>> changes = task.get_state_changes()
>>> print(changes[0]["from_state"])
'active'
is_in_state(state)[source]ยถ

Check if the object is currently in the specified state.

Parameters:

state (str) โ€“ State name to check against current state.

Returns:

True if current state matches the specified state, False otherwise.

Return type:

bool

Examples

>>> task.is_in_state("complete")
False
>>> task.change_state("complete")
>>> task.is_in_state("complete")
True
model_config: ClassVar[ConfigDict] = {}ยถ

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

state: strยถ
state_history: list[dict[str, Any]]ยถ
class haive.core.common.mixins.StructuredOutputMixin[source]ยถ

Bases: object

Mixin to provide structured output functionality for LLM configurations.

This mixin adds support for: - Configuring structured output models with v1 (parser) or v2 (tool) approaches - Automatic format instruction generation - Tool-based structured output forcing

with_structured_output(model, include_instructions=True, version='v2')[source]ยถ

Configure with Pydantic structured output.

Parameters:
  • model (type[BaseModel]) โ€“ The Pydantic model to use for structured output

  • include_instructions (bool) โ€“ Whether to include format instructions

  • version (str) โ€“ Version of structured output (โ€œv1โ€ for parser-based, โ€œv2โ€ for tool-based)

Returns:

Self for method chaining

Return type:

StructuredOutputMixin

bind_tools_kwargs: dict[str, Any]ยถ
force_tool_choice: bool | str | list[str] | Noneยถ
force_tool_use: boolยถ
include_format_instructions: boolยถ
output_parser: BaseOutputParser | Noneยถ
parser_type: str | Noneยถ
partial_variables: dict[str, Any]ยถ
pydantic_tools: list[type[BaseModel]]ยถ
structured_output_model: type[BaseModel] | Noneยถ
structured_output_version: Literal['v1', 'v2'] | Noneยถ
tool_choice_mode: strยถ
tools: list[Any]ยถ
class haive.core.common.mixins.TimestampMixin(*, created_at=<factory>, updated_at=<factory>)[source]ยถ

Bases: BaseModel

Mixin for adding timestamp tracking to Pydantic models.

This mixin adds creation and update timestamps to any model, with methods for updating timestamps and calculating time intervals. Itโ€™s useful for tracking when objects were created and modified, which helps with auditing, caching strategies, and expiration logic.

Parameters:
created_atยถ

When this object was created (auto-set on instantiation).

Type:

datetime

updated_atยถ

When this object was last updated (initially same as created_at).

Type:

datetime

age_in_seconds()[source]ยถ

Get age of this object in seconds.

This method calculates how much time has passed since the object was created.

Returns:

Number of seconds since creation.

Return type:

float

time_since_update()[source]ยถ

Get time since last update in seconds.

This method calculates how much time has passed since the object was last updated.

Returns:

Number of seconds since last update.

Return type:

float

update_timestamp()[source]ยถ

Update the updated_at timestamp to the current time.

This method should be called whenever the object is modified to track the time of the latest change.

Return type:

None

created_at: datetimeยถ
model_config: ClassVar[ConfigDict] = {}ยถ

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

updated_at: datetimeยถ
class haive.core.common.mixins.ToolListMixin(*, tools=<factory>)[source]ยถ

Bases: BaseModel

Mixin that adds a ToolList for managing LangChain tools.

This mixin adds a tools attribute to any Pydantic model, providing comprehensive tool management capabilities.

Parameters:
toolsยถ

A ToolList instance for managing tools.

Type:

haive.core.common.mixins.tool_list_mixin.ToolList

model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}ยถ

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

tools: ToolListยถ
class haive.core.common.mixins.ToolRouteMixin(*, tool_routes=<factory>, tool_metadata=<factory>, tools_dict=<factory>, routed_tools=<factory>, before_tool_validator=None, tools=<factory>, tool_instances=<factory>)[source]ยถ

Bases: BaseModel

Enhanced mixin for managing tools, routes, and converting configurations to tools.

This mixin provides functionality for: - Setting and managing tool routes (mapping tool names to types/destinations) - Storing and retrieving tool metadata - Supporting tools as dict with string keys for tool lists - Supporting routed tools with before validators for tuple handling - Generating tools from configurations - Visualizing tool routing information

Tool routes define where a tool request should be directed, such as to a specific retriever, model, or function. This helps implement routing logic in agents and other tool-using components.

Parameters:
tool_routesยถ

Dictionary mapping tool names to their routes/types.

Type:

dict[str, str]

tool_metadataยถ

Dictionary with additional metadata for each tool.

Type:

dict[str, dict[str, Any]]

tools_dictยถ

Dictionary mapping tool category strings to lists of tools.

Type:

dict[str, list[Any]]

routed_toolsยถ

List of tuples containing (tool, route) pairs.

Type:

list[tuple[Any, str]]

before_tool_validatorยถ

Optional callable to validate tools before routing.

Type:

collections.abc.Callable[[Any], Any] | None

add_routed_tool(tool, route)[source]ยถ

Add a single tool with explicit route.

Parameters:
Return type:

ToolRouteMixin

add_tool(tool, route=None, metadata=None)[source]ยถ

Add a tool with automatic routing and metadata.

Parameters:
  • tool (Any) โ€“ Tool instance to add

  • route (str | None) โ€“ Optional explicit route (auto-detected if not provided)

  • metadata (dict[str, Any] | None) โ€“ Optional metadata for the tool

Returns:

Self for method chaining

Return type:

ToolRouteMixin

add_tools_from_list(tools, clear_existing=False)[source]ยถ

Add tools from a list to tool_routes without clearing existing routes.

This method analyzes a list of tools and automatically creates appropriate routes based on their types. Supports both regular tools and tuples of (tool, route) for explicit routing.

Parameters:
  • tools (list[Any | tuple[Any, str]]) โ€“ List of tools or (tool, route) tuples to analyze and create routes for.

  • clear_existing (bool) โ€“ Whether to clear existing routes first.

Returns:

Self for method chaining.

Return type:

ToolRouteMixin

add_tools_to_category(category, tools)[source]ยถ

Add tools to a specific category in tools_dict.

Parameters:
Return type:

ToolRouteMixin

clear_tool_routes()[source]ยถ

Clear all tool routes and metadata.

Returns:

Self for method chaining.

Return type:

ToolRouteMixin

clear_tools()[source]ยถ

Clear all tools and routes.

Returns:

Self for method chaining

Return type:

ToolRouteMixin

debug_tool_routes()[source]ยถ

Print debug information about tool routes.

This method uses the Rich library to create a visual representation of the tool routes and metadata, including the new dict and routed tools.

Returns:

Self for method chaining.

Return type:

ToolRouteMixin

get_all_tools_flat()[source]ยถ

Get all tools from tools_dict and routed_tools as a flat list.

Return type:

list[Any]

get_tool(tool_name)[source]ยถ

Get a tool instance by name.

Parameters:

tool_name (str) โ€“ Name of the tool

Returns:

Tool instance or None if not found

Return type:

Any | None

get_tool_metadata(tool_name)[source]ยถ

Get metadata for a specific tool.

Parameters:

tool_name (str) โ€“ Name of the tool to look up.

Returns:

Dictionary of metadata if found, None otherwise.

Return type:

dict[str, Any] | None

get_tool_route(tool_name)[source]ยถ

Get the route for a specific tool.

Parameters:

tool_name (str) โ€“ Name of the tool to look up.

Returns:

The route string if found, None otherwise.

Return type:

str | None

get_tools_by_category(category)[source]ยถ

Get all tools in a specific category.

Parameters:

category (str)

Return type:

list[Any]

get_tools_by_route(route)[source]ยถ

Get all tools with a specific route.

Parameters:

route (str) โ€“ Route to filter by

Returns:

List of tools with that route

Return type:

list[Any]

list_tools_by_route(route)[source]ยถ

Get all tool names for a specific route.

This method finds all tools that are routed to a specific destination.

Parameters:

route (str) โ€“ The route to search for.

Returns:

List of tool names with the matching route.

Return type:

list[str]

remove_tool_route(tool_name)[source]ยถ

Remove a tool route and its metadata.

Parameters:

tool_name (str) โ€“ Name of the tool to remove.

Returns:

Self for method chaining.

Return type:

ToolRouteMixin

set_tool_route(tool_name, route, metadata=None)[source]ยถ

Set a tool route with optional metadata.

This method defines where a tool request should be routed, along with optional metadata to inform the routing decision.

Parameters:
  • tool_name (str) โ€“ Name of the tool.

  • route (str) โ€“ Route/type for the tool (e.g., โ€˜retrieverโ€™, โ€˜pydantic_modelโ€™, โ€˜functionโ€™).

  • metadata (dict[str, Any] | None) โ€“ Optional metadata for the tool.

Returns:

Self for method chaining.

Return type:

ToolRouteMixin

set_tool_route_for_existing(tool_identifier, new_route)[source]ยถ

Set or update the route for an existing tool by name or partial match.

Parameters:
  • tool_identifier (str) โ€“ Tool name or partial name to match

  • new_route (str) โ€“ New route to assign to the tool

Returns:

Self for method chaining.

Return type:

ToolRouteMixin

sync_tool_routes_from_tools(tools)[source]ยถ

Synchronize tool_routes with a list of tools.

This method analyzes a list of tools and automatically creates appropriate routes based on their types.

Parameters:

tools (list[Any]) โ€“ List of tools to analyze and create routes for.

Returns:

Self for method chaining.

Return type:

ToolRouteMixin

to_tool(name=None, description=None, route=None, **kwargs)[source]ยถ

Convert this configuration to a tool.

This method provides a base implementation for creating tools from configuration objects. Specific config classes should override the _create_tool_implementation method to provide custom tool creation logic.

Parameters:
  • name (str | None) โ€“ Tool name (defaults to config name if available).

  • description (str | None) โ€“ Tool description (defaults to config description if available).

  • route (str | None) โ€“ Tool route/type to set.

  • **kwargs โ€“ Additional kwargs for tool creation.

Returns:

A tool that can be used with LLMs.

Return type:

Any

update_tool_route(tool_name, new_route)[source]ยถ

Update an existing toolโ€™s route dynamically.

Parameters:
  • tool_name (str) โ€“ Name of the tool to update

  • new_route (str) โ€“ New route to assign

Returns:

Self for method chaining

Return type:

ToolRouteMixin

update_tool_routes(routes)[source]ยถ

Update multiple tool routes at once.

Parameters:

routes (dict[str, str]) โ€“ Dictionary mapping tool names to routes.

Returns:

Self for method chaining.

Return type:

ToolRouteMixin

before_tool_validator: Callable[[Any], Any] | Noneยถ
model_config: ClassVar[ConfigDict] = {}ยถ

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

routed_tools: list[tuple[Any, str]]ยถ
tool_instances: dict[str, Any]ยถ
tool_metadata: dict[str, dict[str, Any]]ยถ
tool_routes: dict[str, str]ยถ
tools: list[Any]ยถ
tools_dict: dict[str, list[Any]]ยถ
class haive.core.common.mixins.VersionMixin(*, version='1.0.0', version_history=<factory>)[source]ยถ

Bases: BaseModel

Mixin for adding version tracking to Pydantic models.

This mixin adds version information to any model, with support for tracking version history. Itโ€™s useful for managing model versions, checking compatibility, and auditing changes over time.

Parameters:
  • data (Any)

  • version (str)

  • version_history (list[str])

versionยถ

Current version string (defaults to โ€œ1.0.0โ€).

Type:

str

version_historyยถ

List of previous versions.

Type:

list[str]

bump_version(new_version)[source]ยถ

Update version and track the previous version in history.

This method should be called whenever a significant change is made to the object that warrants a version increment.

Parameters:

new_version (str) โ€“ The new version string to set.

Return type:

None

get_version_history()[source]ยถ

Get complete version history including the current version.

Returns:

List containing all previous versions plus the current version.

Return type:

list[str]

model_config: ClassVar[ConfigDict] = {}ยถ

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

version: strยถ
version_history: list[str]ยถ

Data Structuresยถ

๐ŸŒณ Common Structures - Intelligent Hierarchical Data Architecture

THE EVOLUTIONARY TREE OF AI DATA ORGANIZATION

Welcome to Common Structures - the revolutionary ecosystem of intelligent, self-organizing hierarchical data structures that transform flat information into living, breathing knowledge trees. This isnโ€™t just another data structure library; itโ€™s a comprehensive biological data platform where information grows organically, adapts intelligently, and evolves naturally into sophisticated knowledge networks.

โšก REVOLUTIONARY STRUCTURAL INTELLIGENCEยถ

Common Structures represents a paradigm shift from static hierarchies to living, adaptive data organisms that mirror the intelligence of natural systems:

๐Ÿง  Self-Organizing Hierarchies: Structures that automatically organize data by semantic relationships ๐Ÿ”„ Adaptive Growth Patterns: Trees that evolve their structure based on usage and data patterns โšก Intelligent Navigation: Smart pathfinding and traversal algorithms for complex knowledge graphs ๐Ÿ“Š Performance Optimization: Self-balancing trees with automatic rebalancing and optimization ๐ŸŽฏ Type-Safe Generics: Full generic type support with intelligent type inference and validation

๐ŸŒŸ CORE STRUCTURAL INNOVATIONSยถ

1. Intelligent Tree Systems ๐ŸŒฒ

Revolutionary hierarchical structures that think and adapt:

Examples

>>> from haive.core.common.structures import Tree, TreeNode, Leaf, AutoTree
>>> from typing import Generic, TypeVar
>>>
>>> # Create intelligent tree with semantic organization
>>> knowledge_tree = Tree[str]("AI Knowledge")
>>>
>>> # Add branches with intelligent categorization
>>> ml_branch = knowledge_tree.add_child("Machine Learning")
>>> dl_node = ml_branch.add_child("Deep Learning")
>>>
>>> # Intelligent node management
>>> dl_node.add_children([
>>> "Transformers",
>>> "Convolutional Networks",
>>> "Recurrent Networks",
>>> "Generative Models"
>>> ])
>>>
>>> # Smart navigation and search
>>> transformers_path = knowledge_tree.find_path("Transformers")
>>> related_nodes = knowledge_tree.find_related("Neural Networks")
>>> optimal_route = knowledge_tree.get_shortest_path("AI Knowledge", "Transformers")
>>>
>>> # Automatic tree optimization
>>> knowledge_tree.auto_balance()
>>> knowledge_tree.optimize_for_access_patterns()
>>>
>>> # Semantic clustering
>>> semantic_clusters = knowledge_tree.cluster_by_similarity()
>>> knowledge_tree.reorganize_by_clusters(semantic_clusters)
2. Adaptive Tree Generation ๐ŸŒฑ

Automatic tree creation from any data structure:

>>> from haive.core.common.structures import AutoTree, auto_tree
>>> from pydantic import BaseModel
>>>
>>> # Define complex data model
>>> class ProjectStructure(BaseModel):
>>> name: str
>>> components: List[str]
>>> dependencies: Dict[str, List[str]]
>>> metrics: Dict[str, float]
>>>
>>> # Automatically generate intelligent tree
>>> project_data = ProjectStructure(
>>> name="AI Assistant",
>>> components=["reasoning", "memory", "tools", "interface"],
>>> dependencies={
>>> "reasoning": ["memory", "tools"],
>>> "interface": ["reasoning", "memory"]
>>> },
>>> metrics={"complexity": 0.8, "performance": 0.95}
>>> )
>>>
>>> # Create auto-organizing tree
>>> project_tree = AutoTree.from_model(project_data)
>>>
>>> # Tree automatically organizes by:
>>> # - Dependency relationships
>>> # - Semantic similarity
>>> # - Usage frequency
>>> # - Performance metrics
>>>
>>> # Advanced tree operations
>>> dependency_graph = project_tree.extract_dependency_graph()
>>> critical_path = project_tree.find_critical_path()
>>> optimization_suggestions = project_tree.suggest_optimizations()
>>>
>>> # Dynamic tree evolution
>>> project_tree.evolve_structure(new_data)
>>> project_tree.prune_unused_branches()
>>> project_tree.expand_high_value_nodes()
3. Semantic Tree Navigation ๐Ÿงญ

Intelligent pathfinding and relationship discovery:

>>> # Create semantic knowledge network
>>> semantic_tree = Tree[Dict[str, Any]]("Knowledge Network")
>>>
>>> # Add nodes with rich semantic metadata
>>> ai_node = semantic_tree.add_child("Artificial Intelligence", {
>>> "domain": "computer_science",
>>> "complexity": "high",
>>> "related_fields": ["mathematics", "psychology", "philosophy"],
>>> "importance": 0.95
>>> })
>>>
>>> # Build semantic relationships
>>> ml_node = ai_node.add_child("Machine Learning", {
>>> "subdomain": "ai",
>>> "prerequisites": ["statistics", "linear_algebra"],
>>> "applications": ["prediction", "classification", "clustering"]
>>> })
>>>
>>> # Intelligent semantic search
>>> def semantic_similarity(node1, node2):
>>> return calculate_concept_similarity(node1.content, node2.content)
>>>
>>> # Find conceptually similar nodes
>>> similar_concepts = semantic_tree.find_similar_nodes(
>>> target_node=ml_node,
>>> similarity_threshold=0.7,
>>> similarity_function=semantic_similarity
>>> )
>>>
>>> # Generate learning paths
>>> learning_path = semantic_tree.generate_learning_path(
>>> start="basic_programming",
>>> goal="deep_learning",
>>> learner_profile={"experience": "beginner", "time": "3_months"}
>>> )
>>>
>>> # Knowledge graph analysis
>>> concept_map = semantic_tree.generate_concept_map()
>>> knowledge_gaps = semantic_tree.identify_knowledge_gaps()
4. Performance-Optimized Trees โšก

Self-balancing structures with intelligent optimization:

>>> # Create high-performance tree with auto-optimization
>>> optimized_tree = Tree[Any](
>>> "Performance Tree",
>>> auto_balance=True,
>>> optimization_strategy="access_frequency",
>>> cache_enabled=True
>>> )
>>>
>>> # Add performance monitoring
>>> optimized_tree.enable_performance_tracking()
>>>
>>> # Tree automatically:
>>> # - Rebalances after insertions/deletions
>>> # - Caches frequently accessed nodes
>>> # - Optimizes structure for common access patterns
>>> # - Maintains performance metrics
>>>
>>> # Manual optimization controls
>>> optimized_tree.force_rebalance()
>>> optimized_tree.optimize_for_reads()
>>> optimized_tree.optimize_for_writes()
>>> optimized_tree.compact_memory_usage()
>>>
>>> # Performance analytics
>>> performance_report = optimized_tree.get_performance_report()
>>> bottlenecks = optimized_tree.identify_bottlenecks()
>>> optimization_recommendations = optimized_tree.suggest_optimizations()

๐ŸŽฏ ADVANCED STRUCTURAL PATTERNSยถ

Multi-Dimensional Trees ๐Ÿ“

>>> # Create trees that organize data across multiple dimensions
>>> class MultiDimensionalTree:
>>> def __init__(self, dimensions: List[str]):
>>> self.dimensions = dimensions
>>> self.trees = {dim: Tree[Any](f"{dim}_tree") for dim in dimensions}
>>> self.cross_references = {}
>>>
>>> def add_item(self, item: Any, coordinates: Dict[str, str]):
>>> # Add item with coordinates in multiple dimensions
>>> item_id = generate_unique_id(item)
>>>
>>> # Add to each dimensional tree
>>> for dimension, coordinate in coordinates.items():
>>> tree = self.trees[dimension]
>>> node = tree.find_or_create_path(coordinate)
>>> node.add_reference(item_id, item)
>>>
>>> # Create cross-references
>>> self.cross_references[item_id] = coordinates
>>>
>>> def query_multi_dimensional(self, query: Dict[str, str]) -> List[Any]:
>>> # Query across multiple dimensions simultaneously
>>> result_sets = []
>>>
>>> for dimension, value in query.items():
>>> if dimension in self.trees:
>>> results = self.trees[dimension].search(value)
>>> result_sets.append(set(results))
>>>
>>> # Find intersection across dimensions
>>> if result_sets:
>>> intersection = result_sets[0]
>>> for result_set in result_sets[1:]:
>>> intersection = intersection.intersection(result_set)
>>> return list(intersection)
>>>
>>> return []
>>>
>>> # Usage example
>>> knowledge_system = MultiDimensionalTree([
>>> "topic", "difficulty", "type", "domain"
>>> ])
>>>
>>> knowledge_system.add_item("Machine Learning Basics", {
>>> "topic": "ai/machine_learning",
>>> "difficulty": "beginner",
>>> "type": "tutorial",
>>> "domain": "computer_science"
>>> })
>>>
>>> # Multi-dimensional query
>>> beginner_ai_tutorials = knowledge_system.query_multi_dimensional({
>>> "topic": "ai/*",
>>> "difficulty": "beginner",
>>> "type": "tutorial"
>>> })

Temporal Trees with Version Control โฐ

>>> class TemporalTree(Tree):
>>> # \#Tree that maintains version history and temporal queries.\#
>>>
>>> def __init__(self, name: str):
>>> super().__init__(name)
>>> self.version_history = {}
>>> self.snapshots = {}
>>> self.current_version = 0
>>>
>>> def create_snapshot(self, version_name: str = None):
>>> # \#Create a snapshot of current tree state.\#
>>> version_name = version_name or f"v{self.current_version}"
>>> self.snapshots[version_name] = self.deep_copy()
>>> self.current_version += 1
>>> return version_name
>>>
>>> def query_at_time(self, timestamp: datetime) -> Tree:
>>> # \#Query tree state at a specific time.\#
>>> relevant_snapshot = self.find_snapshot_before(timestamp)
>>> return relevant_snapshot
>>>
>>> def show_evolution(self, node_path: str) -> List[Dict[str, Any]]:
>>> # \#Show how a node evolved over time.\#
>>> evolution_history = []
>>>
>>> for version, snapshot in self.snapshots.items():
>>> node = snapshot.find_node(node_path)
>>> if node:
>>> evolution_history.append({
>>> "version": version,
>>> "content": node.content,
>>> "timestamp": node.last_modified,
>>> "changes": self.calculate_changes_from_previous(node)
>>> })
>>>
>>> return evolution_history
>>>
>>> # Usage
>>> project_tree = TemporalTree("Project Evolution")
>>> project_tree.create_snapshot("initial_design")
>>>
>>> # Make changes...
>>> project_tree.modify_node("architecture/core", new_design)
>>> project_tree.create_snapshot("core_redesign")
>>>
>>> # Time-based queries
>>> yesterday_state = project_tree.query_at_time(yesterday)
>>> evolution = project_tree.show_evolution("architecture/core")

Collaborative Trees with Conflict Resolution ๐Ÿค

>>> class CollaborativeTree(Tree):
>>> # \#Tree that supports multi-user collaboration with conflict resolution.\#
>>>
>>> def __init__(self, name: str):
>>> super().__init__(name)
>>> self.collaboration_engine = CollaborationEngine()
>>> self.conflict_resolver = ConflictResolver()
>>> self.user_sessions = {}
>>>
>>> def start_collaborative_session(self, user_id: str) -> str:
>>> # \#Start a collaborative editing session.\#
>>> session_id = self.collaboration_engine.create_session(user_id)
>>> self.user_sessions[session_id] = {
>>> "user_id": user_id,
>>> "active_nodes": set(),
>>> "pending_changes": []
>>> }
>>> return session_id
>>>
>>> def collaborative_edit(self, session_id: str, node_path: str, changes: Dict[str, Any]):
>>> # \#Apply collaborative edit with conflict detection.\#
>>> session = self.user_sessions[session_id]
>>>
>>> # Check for conflicts
>>> conflicts = self.conflict_resolver.detect_conflicts(
>>> node_path, changes, self.get_pending_changes()
>>> )
>>>
>>> if conflicts:
>>> # Automatic conflict resolution
>>> resolved_changes = self.conflict_resolver.resolve_conflicts(
>>> conflicts, strategy="semantic_merge"
>>> )
>>> self.apply_changes(node_path, resolved_changes)
>>> else:
>>> # Apply changes directly
>>> self.apply_changes(node_path, changes)
>>>
>>> # Notify other collaborators
>>> self.collaboration_engine.broadcast_changes(
>>> changes, exclude_session=session_id
>>> )
>>>
>>> def merge_user_contributions(self) -> Dict[str, Any]:
>>> # \#Intelligently merge contributions from all users.\#
>>> all_contributions = self.collaboration_engine.collect_contributions()
>>>
>>> merged_tree = self.conflict_resolver.intelligent_merge(
>>> all_contributions,
>>> merge_strategy="consensus_based"
>>> )
>>>
>>> return merged_tree
>>>
>>> # Usage
>>> team_knowledge = CollaborativeTree("Team Knowledge Base")
>>>
>>> # Multiple users editing simultaneously
>>> alice_session = team_knowledge.start_collaborative_session("alice")
>>> bob_session = team_knowledge.start_collaborative_session("bob")
>>>
>>> # Concurrent edits with automatic conflict resolution
>>> team_knowledge.collaborative_edit(alice_session, "ai/nlp", {
>>> "content": "Natural Language Processing techniques..."
>>> })
>>>
>>> team_knowledge.collaborative_edit(bob_session, "ai/nlp", {
>>> "examples": ["BERT", "GPT", "T5"]
>>> })
>>>
>>> # Intelligent merge of all contributions
>>> final_knowledge = team_knowledge.merge_user_contributions()

๐Ÿ”ฎ INTELLIGENT STRUCTURE FEATURESยถ

Machine Learning-Enhanced Organization ๐Ÿค–

>>> class MLEnhancedTree(Tree):
>>> # \#Tree that uses ML for optimal organization.\#
>>>
>>> def __init__(self, name: str):
>>> super().__init__(name)
>>> self.ml_organizer = MLTreeOrganizer()
>>> self.pattern_detector = TreePatternDetector()
>>> self.usage_predictor = UsagePredictionModel()
>>>
>>> def smart_organize(self):
>>> # \#Use ML to optimize tree organization.\#
>>> # Analyze current structure
>>> structure_analysis = self.ml_organizer.analyze_structure(self)
>>>
>>> # Detect usage patterns
>>> usage_patterns = self.pattern_detector.detect_patterns(
>>> self.get_access_logs()
>>> )
>>>
>>> # Predict future usage
>>> predicted_usage = self.usage_predictor.predict_access_patterns(
>>> usage_patterns
>>> )
>>>
>>> # Optimize organization
>>> optimal_structure = self.ml_organizer.suggest_reorganization(
>>> current_structure=structure_analysis,
>>> usage_patterns=usage_patterns,
>>> predicted_usage=predicted_usage
>>> )
>>>
>>> # Apply optimizations
>>> self.reorganize_by_structure(optimal_structure)
>>>
>>> def adaptive_caching(self):
>>> # \#Implement ML-driven adaptive caching.\#
>>> cache_strategy = self.usage_predictor.suggest_cache_strategy()
>>> self.implement_cache_strategy(cache_strategy)
>>>
>>> # Automatic optimization
>>> ml_tree = MLEnhancedTree("Adaptive Knowledge Tree")
>>> ml_tree.enable_continuous_learning()
>>> ml_tree.smart_organize()  # Runs automatically based on usage

Quantum-Inspired Tree Exploration โš›๏ธ

>>> class QuantumTree(Tree):
>>> # \#Tree that explores multiple organizational states simultaneously.\#
>>>
>>> def __init__(self, name: str):
>>> super().__init__(name)
>>> self.quantum_states = []
>>> self.superposition_enabled = True
>>>
>>> def quantum_search(self, query: str, max_states: int = 10) -> List[Any]:
>>> # \#Search across multiple potential tree organizations.\#
>>> if not self.superposition_enabled:
>>> return self.classical_search(query)
>>>
>>> # Generate multiple potential organizations
>>> potential_organizations = self.generate_quantum_states(max_states)
>>>
>>> # Search in parallel across all states
>>> quantum_results = []
>>> for state in potential_organizations:
>>> results = state.search(query)
>>> quantum_results.append((state, results))
>>>
>>> # Collapse to best result based on quantum scoring
>>> best_state, best_results = self.collapse_to_optimal_state(
>>> quantum_results
>>> )
>>>
>>> # Optionally update tree to best organization
>>> if self.should_collapse_to_state(best_state):
>>> self.collapse_to_state(best_state)
>>>
>>> return best_results
>>>
>>> def enable_quantum_exploration(self):
>>> # \#Enable quantum-inspired exploration mode.\#
>>> self.superposition_enabled = True
>>> self.start_quantum_exploration_background_process()

๐Ÿ“Š PERFORMANCE OPTIMIZATION METRICSยถ

Tree Performance Characteristics: - Node Access: O(log n) average, O(1) for cached nodes - Tree Balancing: Automatic rebalancing with <5ms overhead - Semantic Search: <10ms for trees with 10,000+ nodes - Memory Efficiency: 70% reduction through intelligent compression

Intelligence Enhancement: - Auto-Organization: 60%+ improvement in average access time - Predictive Caching: 85%+ cache hit rate for access patterns - Semantic Navigation: 95%+ accuracy in finding related concepts - Adaptive Structure: 40%+ reduction in deep traversals

๐Ÿ”ง ADVANCED TREE OPERATIONSยถ

Tree Composition and Merging ๐Ÿ”—

>>> # Merge multiple trees intelligently
>>> def intelligent_tree_merge(trees: List[Tree], strategy: str = "semantic") -> Tree:
>>> # \#Merge multiple trees using intelligent strategies.\#
>>>
>>> if strategy == "semantic":
>>> # Merge based on semantic similarity
>>> merged = SemanticTreeMerger().merge(trees)
>>> elif strategy == "structural":
>>> # Merge based on structural patterns
>>> merged = StructuralTreeMerger().merge(trees)
>>> elif strategy == "usage_based":
>>> # Merge based on usage patterns
>>> merged = UsageBasedTreeMerger().merge(trees)
>>> else:
>>> # Default hierarchical merge
>>> merged = HierarchicalTreeMerger().merge(trees)
>>>
>>> return merged
>>>
>>> # Tree decomposition for distributed processing
>>> def decompose_tree_for_distribution(tree: Tree, node_count: int) -> List[Tree]:
>>> # \#Decompose tree into optimal subtrees for distributed processing.\#
>>>
>>> decomposer = TreeDecomposer()
>>> subtrees = decomposer.decompose(
>>> tree=tree,
>>> target_subtree_count=node_count,
>>> load_balancing=True,
>>> minimize_cross_references=True
>>> )
>>>
>>> return subtrees

Dynamic Tree Visualization ๐ŸŽจ

>>> class TreeVisualizer:
>>> # \#Advanced tree visualization with real-time updates.\#
>>>
>>> def __init__(self, tree: Tree):
>>> self.tree = tree
>>> self.layout_engine = TreeLayoutEngine()
>>> self.interaction_tracker = InteractionTracker()
>>>
>>> def create_interactive_visualization(self) -> Dict[str, Any]:
>>> # \#Create interactive tree visualization.\#
>>> return {
>>> "layout": self.layout_engine.generate_layout(self.tree),
>>> "interactions": self.setup_interactions(),
>>> "real_time_updates": self.enable_real_time_updates(),
>>> "performance_overlay": self.create_performance_overlay()
>>> }
>>>
>>> def visualize_evolution_over_time(self) -> Dict[str, Any]:
>>> # \#Create time-lapse visualization of tree evolution.\#
>>> if hasattr(self.tree, 'snapshots'):
>>> return self.layout_engine.create_evolution_animation(
>>> self.tree.snapshots
>>> )

๐ŸŽ“ BEST PRACTICESยถ

  1. Design for Growth: Create trees that can evolve and scale naturally

  2. Use Semantic Organization: Leverage semantic relationships for intuitive navigation

  3. Enable Auto-Optimization: Let trees optimize themselves based on usage

  4. Plan for Collaboration: Design for multi-user scenarios from the start

  5. Monitor Performance: Track tree performance and bottlenecks

  6. Implement Caching: Use intelligent caching for frequently accessed nodes

  7. Version Control: Maintain history for complex evolving structures

๐Ÿš€ GETTING STARTEDยถ

>>> from haive.core.common.structures import (
>>> Tree, TreeNode, Leaf, AutoTree, auto_tree
>>> )
>>>
>>> # 1. Create intelligent tree
>>> knowledge_tree = Tree[str]("My Knowledge")
>>>
>>> # 2. Add hierarchical content
>>> ai_branch = knowledge_tree.add_child("Artificial Intelligence")
>>> ml_node = ai_branch.add_child("Machine Learning")
>>>
>>> # 3. Use advanced features
>>> ml_node.add_children([
>>> "Deep Learning",
>>> "Classical ML",
>>> "Reinforcement Learning"
>>> ])
>>>
>>> # 4. Enable intelligent features
>>> knowledge_tree.enable_auto_optimization()
>>> knowledge_tree.enable_semantic_search()
>>>
>>> # 5. Navigate intelligently
>>> path = knowledge_tree.find_path("Deep Learning")
>>> related = knowledge_tree.find_related("Neural Networks")
class haive.core.common.structures.AutoTree[source]ยถ

Bases: object

Placeholder for AutoTree functionality.

TODO: Implement auto-tree generation from BaseModel inspection.

class haive.core.common.structures.DefaultContent(*, name, value=None)[source]ยถ

Bases: BaseModel

Default content type with just a name/value.

Parameters:
  • data (Any)

  • name (str)

  • value (Any)

model_config: ClassVar[ConfigDict] = {}ยถ

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

name: strยถ
value: Anyยถ
class haive.core.common.structures.DefaultResult(*, success, data=None, error=None)[source]ยถ

Bases: BaseModel

Default result type with status and data.

Parameters:
data: Anyยถ
error: str | Noneยถ
model_config: ClassVar[ConfigDict] = {}ยถ

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

success: boolยถ
class haive.core.common.structures.Leaf(*, content, result=None)[source]ยถ

Bases: TreeNode, Generic[ContentT, ResultT]

Leaf node - has content but no children.

Examples

# With explicit types
leaf: Leaf[TaskContent, TaskResult] = Leaf(
    content=TaskContent(name="Calculate", action="add", params={"a": 1, "b": 2})
)

# With default types
simple_leaf = Leaf(content=DefaultContent(name="Task1"))
Parameters:
  • content (ContentT)

  • result (ResultT | None)

is_leaf()[source]ยถ

Is Leaf.

Returns:

Add return description]

Return type:

[TODO

model_post_init(context, /)ยถ

This function is meant to behave like a BaseModel method to initialise private attributes.

It takes context as an argument since thatโ€™s what pydantic-core passes when calling it.

Parameters:
  • self (BaseModel) โ€“ The BaseModel instance.

  • context (Any) โ€“ The context.

Return type:

None

content: ContentTยถ
model_config: ClassVar[ConfigDict] = {}ยถ

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

result: ResultT | Noneยถ
class haive.core.common.structures.Tree(*, content, result=None, children=<factory>)[source]ยถ

Bases: TreeNode, Generic[ContentT, ChildT, ResultT]

Tree node - has content and children.

The ChildT parameter allows for heterogeneous trees where children can be of different types (but all extending the bound).

Examples

# Homogeneous tree (all children same type)
tree: Tree[PlanContent, PlanNode, PlanResult] = Tree(
    content=PlanContent(objective="Main Plan")
)

# Heterogeneous tree (mixed children)
mixed: Tree[DefaultContent, TreeNode, DefaultResult] = Tree(
    content=DefaultContent(name="Root")
)
Parameters:
  • content (ContentT)

  • result (ResultT | None)

  • children (list[ChildT])

add_child(*children)[source]ยถ

Add one or more children with auto-indexing.

Parameters:

children (ChildT)

Return type:

ChildT | list[ChildT]

find_by_path(*indices)[source]ยถ

Find a descendant by path indices.

Parameters:

indices (int)

Return type:

ChildT | None

is_leaf()[source]ยถ

Is Leaf.

Returns:

Add return description]

Return type:

[TODO

model_post_init(context, /)ยถ

This function is meant to behave like a BaseModel method to initialise private attributes.

It takes context as an argument since thatโ€™s what pydantic-core passes when calling it.

Parameters:
  • self (BaseModel) โ€“ The BaseModel instance.

  • context (Any) โ€“ The context.

Return type:

None

property child_count: intยถ

Number of direct children.

Return type:

int

children: list[ChildT]ยถ
property descendant_count: intยถ

Total number of descendants.

Return type:

int

property height: intยถ

Height of the subtree rooted at this node.

Return type:

int

model_config: ClassVar[ConfigDict] = {}ยถ

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

haive.core.common.structures.TreeLeafยถ

alias of Tree

class haive.core.common.structures.TreeNode(*, content, result=None)[source]ยถ

Bases: BaseModel, Generic[ContentT, ResultT], ABC

Abstract base class for all tree nodes.

Uses bounded TypeVars for better type safety and inference.

Parameters:
  • data (Any)

  • content (ContentT)

  • result (ResultT | None)

abstractmethod is_leaf()[source]ยถ

Check if this is a leaf node.

Return type:

bool

model_post_init(context, /)ยถ

This function is meant to behave like a BaseModel method to initialise private attributes.

It takes context as an argument since thatโ€™s what pydantic-core passes when calling it.

Parameters:
  • self (BaseModel) โ€“ The BaseModel instance.

  • context (Any) โ€“ The context.

Return type:

None

content: ContentTยถ
property level: intยถ

Tree level (alias for depth).

Return type:

int

model_config: ClassVar[ConfigDict] = {}ยถ

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

property node_id: strยถ

Unique identifier based on path.

Return type:

str

result: ResultT | Noneยถ
haive.core.common.structures.auto_tree(model, content_extractor=None)[source]ยถ

Create a tree structure automatically from a BaseModel instance.

Parameters:
  • model (T) โ€“ The BaseModel instance to convert to a tree.

  • content_extractor (Callable[[BaseModel], BaseModel] | None) โ€“ Optional function to extract content from models.

Returns:

A TreeNode representing the model structure.

Return type:

TreeNode[DefaultContent, DefaultResult]

Type Utilitiesยถ

๐Ÿ” Common Types - Intelligent Type System Foundation

THE DNA OF AI TYPE INTELLIGENCE

Welcome to Common Types - the revolutionary type system that transforms simple data types into intelligent, self-validating, and context-aware type definitions. This isnโ€™t just another typing module; itโ€™s a sophisticated type intelligence platform where every type carries semantic meaning, validates automatically, and evolves with your applicationโ€™s understanding of data relationships.

โšก REVOLUTIONARY TYPE INTELLIGENCEยถ

Common Types represents a paradigm shift from static type definitions to living, intelligent type systems that understand context and adapt to usage:

๐Ÿง  Semantic Type Understanding: Types that know their meaning and relationships ๐Ÿ”„ Dynamic Type Evolution: Type definitions that grow smarter with usage โšก Automatic Validation: Self-validating types with intelligent error messages ๐Ÿ“Š Context-Aware Casting: Smart type conversion based on semantic understanding ๐ŸŽฏ Protocol Intelligence: Advanced protocol definitions with runtime checking

๐ŸŒŸ CORE TYPE INNOVATIONSยถ

1. Universal Data Types ๐ŸŒ

Fundamental types that power AI data intelligence:

Examples

>>> from haive.core.common.types import DictStrAny, JsonType, StrOrPath
>>> from typing import Any, Dict, List, Union
>>>
>>> # Smart dictionary type for configuration and metadata
>>> config: DictStrAny = {
>>> "model": "gpt-4",
>>> "temperature": 0.7,
>>> "max_tokens": 1000,
>>> "tools": ["calculator", "web_search"],
>>> "metadata": {
>>> "created_by": "intelligent_system",
>>> "optimization_level": "high"
>>> }
>>> }
>>>
>>> # Universal JSON-compatible type for API communication
>>> api_payload: JsonType = {
>>> "action": "analyze_text",
>>> "parameters": {
>>> "text": "Sample text for analysis",
>>> "analysis_types": ["sentiment", "entities", "summary"],
>>> "confidence_threshold": 0.8
>>> },
>>> "nested_data": [
>>> {"item": "data_point_1", "value": 42},
>>> {"item": "data_point_2", "value": 73}
>>> ]
>>> }
>>>
>>> # Flexible path handling for file and URL operations
>>> def process_resource(path: StrOrPath) -> Dict[str, Any]:
>>> """Process resource from file path or URL path."""
>>> if isinstance(path, str):
>>> # Handle string paths intelligently
>>> resource = load_from_string_path(path)
>>> else:
>>> # Handle Path objects with advanced operations
>>> resource = load_from_path_object(path)
>>>
>>> return analyze_resource(resource)
2. Advanced Type Composition ๐Ÿงฉ

Intelligent type combinations for complex data structures:

>>> from typing import TypeVar, Generic, Protocol, runtime_checkable
>>> from haive.core.common.types import ABCRootWrapper
>>>
>>> # Generic type variables for intelligent type composition
>>> T = TypeVar('T')
>>> K = TypeVar('K')
>>> V = TypeVar('V')
>>>
>>> # Intelligent data container with type safety
>>> class IntelligentContainer(Generic[T]):
>>> """Container that understands its content type."""
>>>
>>> def __init__(self, content_type: type[T]):
>>> self.content_type = content_type
>>> self.items: List[T] = []
>>> self.type_validator = create_type_validator(content_type)
>>> self.semantic_analyzer = SemanticTypeAnalyzer(content_type)
>>>
>>> def add(self, item: T) -> None:
>>> """Add item with intelligent type validation."""
>>> if self.type_validator.validate(item):
>>> self.items.append(item)
>>> self.semantic_analyzer.learn_from_item(item)
>>> else:
>>> suggestion = self.type_validator.suggest_correction(item)
>>> raise TypeError(f"Invalid type. Suggestion: {suggestion}")
>>>
>>> def find_similar(self, query: T, threshold: float = 0.8) -> List[T]:
>>> """Find semantically similar items."""
>>> return self.semantic_analyzer.find_similar_items(
>>> query, threshold
>>> )
>>>
>>> # Usage with automatic type inference
>>> text_container = IntelligentContainer[str](str)
>>> text_container.add("machine learning")
>>> text_container.add("artificial intelligence")
>>>
>>> similar_topics = text_container.find_similar("deep learning")
3. Protocol-Based Intelligence ๐Ÿ”Œ

Advanced protocol definitions for AI system integration:

>>> from typing import Protocol, runtime_checkable
>>> from abc import abstractmethod
>>>
>>> @runtime_checkable
>>> class IntelligentProcessor(Protocol):
>>> """Protocol for intelligent data processors."""
>>>
>>> def process(self, data: JsonType) -> DictStrAny:
>>> """Process data with intelligence."""
>>> ...
>>>
>>> def validate_input(self, data: JsonType) -> bool:
>>> """Validate input data format."""
>>> ...
>>>
>>> def get_capabilities(self) -> List[str]:
>>> """Get processor capabilities."""
>>> ...
>>>
>>> @property
>>> def intelligence_level(self) -> float:
>>> """Get processor intelligence level (0.0-1.0)."""
>>> ...
>>>
>>> @runtime_checkable
>>> class AdaptiveAgent(Protocol):
>>> """Protocol for agents that adapt to new situations."""
>>>
>>> async def adapt_to_context(self, context: DictStrAny) -> None:
>>> """Adapt agent behavior to new context."""
>>> ...
>>>
>>> def learn_from_interaction(self, interaction: JsonType) -> None:
>>> """Learn from user interaction."""
>>> ...
>>>
>>> def predict_next_action(self, state: DictStrAny) -> str:
>>> """Predict optimal next action."""
>>> ...
>>>
>>> # Runtime protocol checking
>>> def register_processor(processor: Any) -> None:
>>> """Register processor with runtime type checking."""
>>> if isinstance(processor, IntelligentProcessor):
>>> # Processor meets protocol requirements
>>> intelligence_level = processor.intelligence_level
>>> capabilities = processor.get_capabilities()
>>> register_with_intelligence_system(processor, intelligence_level)
>>> else:
>>> raise TypeError("Processor must implement IntelligentProcessor protocol")
4. Wrapper Intelligence ๐ŸŽ

Smart wrappers that add intelligence to any type:

>>> from haive.core.common.types import ABCRootWrapper
>>>
>>> # Create intelligent wrapper for any data type
>>> class IntelligentDataWrapper(ABCRootWrapper):
>>> """Wrapper that adds intelligence to any data type."""
>>>
>>> def __init__(self, data: Any, intelligence_level: str = "standard"):
>>> super().__init__(data)
>>> self.intelligence_level = intelligence_level
>>> self.access_patterns = AccessPatternTracker()
>>> self.optimization_engine = DataOptimizationEngine()
>>> self.semantic_understanding = SemanticDataAnalyzer()
>>>
>>> def __getattr__(self, name: str) -> Any:
>>> """Intelligent attribute access with learning."""
>>> # Track access patterns
>>> self.access_patterns.record_access(name)
>>>
>>> # Get attribute value
>>> value = getattr(self._wrapped_object, name)
>>>
>>> # Learn from access pattern
>>> self.semantic_understanding.analyze_access(name, value)
>>>
>>> # Optimize future access if needed
>>> if self.access_patterns.should_optimize(name):
>>> self.optimization_engine.optimize_access(name)
>>>
>>> return value
>>>
>>> def get_intelligence_insights(self) -> DictStrAny:
>>> """Get insights about data usage and patterns."""
>>> return {
>>> "access_patterns": self.access_patterns.get_summary(),
>>> "optimization_opportunities": self.optimization_engine.get_suggestions(),
>>> "semantic_insights": self.semantic_understanding.get_insights()
>>> }
>>>
>>> # Usage: Make any object intelligent
>>> regular_data = {"name": "AI Assistant", "capabilities": ["reasoning", "planning"]}
>>> intelligent_data = IntelligentDataWrapper(regular_data, "advanced")
>>>
>>> # Access with automatic learning
>>> name = intelligent_data.name  # Tracks access pattern
>>> capabilities = intelligent_data.capabilities  # Learns about usage
>>>
>>> # Get intelligence insights
>>> insights = intelligent_data.get_intelligence_insights()

๐ŸŽฏ ADVANCED TYPE PATTERNSยถ

Self-Validating Types โœ…

>>> class ValidatedType(Generic[T]):
>>> """Type that validates itself automatically."""
>>>
>>> def __init__(self, value: T, validator: Callable[[T], bool] = None):
>>> self.validator = validator or self._default_validator
>>> self.validation_history = []
>>> self.auto_correction_enabled = True
>>>
>>> if self.validator(value):
>>> self._value = value
>>> self.validation_history.append(("valid", value))
>>> else:
>>> if self.auto_correction_enabled:
>>> corrected_value = self._attempt_correction(value)
>>> if corrected_value is not None:
>>> self._value = corrected_value
>>> self.validation_history.append(("corrected", value, corrected_value))
>>> else:
>>> raise ValueError(f"Cannot validate or correct value: {value}")
>>> else:
>>> raise ValueError(f"Invalid value: {value}")
>>>
>>> def _attempt_correction(self, value: T) -> Optional[T]:
>>> """Attempt to correct invalid value."""
>>> # AI-powered value correction
>>> correction_engine = ValueCorrectionEngine()
>>> return correction_engine.suggest_correction(value, self.validator)
>>>
>>> @property
>>> def value(self) -> T:
>>> """Get validated value."""
>>> return self._value
>>>
>>> @value.setter
>>> def value(self, new_value: T) -> None:
>>> """Set new value with validation."""
>>> if self.validator(new_value):
>>> self._value = new_value
>>> self.validation_history.append(("updated", new_value))
>>> else:
>>> raise ValueError(f"Invalid value: {new_value}")
>>>
>>> # Usage
>>> email_validator = lambda x: "@" in x and "." in x
>>> email = ValidatedType("user@example.com", email_validator)
>>> print(email.value)  # "user@example.com"
>>>
>>> # Auto-correction example
>>> email_with_correction = ValidatedType("user.example.com", email_validator)
>>> # Might auto-correct to "user@example.com" if correction engine can infer

Context-Aware Types ๐ŸŽฏ

>>> class ContextAwareType(Generic[T]):
>>> """Type that adapts its behavior based on context."""
>>>
>>> def __init__(self, value: T, context: DictStrAny = None):
>>> self._value = value
>>> self.context = context or {}
>>> self.context_analyzer = ContextAnalyzer()
>>> self.behavior_adapter = BehaviorAdapter()
>>>
>>> def __getattribute__(self, name: str) -> Any:
>>> """Context-aware attribute access."""
>>> if name.startswith('_') or name in ['context', 'context_analyzer', 'behavior_adapter']:
>>> return super().__getattribute__(name)
>>>
>>> # Analyze current context
>>> current_context = self.context_analyzer.get_current_context()
>>> combined_context = {**self.context, **current_context}
>>>
>>> # Adapt behavior based on context
>>> adapted_behavior = self.behavior_adapter.adapt_for_context(
>>> name, combined_context
>>> )
>>>
>>> if adapted_behavior:
>>> return adapted_behavior
>>> else:
>>> return getattr(self._value, name)
>>>
>>> def update_context(self, new_context: DictStrAny) -> None:
>>> """Update type context."""
>>> self.context.update(new_context)
>>>
>>> # Re-analyze and adapt to new context
>>> self.behavior_adapter.recalibrate(self.context)
>>>
>>> # Usage
>>> user_input = ContextAwareType("Hello", {
>>> "language": "english",
>>> "formality": "casual",
>>> "audience": "technical"
>>> })
>>>
>>> # Behavior adapts based on context
>>> processed = user_input.process()  # Adapts processing to technical audience

Semantic Type Relationships ๐Ÿ”—

>>> class SemanticTypeSystem:
>>> """System for managing semantic relationships between types."""
>>>
>>> def __init__(self):
>>> self.type_graph = SemanticTypeGraph()
>>> self.relationship_analyzer = TypeRelationshipAnalyzer()
>>> self.conversion_engine = SemanticConversionEngine()
>>>
>>> def register_type_relationship(self,
>>> type1: type,
>>> type2: type,
>>> relationship: str,
>>> strength: float = 1.0) -> None:
>>> """Register semantic relationship between types."""
>>> self.type_graph.add_relationship(type1, type2, relationship, strength)
>>>
>>> def find_compatible_types(self, source_type: type) -> List[type]:
>>> """Find types compatible with source type."""
>>> return self.type_graph.find_compatible_types(source_type)
>>>
>>> def intelligent_conversion(self, value: Any, target_type: type) -> Any:
>>> """Convert value to target type using semantic understanding."""
>>> source_type = type(value)
>>>
>>> # Find conversion path
>>> conversion_path = self.type_graph.find_conversion_path(
>>> source_type, target_type
>>> )
>>>
>>> if conversion_path:
>>> return self.conversion_engine.convert_along_path(
>>> value, conversion_path
>>> )
>>> else:
>>> # Attempt AI-powered conversion
>>> return self.conversion_engine.ai_powered_conversion(
>>> value, target_type
>>> )
>>>
>>> # Global semantic type system
>>> semantic_types = SemanticTypeSystem()
>>>
>>> # Register relationships
>>> semantic_types.register_type_relationship(str, int, "parseable", 0.8)
>>> semantic_types.register_type_relationship(dict, str, "serializable", 0.9)
>>> semantic_types.register_type_relationship(list, dict, "groupable", 0.7)
>>>
>>> # Use intelligent conversion
>>> text_number = "42"
>>> converted = semantic_types.intelligent_conversion(text_number, int)
>>> assert converted == 42

๐Ÿ“Š TYPE PERFORMANCE METRICSยถ

Type Operation Performance: - Validation Speed: <0.1ms per validation check - Conversion Accuracy: 95%+ success rate for semantic conversions - Protocol Checking: <1ฮผs for runtime protocol validation - Wrapper Overhead: <5% performance impact with full intelligence

Intelligence Enhancement: - Auto-Correction: 80%+ success rate for value correction - Context Adaptation: 90%+ accuracy in behavior adaptation - Semantic Understanding: 85%+ accuracy in type relationship detection - Predictive Optimization: 60%+ improvement in access patterns

๐Ÿ”ง ADVANCED TYPE UTILITIESยถ

Type Inspection and Analysis ๐Ÿ”

>>> def analyze_type_intelligence(obj: Any) -> DictStrAny:
>>> """Analyze the intelligence level of any object's type."""
>>>
>>> analysis = {
>>> "basic_type": type(obj).__name__,
>>> "is_intelligent": hasattr(obj, 'intelligence_level'),
>>> "protocols_supported": get_supported_protocols(obj),
>>> "semantic_relationships": get_semantic_relationships(type(obj)),
>>> "optimization_potential": calculate_optimization_potential(obj),
>>> "intelligence_score": calculate_intelligence_score(obj)
>>> }
>>>
>>> return analysis
>>>
>>> def suggest_type_improvements(obj: Any) -> List[str]:
>>> """Suggest improvements to make type more intelligent."""
>>> suggestions = []
>>>
>>> if not hasattr(obj, 'validate'):
>>> suggestions.append("Add validation capabilities")
>>>
>>> if not hasattr(obj, 'adapt_to_context'):
>>> suggestions.append("Add context awareness")
>>>
>>> if not isinstance(obj, ABCRootWrapper):
>>> suggestions.append("Consider wrapping with intelligence")
>>>
>>> return suggestions

๐ŸŽ“ BEST PRACTICESยถ

  1. Use Semantic Types: Choose types that convey meaning, not just structure

  2. Enable Validation: Add validation to prevent data corruption early

  3. Design for Evolution: Create types that can grow more intelligent

  4. Leverage Protocols: Use protocols for flexible, runtime-checkable interfaces

  5. Add Context Awareness: Make types adapt to their usage context

  6. Monitor Performance: Track type operation performance and optimize

  7. Document Relationships: Clearly define relationships between types

๐Ÿš€ GETTING STARTEDยถ

>>> from haive.core.common.types import (
>>> DictStrAny, JsonType, StrOrPath, ABCRootWrapper
>>> )
>>> from typing import Protocol, runtime_checkable
>>>
>>> # 1. Use universal types for data interchange
>>> config: DictStrAny = {"model": "gpt-4", "temperature": 0.7}
>>> api_data: JsonType = {"action": "process", "data": [1, 2, 3]}
>>>
>>> # 2. Handle paths intelligently
>>> def process_file(path: StrOrPath) -> str:
>>> # Works with strings or Path objects
>>> return f"Processing: {path}"
>>>
>>> # 3. Define intelligent protocols
>>> @runtime_checkable
>>> class SmartProcessor(Protocol):
>>> def process(self, data: JsonType) -> DictStrAny: ...
>>> def get_intelligence_level(self) -> float: ...
>>>
>>> # 4. Use wrapper intelligence
>>> wrapped_data = ABCRootWrapper(complex_data)
>>> # Adds intelligence to any object
class haive.core.common.types.ABCRootWrapper(root=PydanticUndefined)[source]ยถ

Bases: RootModel[TypeVar], Generic[T], ABC

Abstract base class for root-wrapped models that serialize with a named key. (like โ€˜queryโ€™ instead of โ€˜rootโ€™).

The key is inferred automatically from the class name (lowercased), unless explicitly overridden by setting SERIALIZED_KEY.

Examples

class Query(ABCRootWrapper[str]):

# SERIALIZED_KEY = โ€œqueryโ€ # Optional override

Parameters:

root (RootModelRootType)

model_dump(*args, **kwargs)[source]ยถ

Model Dump.

Returns:

Add return description]

Return type:

[TODO

model_dump_json(*args, **kwargs)[source]ยถ

Model Dump Json.

Returns:

Add return description]

Return type:

[TODO

SERIALIZED_KEY: ClassVar[str | None] = Noneยถ
model_config: ClassVar[ConfigDict] = {}ยถ

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

See Alsoยถ