Skip to content

Commit 5301409

Browse files
authored
Supporting bytes in stored documents (#997)
1 parent a88bb6a commit 5301409

File tree

1 file changed

+1
-19
lines changed

1 file changed

+1
-19
lines changed

paperqa/agents/search.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
import zlib
1313
from collections import Counter
1414
from collections.abc import AsyncIterator, Callable, Sequence
15-
from datetime import datetime
1615
from enum import StrEnum, auto
1716
from typing import TYPE_CHECKING, Any, ClassVar
18-
from uuid import UUID
1917

2018
import anyio
2119
from pydantic import BaseModel
@@ -62,22 +60,6 @@ class AsyncRetryError(Exception):
6260
"""Flags a retry for another tenacity attempt."""
6361

6462

65-
class RobustEncoder(json.JSONEncoder):
66-
"""JSON encoder that can handle UUID and set objects."""
67-
68-
def default(self, o):
69-
if isinstance(o, UUID):
70-
# if the obj is uuid, we simply return the value of uuid
71-
return str(o)
72-
if isinstance(o, set):
73-
return list(o)
74-
if isinstance(o, os.PathLike):
75-
return str(o)
76-
if isinstance(o, datetime):
77-
return o.isoformat()
78-
return json.JSONEncoder.default(self, o)
79-
80-
8163
class SearchDocumentStorage(StrEnum):
8264
"""Method to serialize a document."""
8365

@@ -95,7 +77,7 @@ def extension(self) -> str:
9577
def write_to_string(self, data: BaseModel | SupportsPickle) -> bytes:
9678
if self == SearchDocumentStorage.JSON_MODEL_DUMP:
9779
if isinstance(data, BaseModel):
98-
return json.dumps(data.model_dump(), cls=RobustEncoder).encode("utf-8")
80+
return data.model_dump_json().encode("utf-8")
9981
raise ValueError("JSON_MODEL_DUMP requires a BaseModel object.")
10082
if self == SearchDocumentStorage.PICKLE_COMPRESSED:
10183
return zlib.compress(pickle.dumps(data))

0 commit comments

Comments
 (0)