Skip to content
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

langchain dumps/loads unable to serialize/deserialize models that inherit from langchain models #30076

Open
5 tasks done
caravin opened this issue Mar 3, 2025 · 0 comments
Open
5 tasks done
Labels
Ɑ: core Related to langchain-core

Comments

@caravin
Copy link

caravin commented Mar 3, 2025

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

Adding below a sample code along with the error stack trace:

>>> from langchain_core.outputs.generation import Generation
>>> class GatewayGeneration(Generation):
...     is_error: bool = False
>>> obj = GatewayGeneration(text="hi")
>>> print(obj)
>>> from langchain_core.load import loads, dumps
>>> print(loads(dumps(obj)))

Error Message and Stack Trace (if applicable)

text='hi'

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[30], line 10
      7 print(obj)
      9 from langchain_core.load import loads, dumps
---> 10 print(loads(dumps(obj)))

File /usr/local/python/python-3.11/std/lib64/python3.11/site-packages/langchain_core/_api/beta_decorator.py:111, in beta.<locals>.beta.<locals>.warning_emitting_wrapper(*args, **kwargs)
    109     warned = True
    110     emit_warning()
--> 111 return wrapped(*args, **kwargs)

File /usr/local/python/python-3.11/std/lib64/python3.11/site-packages/langchain_core/load/load.py:190, in loads(text, secrets_map, valid_namespaces, secrets_from_env, additional_import_mappings)
    162 @beta()
    163 def loads(
    164     text: str,
   (...)
    169     additional_import_mappings: Optional[dict[tuple[str, ...], tuple[str, ...]]] = None,
    170 ) -> Any:
    171     """Revive a LangChain class from a JSON string.
    172     Equivalent to `load(json.loads(text))`.
    173 
   (...)
    188         Revived LangChain objects.
    189     """
--> 190     return json.loads(
    191         text,
    192         object_hook=Reviver(
    193             secrets_map, valid_namespaces, secrets_from_env, additional_import_mappings
    194         ),
    195     )

File /opt/python/python-3.11/lib64/python3.11/json/__init__.py:359, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    357 if parse_constant is not None:
    358     kw['parse_constant'] = parse_constant
--> 359 return cls(**kw).decode(s)

File /opt/python/python-3.11/lib64/python3.11/json/decoder.py:337, in JSONDecoder.decode(self, s, _w)
    332 def decode(self, s, _w=WHITESPACE.match):
    333     """Return the Python representation of ``s`` (a ``str`` instance
    334     containing a JSON document).
    335 
    336     """
--> 337     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338     end = _w(s, end).end()
    339     if end != len(s):

File /opt/python/python-3.11/lib64/python3.11/json/decoder.py:353, in JSONDecoder.raw_decode(self, s, idx)
    344 """Decode a JSON document from ``s`` (a ``str`` beginning with
    345 a JSON document) and return a 2-tuple of the Python
    346 representation and the index in ``s`` where the document ended.
   (...)
    350 
    351 """
    352 try:
--> 353     obj, end = self.scan_once(s, idx)
    354 except StopIteration as err:
    355     raise JSONDecodeError("Expecting value", s, err.value) from None

File /usr/local/python/python-3.11/std/lib64/python3.11/site-packages/langchain_core/load/load.py:142, in Reviver.__call__(self, value)
    136 elif namespace[0] in DISALLOW_LOAD_FROM_PATH:
    137     msg = (
    138         "Trying to deserialize something that cannot "
    139         "be deserialized in current version of langchain-core: "
    140         f"{mapping_key}."
    141     )
--> 142     raise ValueError(msg)
    143 # Otherwise, treat namespace as path.
    144 else:
    145     mod = importlib.import_module(".".join(namespace))

ValueError: Trying to deserialize something that cannot be deserialized in current version of langchain-core: ('langchain', 'schema', 'output', 'GatewayGeneration').

Description

We use langchain heavily in our organization including writing custom wrappers on top of langchain models.
However, langchain loads/dumps methods doesn't seem to support serializing such custom models.
In this case, although the GatewayGeneration is a custom model, it somehow mapping it to langchain's namespace but when trying to load, it unable to find GatewayGeneration.
With completely custom models that do not inherit from langchain ones, we can make the serialization work by provided valid_namespaces argument. But in this case, it is incorrect mapping to a different namespace and resulting in errors.

System Info

Standard langchain==0.3 installation.

@dosubot dosubot bot added the Ɑ: core Related to langchain-core label Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ɑ: core Related to langchain-core
Projects
None yet
Development

No branches or pull requests

1 participant