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

to/fromMap serialization for ChatMessage, PromptValue & in_memory ChatHistory #652

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

cyberpwnn
Copy link

Hey! I’ve been using LangChain for a few projects now, and whenever I need serialization—be it Firebase, local storage, or something wacky like Protobuf—I always end up basically rebuilding the changing model structure of LangChain. After searching for toJson or toMap in the issue tracker and finding nothing, I figured I’d do something about it.

It looks like the main goal is parity with the Python version of LangChain. My reasoning is that many other languages have reflection or don’t require such detailed typing, so a built-in serialization might not be as big of a deal there. But in Dart, something like this is straightforward and easy to iterate on, so it makes sense.

This PR is super simple and doesn’t interfere with any plans to expand storage features in the framework. It’s just a step toward providing some built-in serialization. From here, you can drop these maps directly into Firestore (or wherever else). I only added toMap/fromMap for what I consider the most-used models: ChatMessage, PromptValue, and ChatHistory (plus all relevant subclasses and field types).

I also tossed in a unit test (a basic encode-reencode-compare test) to confirm the toMap/fromMap flow works as expected. I tried to handle missing required fields gracefully, but generally it shouldn’t be an issue if new fields are added in the future.

The only extra thing to note is that if new fields or new serializable subclasses pop up, they’ll need their own toMap/fromMap implementations. That’s pretty normal, but just calling it out so everything stays in sync.

@davidmigloz davidmigloz self-requested a review February 26, 2025 21:38
@davidmigloz davidmigloz added p:langchain_core langchain_core package. p:langchain langchain package. t:enhancement New feature or request labels Feb 26, 2025
@cyberpwnn
Copy link
Author

Any eta on when / if this will land?

@davidmigloz
Copy link
Owner

hey @cyberpwnn!

Thanks for the PR, it makes sense. I'm aiming to issue a new release next week, I'll try to get this in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p:langchain_core langchain_core package. p:langchain langchain package. t:enhancement New feature or request
Projects
Status: 📋 Backlog
Development

Successfully merging this pull request may close these issues.

2 participants