haive.core.common.mixins.general.metadata¶

Metadata mixin for arbitrary key-value storage.

This module provides a mixin for adding flexible metadata storage to Pydantic models. It enables storing arbitrary key-value pairs as additional information that may not warrant dedicated model fields.

Usage:

from pydantic import BaseModel from haive.core.common.mixins.general import MetadataMixin

class Document(MetadataMixin, BaseModel):

title: str content: str

# Create a document with metadata doc = Document(

title=”Example”, content=”Sample content”, metadata={“author”: “John Doe”, “tags”: [“example”, “sample”]}

)

# Add additional metadata doc.add_metadata(“created_at”, “2025-06-19”)

# Access metadata author = doc.get_metadata(“author”) # “John Doe”

# Check if metadata exists if doc.has_metadata(“tags”):

tags = doc.get_metadata(“tags”)

Classes¶

MetadataMixin

Mixin for adding flexible metadata storage capabilities.

Module Contents¶

class haive.core.common.mixins.general.metadata.MetadataMixin(/, **data)[source]¶

Bases: pydantic.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:

data (Any)

metadata¶

Dictionary containing arbitrary metadata.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

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