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

Core: Fix __add__ for concatting two BaseMessageChunk's #29531

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

inf3rnus
Copy link

@inf3rnus inf3rnus commented Feb 1, 2025

Description:

The change allows you to use the overloaded + operator correctly when +ing two BaseMessageChunk subclasses. Without this you must instantiate a subclass for it to work.

Which feels... wrong. Base classes should be decoupled from sub classes and should have in no way a dependency on them.

Issue:

You can't + a BaseMessageChunk with a BaseMessageChunk

e.g. this will explode

from langchain_core.outputs import (
    ChatGenerationChunk,
)
from langchain_core.messages import BaseMessageChunk


chunk1 = ChatGenerationChunk(
    message=BaseMessageChunk(
        type="customChunk",
        content="HI",
    ),
)

chunk2 = ChatGenerationChunk(
    message=BaseMessageChunk(
        type="customChunk",
        content="HI",
    ),
)

# this will throw
new_chunk = chunk1 + chunk2

In case anyone ran into this issue themselves, it's probably best to use the AIMessageChunk:

a la

from langchain_core.outputs import (
    ChatGenerationChunk,
)
from langchain_core.messages import AIMessageChunk


chunk1 = ChatGenerationChunk(
    message=AIMessageChunk(
        content="HI",
    ),
)

chunk2 = ChatGenerationChunk(
    message=AIMessageChunk(
        content="HI",
    ),
)

# No explosion!
new_chunk = chunk1 + chunk2

Dependencies:

None!

Twitter handle:
aaron_vogler

Keeping these for later if need be:

baskaryan
efriis 
eyurtsev
ccurme 
vbarda
hwchase17
baskaryan
efriis

…class

This allows you to use the overloaded `+` operator correctly. Without this you *must* instantiate a subclass. Which feels very very wrong.
Copy link

vercel bot commented Feb 1, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Feb 1, 2025 0:58am

@dosubot dosubot bot added size:XS This PR changes 0-9 lines, ignoring generated files. 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature labels Feb 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature size:XS This PR changes 0-9 lines, ignoring generated files.
Projects
Status: Triage
Development

Successfully merging this pull request may close these issues.

1 participant