diff --git a/libs/core/langchain_core/callbacks/manager.py b/libs/core/langchain_core/callbacks/manager.py index cd26f49f5b6..31aa2ac156f 100644 --- a/libs/core/langchain_core/callbacks/manager.py +++ b/libs/core/langchain_core/callbacks/manager.py @@ -94,7 +94,9 @@ def trace_as_chain_group( manager.on_chain_end({"output": res}) ``` """ - from langchain_core.tracers.context import _get_trace_callbacks # noqa: PLC0415 + from langchain_core.tracers.context import ( # noqa: PLC0415 -- deferred to avoid importing langsmith at module level + _get_trace_callbacks, + ) cb = _get_trace_callbacks( project_name, example_id, callback_manager=callback_manager @@ -175,7 +177,9 @@ async def atrace_as_chain_group( await manager.on_chain_end({"output": res}) ``` """ - from langchain_core.tracers.context import _get_trace_callbacks # noqa: PLC0415 + from langchain_core.tracers.context import ( # noqa: PLC0415 -- deferred to avoid importing langsmith at module level + _get_trace_callbacks, + ) cb = _get_trace_callbacks( project_name, example_id, callback_manager=callback_manager @@ -2319,6 +2323,7 @@ def _configure( Returns: The configured callback manager. """ + # Deferred to avoid importing langsmith at module level (~132ms). from langsmith.run_helpers import get_tracing_context # noqa: PLC0415 from langchain_core.tracers.context import ( # noqa: PLC0415 diff --git a/libs/core/langchain_core/runnables/config.py b/libs/core/langchain_core/runnables/config.py index 2a19dea8e21..b538ff9fa05 100644 --- a/libs/core/langchain_core/runnables/config.py +++ b/libs/core/langchain_core/runnables/config.py @@ -158,6 +158,7 @@ def _set_config_context( Returns: The token to reset the config and the previous tracing context. """ + # Deferred to avoid importing langsmith at module level (~132ms). from langsmith.run_helpers import ( # noqa: PLC0415 _set_tracing_context, get_tracing_context, @@ -199,6 +200,7 @@ def set_config_context(config: RunnableConfig) -> Generator[Context, None, None] Yields: The config context. """ + # Deferred to avoid importing langsmith at module level (~132ms). from langsmith.run_helpers import _set_tracing_context # noqa: PLC0415 ctx = copy_context()