Hash nested model fields by immutable value

This commit is contained in:
Andras Schmelczer 2026-10-27 20:09:29 +00:00
commit 48324403bb
2 changed files with 29 additions and 1 deletions

View file

@ -3,4 +3,6 @@ from pydantic import BaseModel
class HashableBaseModel(BaseModel): class HashableBaseModel(BaseModel):
def __hash__(self) -> int: def __hash__(self) -> int:
return hash((type(self),) + tuple(self.__dict__.values())) from ..helper.frozen_call import make_hashable
return hash(make_hashable(self))

View file

@ -0,0 +1,26 @@
from great_ai import ClassificationOutput, MultiLabelClassificationOutput, Trace
def test_output_models_hash_nested_mutable_fields_by_value() -> None:
first = MultiLabelClassificationOutput(
labels=[
ClassificationOutput(
label="a", confidence=0.5, explanation={"tokens": ["a", "b"]}
)
]
)
second = first.model_copy(deep=True)
assert hash(first) == hash(second)
assert len({first, second}) == 1
def test_trace_hash_supports_its_dictionary_and_list_fields() -> None:
trace: Trace = Trace(
trace_id="id",
created="2026-01-01",
original_execution_time_ms=0,
logged_values={"x": [1, 2]},
models=[],
tags=["online"],
)
assert hash(trace) == hash(trace.model_copy(deep=True))