Make compatible with langchain-core 0.1 by conditionally importing _StreamingCallbackHandler

This commit is contained in:
Nuno Campos
2025-04-08 10:44:12 -07:00
parent c5b118a672
commit 5b73e38c38
+16 -8
View File
@@ -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