From 5b73e38c3888321c97c050c18ff76ffcb2aaf8e6 Mon Sep 17 00:00:00 2001 From: Nuno Campos Date: Tue, 8 Apr 2025 10:44:12 -0700 Subject: [PATCH] Make compatible with langchain-core 0.1 by conditionally importing _StreamingCallbackHandler --- libs/langgraph/langgraph/pregel/__init__.py | 24 ++++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/libs/langgraph/langgraph/pregel/__init__.py b/libs/langgraph/langgraph/pregel/__init__.py index 545c8faf1..1b28260d1 100644 --- a/libs/langgraph/langgraph/pregel/__init__.py +++ b/libs/langgraph/langgraph/pregel/__init__.py @@ -39,7 +39,11 @@ from langchain_core.runnables.utils import ( ConfigurableFieldSpec, get_unique_config_specs, ) -from langchain_core.tracers._streaming import _StreamingCallbackHandler + +try: + from langchain_core.tracers._streaming import _StreamingCallbackHandler +except ImportError: + _StreamingCallbackHandler = None from pydantic import BaseModel from typing_extensions import Self @@ -2529,13 +2533,17 @@ class Pregel(PregelProtocol): run_id=config.get("run_id"), ) # if running from astream_log() run each proc with streaming - do_stream = next( - ( - cast(_StreamingCallbackHandler, h) - for h in run_manager.handlers - if isinstance(h, _StreamingCallbackHandler) - ), - None, + do_stream = ( + next( + ( + cast(_StreamingCallbackHandler, h) # type: ignore + for h in run_manager.handlers + if isinstance(h, _StreamingCallbackHandler) + ), + None, + ) + if _StreamingCallbackHandler is not None + else False ) try: # assign defaults