Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

refactor: replace xxhash with stdlib hashlib.sha256 (#9734)

Isaac Miller8568571955a2
diff --git a/dspy/utils/hasher.py b/dspy/utils/hasher.py
index bfde976e4..3f93f0167 100644
--- a/dspy/utils/hasher.py
+++ b/dspy/utils/hasher.py
@@ -1,8 +1,7 @@
+import hashlib
from pickle import dumps
from typing import Any
-import xxhash
-
"""
The following class was pulled from the `datasets` package from Hugging Face.
The reason for vendoring this code is to avoid a hard dependency on `datasets`,
@@ -12,6 +11,7 @@ License: Apache License 2.0
Author: Hugging Face Inc.
URL: https://github.com/huggingface/datasets/blob/fa73ab472eecf9136a3daf7a0fbff16a3dffa7a6/src/datasets/fingerprint.py#L170
Changes: 2025-08-10 - Ran ruff to format the code to DSPy styles.
+ 2026-05-07 - Replaced xxhash with hashlib.sha256 to remove C-extension dep.
"""
class Hasher:
"""Hasher that accepts python objects as inputs."""
@@ -19,8 +19,7 @@ class Hasher:
dispatch: dict = {}
def __init__(self):
- """Initialize an empty xxhash64 hasher state."""
- self.m = xxhash.xxh64()
+ self.m = hashlib.sha256()
@classmethod
def hash_bytes(cls, value: bytes | list[bytes]) -> str:
@@ -30,10 +29,10 @@ class Hasher:
value: A single bytes object or a list of bytes to hash in order.
Returns:
- The xxhash64 hexadecimal digest.
+ The hexadecimal digest.
"""
value = [value] if isinstance(value, bytes) else value
- m = xxhash.xxh64()
+ m = hashlib.sha256()
for x in value:
m.update(x)
return m.hexdigest()
@@ -46,7 +45,7 @@ class Hasher:
value: Any pickle-serializable Python object.
Returns:
- The xxhash64 hexadecimal digest of the serialized object.
+ The hexadecimal digest of the serialized object.
"""
return cls.hash_bytes(dumps(value))
diff --git a/pyproject.toml b/pyproject.toml
index e47d53449..e2e4d67b9 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -35,7 +35,6 @@ dependencies = [
"cachetools>=5.5.0",
"cloudpickle>=3.1.2",
"numpy>=1.26.0",
- "xxhash>=3.5.0",
"gepa[dspy]==0.1.1",
"typeguard==4.5.1",
]
diff --git a/uv.lock b/uv.lock
index 2a5be5ce6..24b2782b3 100644
--- a/uv.lock
+++ b/uv.lock
@@ -758,7 +758,6 @@ dependencies = [
{ name = "tenacity" },
{ name = "tqdm" },
{ name = "typeguard" },
- { name = "xxhash" },
]
[package.optional-dependencies]
@@ -837,7 +836,6 @@ requires-dist = [
{ name = "tqdm", specifier = ">=4.66.1" },
{ name = "typeguard", specifier = "==4.5.1" },
{ name = "weaviate-client", marker = "extra == 'weaviate'", specifier = "~=4.5.4" },
- { name = "xxhash", specifier = ">=3.5.0" },
]
provides-extras = ["anthropic", "weaviate", "mcp", "langchain", "optuna", "dev", "test-extras"]