core[patch]: revert change to stream type hint #31501
Merged
+30
−12
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
#31286 included an update to the return type for
BaseChatModel.(a)stream
, fromIterator[BaseMessageChunk]
toIterator[BaseMessage]
.This change is correct, because when streaming is disabled, the stream methods return an iterator of
BaseMessage
, and the inheritance is such that anBaseMessage
is not aBaseMessageChunk
(but the reverse is true).However, LangChain includes a pattern throughout its docs of summing BaseMessageChunks to accumulate a chat model stream. This pattern is implemented in tests for most integration packages and appears in application code. So #31286 introduces mypy errors throughout the ecosystem (or maybe more accurately, it reveals that this pattern does not account for use of the
.stream
method when streaming is disabled).Here we revert just the change to the stream return type to unblock things. A fix for this should address docs + integration packages (or if we elect to just force people to update code, be explicit about that).