Skip to content

feat: add str-fallback for custom types in msgpack serialization#7500

Draft
open-swe[bot] wants to merge 1 commit into
mainfrom
open-swe/a2e2088a-1fd1-eb4d-a880-5469f1ebb7b5
Draft

feat: add str-fallback for custom types in msgpack serialization#7500
open-swe[bot] wants to merge 1 commit into
mainfrom
open-swe/a2e2088a-1fd1-eb4d-a880-5469f1ebb7b5

Conversation

@open-swe

@open-swe open-swe Bot commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

Description

Adds a duck-type fallback in _msgpack_default for objects that define a custom __str__ method. Before raising TypeError for unserializable types, the serializer now checks if the object's class overrides __str__ (i.e., type(obj).__str__ is not object.__str__), and if so, stores (module, classname, str(obj)) as an EXT_CONSTRUCTOR_SINGLE_ARG. This enables round-trip serialization of types like bson.ObjectId without adding any new dependencies.

Resolves https://forum.langchain.com/t/objectid-handling-by-msgpack-in-mongodb-checkpointer/3360

Test Plan

  • Verify round-trip serialization of objects with custom __str__ (new test: test_serde_str_reconstructable_roundtrip)
  • Verify objects without custom __str__ still raise TypeError (new test: test_serde_str_fallback_not_triggered_for_plain_object)

Opened collaboratively by William FH and open-swe.

Objects whose class defines __str__ (not inherited from object) are now
serialized as EXT_CONSTRUCTOR_SINGLE_ARG with str(obj), enabling
round-trip serde for types like bson.ObjectId without new dependencies.

Co-authored-by: William FH <13333726+hinthornw@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant