Skip to content

LangChain integration #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ pymitter = ">=0.4,<1.1"
typing-extensions = ">=4.11,<4.14"
types-protobuf = "6.30.2.20250506"
pympler = "1.1"
numpy = "2.0.2"
numpy = [
{version = ">=1.26.4", python = "<3.13"},
{version = ">=2.1.0", python = ">=3.13"}
]

[tool.poetry.dev-dependencies]
pytest = "~8.3"
Expand Down
25 changes: 22 additions & 3 deletions src/coherence/serialization.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Copyright (c) 2022, 2023, Oracle and/or its affiliates.
# Copyright (c) 2022, 2025, Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at
# https://oss.oracle.com/licenses/upl.

from __future__ import annotations

import collections
import json
from abc import ABC, abstractmethod
from decimal import Decimal
from typing import Any, Callable, Dict, Final, Optional, Type, TypeVar, cast
Expand Down Expand Up @@ -70,25 +71,43 @@ def __init__(self) -> None:
self._pickler = JavaProxyPickler()
self._unpickler = JavaProxyUnpickler()

def _to_json_from_object(self, obj: object) -> str:
jsn = jsonpickle.encode(obj, context=self._pickler)
return jsn

def serialize(self, obj: object) -> bytes:
jsn: str = jsonpickle.encode(obj, context=self._pickler)
jsn: str = self._to_json_from_object(obj)
b: bytes = MAGIC_BYTE + jsn.encode()
return b

def _to_object_from_json(self, json_str: str) -> T: # type: ignore
o = jsonpickle.decode(json_str, context=self._unpickler)
return o

def deserialize(self, value: bytes) -> T: # type: ignore
if isinstance(value, bytes):
s = value.decode()
if value.__len__() == 0: # empty string
return cast(T, None)
else:
if ord(s[0]) == ord(MAGIC_BYTE):
r = jsonpickle.decode(s[1:], context=self._unpickler)
r: T = self._to_object_from_json(s[1:])
return r
else:
raise ValueError("Invalid JSON serialization format")
else:
return cast(T, value)

def flatten_to_dict(self, o: object) -> dict[Any, Any]:
jsn = self._to_json_from_object(o)
d = json.loads(jsn)
return d

def restore_to_object(self, the_dict: dict[Any, Any]) -> T: # type: ignore
jsn = json.dumps(the_dict)
o: T = self._to_object_from_json(jsn)
return o


class SerializerRegistry:
_singleton: SerializerRegistry
Expand Down