77import functools
88import logging
99from abc import ABC , abstractmethod
10- from collections .abc import Callable
10+ from collections .abc import Callable , Mapping
1111from concurrent .futures import ThreadPoolExecutor
1212from contextlib import asynccontextmanager , contextmanager
1313from contextvars import copy_context
@@ -1614,6 +1614,9 @@ def configure(
16141614 local_tags : list [str ] | None = None ,
16151615 inheritable_metadata : dict [str , Any ] | None = None ,
16161616 local_metadata : dict [str , Any ] | None = None ,
1617+ * ,
1618+ langsmith_inheritable_metadata : Mapping [str , str ] | None = None ,
1619+ langsmith_inheritable_tags : list [str ] | None = None ,
16171620 ) -> CallbackManager :
16181621 """Configure the callback manager.
16191622
@@ -1625,6 +1628,10 @@ def configure(
16251628 local_tags: The local tags.
16261629 inheritable_metadata: The inheritable metadata.
16271630 local_metadata: The local metadata.
1631+ langsmith_inheritable_metadata: Default inheritable metadata applied
1632+ to any `LangChainTracer` handlers via `set_defaults`.
1633+ langsmith_inheritable_tags: Default inheritable tags applied to any
1634+ `LangChainTracer` handlers via `set_defaults`.
16281635
16291636 Returns:
16301637 The configured callback manager.
@@ -1638,6 +1645,8 @@ def configure(
16381645 inheritable_metadata ,
16391646 local_metadata ,
16401647 verbose = verbose ,
1648+ langsmith_inheritable_metadata = langsmith_inheritable_metadata ,
1649+ langsmith_inheritable_tags = langsmith_inheritable_tags ,
16411650 )
16421651
16431652
@@ -2134,6 +2143,9 @@ def configure(
21342143 local_tags : list [str ] | None = None ,
21352144 inheritable_metadata : dict [str , Any ] | None = None ,
21362145 local_metadata : dict [str , Any ] | None = None ,
2146+ * ,
2147+ langsmith_inheritable_metadata : Mapping [str , str ] | None = None ,
2148+ langsmith_inheritable_tags : list [str ] | None = None ,
21372149 ) -> AsyncCallbackManager :
21382150 """Configure the async callback manager.
21392151
@@ -2145,6 +2157,10 @@ def configure(
21452157 local_tags: The local tags.
21462158 inheritable_metadata: The inheritable metadata.
21472159 local_metadata: The local metadata.
2160+ langsmith_inheritable_metadata: Default inheritable metadata applied
2161+ to any `LangChainTracer` handlers via `set_defaults`.
2162+ langsmith_inheritable_tags: Default inheritable tags applied to any
2163+ `LangChainTracer` handlers via `set_defaults`.
21482164
21492165 Returns:
21502166 The configured async callback manager.
@@ -2158,6 +2174,8 @@ def configure(
21582174 inheritable_metadata ,
21592175 local_metadata ,
21602176 verbose = verbose ,
2177+ langsmith_inheritable_metadata = langsmith_inheritable_metadata ,
2178+ langsmith_inheritable_tags = langsmith_inheritable_tags ,
21612179 )
21622180
21632181
@@ -2304,6 +2322,8 @@ def _configure(
23042322 local_metadata : dict [str , Any ] | None = None ,
23052323 * ,
23062324 verbose : bool = False ,
2325+ langsmith_inheritable_metadata : Mapping [str , str ] | None = None ,
2326+ langsmith_inheritable_tags : list [str ] | None = None ,
23072327) -> T :
23082328 """Configure the callback manager.
23092329
@@ -2316,6 +2336,10 @@ def _configure(
23162336 inheritable_metadata: The inheritable metadata.
23172337 local_metadata: The local metadata.
23182338 verbose: Whether to enable verbose mode.
2339+ langsmith_inheritable_metadata: Default inheritable metadata applied to
2340+ any `LangChainTracer` handlers via `set_defaults`.
2341+ langsmith_inheritable_tags: Default inheritable tags applied to any
2342+ `LangChainTracer` handlers via `set_defaults`.
23192343
23202344 Raises:
23212345 RuntimeError: If `LANGCHAIN_TRACING` is set but `LANGCHAIN_TRACING_V2` is not.
@@ -2440,6 +2464,7 @@ def _configure(
24402464 else tracing_context ["client" ]
24412465 ),
24422466 tags = tracing_tags ,
2467+ metadata = tracing_metadata ,
24432468 )
24442469 callback_manager .add_handler (handler )
24452470 except Exception as e :
@@ -2479,6 +2504,25 @@ def _configure(
24792504 for handler in callback_manager .handlers
24802505 ):
24812506 callback_manager .add_handler (var_handler , inheritable )
2507+ if langsmith_inheritable_metadata or langsmith_inheritable_tags :
2508+ callback_manager .handlers = [
2509+ handler .copy_with_metadata_defaults (
2510+ metadata = langsmith_inheritable_metadata ,
2511+ tags = langsmith_inheritable_tags ,
2512+ )
2513+ if isinstance (handler , LangChainTracer )
2514+ else handler
2515+ for handler in callback_manager .handlers
2516+ ]
2517+ callback_manager .inheritable_handlers = [
2518+ handler .copy_with_metadata_defaults (
2519+ metadata = langsmith_inheritable_metadata ,
2520+ tags = langsmith_inheritable_tags ,
2521+ )
2522+ if isinstance (handler , LangChainTracer )
2523+ else handler
2524+ for handler in callback_manager .inheritable_handlers
2525+ ]
24822526 return callback_manager
24832527
24842528
0 commit comments