Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions langextract/providers/gemini_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@
_KEY_IDX = "idx-"
_CACHE_PREFIX = "cache"
_UNSET = object()

def _json_serializer(obj: Any) -> Any:
"""JSON serializer for objects not serializable by default json code."""
if dataclasses.is_dataclass(obj):
return dataclasses.asdict(obj)
if isinstance(obj, enum.Enum):
return obj.value
raise TypeError(f"Object of type {type(obj)} is not JSON serializable")

@dataclasses.dataclass(slots=True, frozen=True)
class BatchConfig:
Expand Down Expand Up @@ -386,7 +392,10 @@ def __init__(self, bucket_name: str, project: str | None = None):

def _compute_hash(self, key_data: dict) -> str:
"""Compute SHA256 hash of the canonicalized request data."""
canonical_json = json.dumps(key_data, sort_keys=True, ensure_ascii=False)
# to prevent crash added default=_json_serializer
canonical_json = json.dumps(
key_data, sort_keys=True, ensure_ascii=False, default=_json_serializer
)
return hashlib.sha256(canonical_json.encode("utf-8")).hexdigest()

def _get_single(self, key_hash: str) -> str | None:
Expand Down
Loading