Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
eyurtsev committed Sep 14, 2024
1 parent 2cc690b commit 71657c5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
34 changes: 26 additions & 8 deletions langserve/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,26 +161,44 @@ def dumpd(self, obj: Any) -> Any:
"""Convert the given object to a JSON serializable object."""
return orjson.loads(self.dumps(obj))

def loads(self, s: bytes) -> Any:
"""Load the given JSON string."""
return self.loadd(orjson.loads(s))

@abc.abstractmethod
def dumps(self, obj: Any) -> bytes:
"""Dump the given object as a JSON string."""
"""Dump the given object to a JSON byte string."""

@abc.abstractmethod
def loads(self, s: bytes) -> Any:
"""Load the given JSON string."""
def loadd(self, s: bytes) -> Any:
"""Given a python object, load it into a well known object.
def loads(self, s: bytes) -> Any:
"""Load the given JSON string."""
return self.loadd(orjson.loads(s))
The obj represents content that was json loaded from a string, but
not yet validated or converted into a well known object.
"""


class WellKnownLCSerializer(Serializer):
"""A pre-defined serializer for well known LangChain objects.
This is the default serialized used by LangServe for serializing and
de-serializing well known LangChain objects.
If you need to extend the serialization capabilities for your own application,
feel free to create a new instance of the Serializer class and implement
the abstract methods dumps and loadd.
"""

def dumps(self, obj: Any) -> bytes:
"""Dump the given object as a JSON string."""
"""Dump the given object to a JSON byte string."""
return orjson.dumps(obj, default=default)

def loadd(self, obj: Any) -> Any:
"""Load the given object."""
"""Given a python object, load it into a well known object.
The obj represents content that was json loaded from a string, but
not yet validated or converted into a well known object.
"""
return _decode_lc_objects(obj)


Expand Down
27 changes: 15 additions & 12 deletions tests/unit_tests/test_server_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2293,26 +2293,28 @@ class CustomObject:
def __init__(self, x: int) -> None:
self.x = x

class CustomSerializer(Serializer):
def dumpd(self, obj: Any) -> Any:
"""Convert the given object to a JSON serializable object."""
return orjson.loads(orjson.dumps(obj))
def __eq__(self, other) -> bool:
return self.x == other.x

class CustomSerializer(Serializer):
def dumps(self, obj: Any) -> bytes:
"""Dump the given object as a JSON string."""
return orjson.dumps(obj)
if isinstance(obj, CustomObject):
return orjson.dumps({"x": obj.x})
else:
return orjson.dumps(obj)

def loadd(self, obj: Any) -> Any:
"""Load the given object."""
raise NotImplementedError()

def loads(self, s: bytes) -> Any:
"""Load the given JSON string."""
return orjson.loads(s)
if isinstance(obj, bytes):
obj = obj.decode("utf-8")
if obj.get("x"):
return CustomObject(x=obj["x"])
return obj

def foo(x: int) -> Any:
"""Add one to simulate a valid function."""
return 2
return CustomObject(x=5)

app = FastAPI()
server_runnable = RunnableLambda(foo)
Expand All @@ -2322,7 +2324,8 @@ def foo(x: int) -> Any:
app, raise_app_exceptions=True, serializer=CustomSerializer()
) as runnable:
result = await runnable.ainvoke(5)
assert result == {}
assert isinstance(result, CustomObject)
assert result == CustomObject(x=5)


async def test_endpoint_configurations() -> None:
Expand Down

0 comments on commit 71657c5

Please sign in to comment.