Skip to content

Proposal: Add id field to Message struct for easier stream management #281

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

Open
nallwhy opened this issue Mar 24, 2025 · 5 comments
Open

Comments

@nallwhy
Copy link
Contributor

nallwhy commented Mar 24, 2025

I’d like to propose adding an id field to the Message struct.

When using LangChain to build chat interfaces, it’s common to integrate with LiveView streams for real-time updates. However, the current Message struct doesn’t include an id, which forces developers to manually assign and manage IDs when using streams. This results in extra boilerplate, even though LangChain is intended to provide an abstraction layer.

Given that some providers (e.g., OpenAI) already include message IDs in their responses, we could directly use those IDs where available. For user-generated messages or cases where an ID is missing, a randomly generated UUID could be used to ensure all messages consistently have IDs.

This addition would make it easier to use LiveView streams without additional effort, while keeping LangChain’s abstractions clean and developer-friendly.

@brainlid
Copy link
Owner

Hi @nallwhy! I like and support your idea of adding an id to Message struct. You make a good point for rendering those structs directly using a stream.

@nallwhy
Copy link
Contributor Author

nallwhy commented Mar 25, 2025

Thanks! I’ll put together a draft in the next few days.

@nallwhy
Copy link
Contributor Author

nallwhy commented Mar 25, 2025

@brainlid
One thing we might need to decide: for providers like OpenAI, a single chat completion response can include multiple choices, but only one id is returned for the whole completion.

In that case, should we assign each message its own unique id (e.g., "#{chat_completion_id}_#{index}"), or should we just reuse the same id across all messages in that completion?

Since the relationship between messages and chat completions isn’t always 1:1, this feels a bit tricky. Curious to hear your thoughts.

@brainlid
Copy link
Owner

brainlid commented Mar 25, 2025 via email

@nallwhy
Copy link
Contributor Author

nallwhy commented Mar 26, 2025

@brainlid
In general, message IDs are not sent back to the server.

They’re mainly used when retrieving previous messages via an API like this one:
https://platform.openai.com/docs/api-reference/chat/get

So I don’t think there’s any case where we’d need to send the IDs we generate back to the AI provider.

That said, there’s no strong reason to remove them either. Since the AI provider does return an ID, I think it’s best to just preserve it in case it’s useful in the future.

From what I’ve found, using multiple choices is quite a niche use case. But since OpenAI’s chat_completion can return multiple choices, I think it’s safer to keep the ID even if it gets duplicated during the choice → message conversion process. It’s probably better to keep it as-is rather than try to deduplicate or modify it unnecessarily.

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

No branches or pull requests

2 participants