Skip to content

Conversation

@AllieRays
Copy link
Contributor

@AllieRays AllieRays commented Oct 27, 2025

Description

Fixes #111 - Similar to #109
This PR fixes a bug in JsonPlusRedisSerializer.dumps() that caused a TypeError when serializing LangChain message objects (e.g., HumanMessage, AIMessage, SystemMessage).


Changes

  • Fixed dumps() method:
    Replaced the call to non-existent super().dumps() with orjson.dumps() using default=self._default, which properly handles LangChain object serialization.
  • Improved loads() method:
    Enhanced error handling to gracefully fall back to msgpack and other formats when JSON parsing fails.
  • Added comprehensive tests:
    Introduced tests/test_jsonplus_serializer_default_handler.py to validate:
    • All LangChain message types serialize/deserialize correctly
    • Lists and nested structures with messages work properly
    • Round-trip serialization with both dumps()/loads() and dumps_typed()/loads_typed()
    • Backwards compatibility with standard JSON-serializable objects

Root Cause

The previous implementation attempted to call super().dumps() when orjson.dumps(obj) raised a TypeError.
However, the parent class JsonPlusSerializer does not define a dumps() method, so LangChain objects always failed to serialize.


Solution

Use the existing _default handler from the parent JsonPlusSerializer class with orjson:

# Example
data = orjson.dumps(obj, default=self._default)

This leverages the parent class’s LangChain serialization logic while maintaining orjson’s performance benefits.

Testing
pytest

All tests pass, confirming that LangChain message objects now serialize and deserialize correctly.

The parent class JsonPlusSerializer doesn't have dumps() or loads() methods,
only dumps_typed() and loads_typed(). This fix corrects the fallback logic
to properly use the parent's typed serialization methods.

Fixes AttributeError: 'super' object has no attribute 'dumps'
The bug was in line 28 where super().dumps(obj) was called, but the parent
class JsonPlusSerializer doesn't have a dumps() method.

The correct fix is to use orjson.dumps() with default=self._default, which
properly delegates LangChain object serialization to the parent's _default
method.
…dler fix

Tests validate that the fix properly handles:
- HumanMessage, AIMessage, SystemMessage, ToolMessage serialization
- Lists of messages (common LangGraph pattern)
- Nested structures with embedded messages
- dumps_typed() method (used by checkpointer)
- Backwards compatibility with non-LangChain objects
- LangChain serialized format revival

All tests pass, confirming the fix works correctly.
Renamed test_fix_standalone.py to test_jsonplus_redis_serializer.py
to better describe what is being tested (the JsonPlusRedisSerializer class).
@bsbodden bsbodden changed the title Fix 111 jsonplus serializer dumps fix(checkpoint): use orjson default handler for efficient LangChain serialization Oct 27, 2025
@bsbodden bsbodden self-requested a review October 27, 2025 17:56
@bsbodden bsbodden changed the title fix(checkpoint): use orjson default handler for efficient LangChain serialization perf(checkpoint): eliminate json.dumps fallback by using orjson default handler Oct 27, 2025
Copy link
Contributor

@bsbodden bsbodden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGMT! Thanks 🚀

@bsbodden bsbodden merged commit 4919e23 into redis-developer:main Oct 27, 2025
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JsonPlusRedisSerializer.dumps() fails with TypeError for LangChain message objects

2 participants