langchain-core 0.3.74__py3-none-any.whl → 0.3.76__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of langchain-core might be problematic. Click here for more details.

Files changed (122) hide show
  1. langchain_core/_api/beta_decorator.py +18 -41
  2. langchain_core/_api/deprecation.py +20 -7
  3. langchain_core/_api/path.py +19 -2
  4. langchain_core/_import_utils.py +7 -0
  5. langchain_core/agents.py +10 -6
  6. langchain_core/beta/runnables/context.py +2 -3
  7. langchain_core/callbacks/base.py +11 -4
  8. langchain_core/callbacks/file.py +13 -2
  9. langchain_core/callbacks/manager.py +129 -78
  10. langchain_core/callbacks/usage.py +4 -2
  11. langchain_core/chat_history.py +10 -12
  12. langchain_core/document_loaders/base.py +34 -9
  13. langchain_core/document_loaders/langsmith.py +3 -0
  14. langchain_core/documents/base.py +36 -11
  15. langchain_core/documents/compressor.py +9 -6
  16. langchain_core/documents/transformers.py +4 -2
  17. langchain_core/embeddings/fake.py +8 -5
  18. langchain_core/env.py +2 -3
  19. langchain_core/example_selectors/base.py +12 -0
  20. langchain_core/exceptions.py +7 -0
  21. langchain_core/globals.py +17 -28
  22. langchain_core/indexing/api.py +56 -44
  23. langchain_core/indexing/base.py +7 -10
  24. langchain_core/indexing/in_memory.py +23 -3
  25. langchain_core/language_models/__init__.py +3 -2
  26. langchain_core/language_models/base.py +64 -39
  27. langchain_core/language_models/chat_models.py +130 -42
  28. langchain_core/language_models/fake_chat_models.py +10 -11
  29. langchain_core/language_models/llms.py +49 -17
  30. langchain_core/load/dump.py +5 -7
  31. langchain_core/load/load.py +15 -1
  32. langchain_core/load/serializable.py +38 -43
  33. langchain_core/memory.py +7 -3
  34. langchain_core/messages/ai.py +36 -16
  35. langchain_core/messages/base.py +13 -6
  36. langchain_core/messages/content_blocks.py +23 -2
  37. langchain_core/messages/human.py +2 -6
  38. langchain_core/messages/modifier.py +1 -1
  39. langchain_core/messages/system.py +2 -6
  40. langchain_core/messages/tool.py +36 -16
  41. langchain_core/messages/utils.py +198 -87
  42. langchain_core/output_parsers/base.py +5 -2
  43. langchain_core/output_parsers/json.py +4 -4
  44. langchain_core/output_parsers/list.py +7 -22
  45. langchain_core/output_parsers/openai_functions.py +3 -0
  46. langchain_core/output_parsers/openai_tools.py +8 -1
  47. langchain_core/output_parsers/pydantic.py +4 -0
  48. langchain_core/output_parsers/string.py +5 -1
  49. langchain_core/output_parsers/transform.py +2 -2
  50. langchain_core/output_parsers/xml.py +23 -22
  51. langchain_core/outputs/chat_generation.py +18 -7
  52. langchain_core/outputs/generation.py +14 -3
  53. langchain_core/outputs/llm_result.py +8 -1
  54. langchain_core/prompt_values.py +10 -4
  55. langchain_core/prompts/base.py +4 -9
  56. langchain_core/prompts/chat.py +88 -61
  57. langchain_core/prompts/dict.py +16 -8
  58. langchain_core/prompts/few_shot.py +9 -11
  59. langchain_core/prompts/few_shot_with_templates.py +5 -1
  60. langchain_core/prompts/image.py +12 -5
  61. langchain_core/prompts/message.py +5 -6
  62. langchain_core/prompts/pipeline.py +13 -8
  63. langchain_core/prompts/prompt.py +22 -8
  64. langchain_core/prompts/string.py +18 -10
  65. langchain_core/prompts/structured.py +7 -2
  66. langchain_core/rate_limiters.py +2 -2
  67. langchain_core/retrievers.py +7 -6
  68. langchain_core/runnables/base.py +842 -567
  69. langchain_core/runnables/branch.py +15 -20
  70. langchain_core/runnables/config.py +11 -17
  71. langchain_core/runnables/configurable.py +34 -19
  72. langchain_core/runnables/fallbacks.py +24 -17
  73. langchain_core/runnables/graph.py +47 -40
  74. langchain_core/runnables/graph_ascii.py +40 -17
  75. langchain_core/runnables/graph_mermaid.py +27 -15
  76. langchain_core/runnables/graph_png.py +27 -31
  77. langchain_core/runnables/history.py +56 -59
  78. langchain_core/runnables/passthrough.py +47 -24
  79. langchain_core/runnables/retry.py +10 -6
  80. langchain_core/runnables/router.py +10 -9
  81. langchain_core/runnables/schema.py +2 -0
  82. langchain_core/runnables/utils.py +51 -89
  83. langchain_core/stores.py +13 -25
  84. langchain_core/structured_query.py +3 -7
  85. langchain_core/sys_info.py +9 -8
  86. langchain_core/tools/base.py +30 -23
  87. langchain_core/tools/convert.py +24 -13
  88. langchain_core/tools/simple.py +35 -3
  89. langchain_core/tools/structured.py +26 -3
  90. langchain_core/tracers/_streaming.py +6 -7
  91. langchain_core/tracers/base.py +2 -2
  92. langchain_core/tracers/context.py +5 -1
  93. langchain_core/tracers/core.py +109 -39
  94. langchain_core/tracers/evaluation.py +22 -26
  95. langchain_core/tracers/event_stream.py +41 -28
  96. langchain_core/tracers/langchain.py +12 -3
  97. langchain_core/tracers/langchain_v1.py +10 -2
  98. langchain_core/tracers/log_stream.py +57 -18
  99. langchain_core/tracers/root_listeners.py +4 -20
  100. langchain_core/tracers/run_collector.py +6 -16
  101. langchain_core/tracers/schemas.py +5 -1
  102. langchain_core/utils/aiter.py +14 -6
  103. langchain_core/utils/env.py +3 -0
  104. langchain_core/utils/function_calling.py +49 -30
  105. langchain_core/utils/interactive_env.py +6 -2
  106. langchain_core/utils/iter.py +11 -3
  107. langchain_core/utils/json.py +5 -2
  108. langchain_core/utils/json_schema.py +15 -5
  109. langchain_core/utils/loading.py +5 -1
  110. langchain_core/utils/mustache.py +24 -15
  111. langchain_core/utils/pydantic.py +32 -4
  112. langchain_core/utils/utils.py +24 -8
  113. langchain_core/vectorstores/base.py +7 -20
  114. langchain_core/vectorstores/in_memory.py +18 -12
  115. langchain_core/vectorstores/utils.py +18 -12
  116. langchain_core/version.py +1 -1
  117. langchain_core-0.3.76.dist-info/METADATA +77 -0
  118. langchain_core-0.3.76.dist-info/RECORD +174 -0
  119. langchain_core-0.3.74.dist-info/METADATA +0 -108
  120. langchain_core-0.3.74.dist-info/RECORD +0 -174
  121. {langchain_core-0.3.74.dist-info → langchain_core-0.3.76.dist-info}/WHEEL +0 -0
  122. {langchain_core-0.3.74.dist-info → langchain_core-0.3.76.dist-info}/entry_points.txt +0 -0
@@ -11,15 +11,7 @@ from abc import ABC, abstractmethod
11
11
  from concurrent.futures import ThreadPoolExecutor
12
12
  from contextlib import asynccontextmanager, contextmanager
13
13
  from contextvars import copy_context
14
- from typing import (
15
- TYPE_CHECKING,
16
- Any,
17
- Callable,
18
- Optional,
19
- TypeVar,
20
- Union,
21
- cast,
22
- )
14
+ from typing import TYPE_CHECKING, Any, Callable, Optional, TypeVar, Union, cast
23
15
  from uuid import UUID
24
16
 
25
17
  from langsmith.run_helpers import get_tracing_context
@@ -36,8 +28,18 @@ from langchain_core.callbacks.base import (
36
28
  ToolManagerMixin,
37
29
  )
38
30
  from langchain_core.callbacks.stdout import StdOutCallbackHandler
31
+ from langchain_core.globals import get_debug
39
32
  from langchain_core.messages import BaseMessage, get_buffer_string
33
+ from langchain_core.tracers.context import (
34
+ _configure_hooks,
35
+ _get_trace_callbacks,
36
+ _get_tracer_project,
37
+ _tracing_v2_is_enabled,
38
+ tracing_v2_callback_var,
39
+ )
40
+ from langchain_core.tracers.langchain import LangChainTracer
40
41
  from langchain_core.tracers.schemas import Run
42
+ from langchain_core.tracers.stdout import ConsoleCallbackHandler
41
43
  from langchain_core.utils.env import env_var_is_set
42
44
 
43
45
  if TYPE_CHECKING:
@@ -54,8 +56,6 @@ logger = logging.getLogger(__name__)
54
56
 
55
57
 
56
58
  def _get_debug() -> bool:
57
- from langchain_core.globals import get_debug
58
-
59
59
  return get_debug()
60
60
 
61
61
 
@@ -92,23 +92,25 @@ def trace_as_chain_group(
92
92
  metadata (dict[str, Any], optional): The metadata to apply to all runs.
93
93
  Defaults to None.
94
94
 
95
- Note: must have LANGCHAIN_TRACING_V2 env var set to true to see the trace in LangSmith.
95
+ .. note:
96
+ Must have ``LANGCHAIN_TRACING_V2`` env var set to true to see the trace in
97
+ LangSmith.
96
98
 
97
- Returns:
98
- CallbackManagerForChainGroup: The callback manager for the chain group.
99
+ Yields:
100
+ The callback manager for the chain group.
99
101
 
100
102
  Example:
101
103
  .. code-block:: python
102
104
 
103
105
  llm_input = "Foo"
104
- with trace_as_chain_group("group_name", inputs={"input": llm_input}) as manager:
106
+ with trace_as_chain_group(
107
+ "group_name", inputs={"input": llm_input}
108
+ ) as manager:
105
109
  # Use the callback manager for the chain group
106
110
  res = llm.invoke(llm_input, {"callbacks": manager})
107
111
  manager.on_chain_end({"output": res})
108
112
 
109
- """ # noqa: E501
110
- from langchain_core.tracers.context import _get_trace_callbacks
111
-
113
+ """
112
114
  cb = _get_trace_callbacks(
113
115
  project_name, example_id, callback_manager=callback_manager
114
116
  )
@@ -160,8 +162,8 @@ async def atrace_as_chain_group(
160
162
 
161
163
  Args:
162
164
  group_name (str): The name of the chain group.
163
- callback_manager (AsyncCallbackManager, optional): The async callback manager to use,
164
- which manages tracing and other callback behavior. Defaults to None.
165
+ callback_manager (AsyncCallbackManager, optional): The async callback manager
166
+ to use, which manages tracing and other callback behavior. Defaults to None.
165
167
  inputs (dict[str, Any], optional): The inputs to the chain group.
166
168
  Defaults to None.
167
169
  project_name (str, optional): The name of the project.
@@ -174,23 +176,25 @@ async def atrace_as_chain_group(
174
176
  metadata (dict[str, Any], optional): The metadata to apply to all runs.
175
177
  Defaults to None.
176
178
 
177
- Returns:
178
- AsyncCallbackManager: The async callback manager for the chain group.
179
+ Yields:
180
+ The async callback manager for the chain group.
179
181
 
180
- Note: must have LANGCHAIN_TRACING_V2 env var set to true to see the trace in LangSmith.
182
+ .. note:
183
+ Must have ``LANGCHAIN_TRACING_V2`` env var set to true to see the trace in
184
+ LangSmith.
181
185
 
182
186
  Example:
183
187
  .. code-block:: python
184
188
 
185
189
  llm_input = "Foo"
186
- async with atrace_as_chain_group("group_name", inputs={"input": llm_input}) as manager:
190
+ async with atrace_as_chain_group(
191
+ "group_name", inputs={"input": llm_input}
192
+ ) as manager:
187
193
  # Use the async callback manager for the chain group
188
194
  res = await llm.ainvoke(llm_input, {"callbacks": manager})
189
195
  await manager.on_chain_end({"output": res})
190
196
 
191
- """ # noqa: E501
192
- from langchain_core.tracers.context import _get_trace_callbacks
193
-
197
+ """
194
198
  cb = _get_trace_callbacks(
195
199
  project_name, example_id, callback_manager=callback_manager
196
200
  )
@@ -234,6 +238,7 @@ def shielded(func: Func) -> Func:
234
238
 
235
239
  Returns:
236
240
  Callable: The shielded function
241
+
237
242
  """
238
243
 
239
244
  @functools.wraps(func)
@@ -252,15 +257,17 @@ def handle_event(
252
257
  ) -> None:
253
258
  """Generic event handler for CallbackManager.
254
259
 
255
- Note: This function is used by LangServe to handle events.
260
+ .. note::
261
+ This function is used by ``LangServe`` to handle events.
256
262
 
257
263
  Args:
258
264
  handlers: The list of handlers that will handle the event.
259
- event_name: The name of the event (e.g., "on_llm_start").
265
+ event_name: The name of the event (e.g., ``'on_llm_start'``).
260
266
  ignore_condition_name: Name of the attribute defined on handler
261
267
  that if True will cause the handler to be skipped for the given event.
262
268
  *args: The arguments to pass to the event handler.
263
269
  **kwargs: The keyword arguments to pass to the event handler
270
+
264
271
  """
265
272
  coros: list[Coroutine[Any, Any, Any]] = []
266
273
 
@@ -415,17 +422,19 @@ async def ahandle_event(
415
422
  *args: Any,
416
423
  **kwargs: Any,
417
424
  ) -> None:
418
- """Async generic event handler for AsyncCallbackManager.
425
+ """Async generic event handler for ``AsyncCallbackManager``.
419
426
 
420
- Note: This function is used by LangServe to handle events.
427
+ .. note::
428
+ This function is used by ``LangServe`` to handle events.
421
429
 
422
430
  Args:
423
431
  handlers: The list of handlers that will handle the event.
424
- event_name: The name of the event (e.g., "on_llm_start").
432
+ event_name: The name of the event (e.g., ``'on_llm_start'``).
425
433
  ignore_condition_name: Name of the attribute defined on handler
426
434
  that if True will cause the handler to be skipped for the given event.
427
435
  *args: The arguments to pass to the event handler.
428
436
  **kwargs: The keyword arguments to pass to the event handler.
437
+
429
438
  """
430
439
  for handler in [h for h in handlers if h.run_inline]:
431
440
  await _ahandle_event_for_handler(
@@ -477,6 +486,7 @@ class BaseRunManager(RunManagerMixin):
477
486
  Defaults to None.
478
487
  inheritable_metadata (Optional[dict[str, Any]]): The inheritable metadata.
479
488
  Defaults to None.
489
+
480
490
  """
481
491
  self.run_id = run_id
482
492
  self.handlers = handlers
@@ -493,6 +503,7 @@ class BaseRunManager(RunManagerMixin):
493
503
 
494
504
  Returns:
495
505
  BaseRunManager: The noop manager.
506
+
496
507
  """
497
508
  return cls(
498
509
  run_id=uuid.uuid4(),
@@ -512,15 +523,12 @@ class RunManager(BaseRunManager):
512
523
  self,
513
524
  text: str,
514
525
  **kwargs: Any,
515
- ) -> Any:
526
+ ) -> None:
516
527
  """Run when a text is received.
517
528
 
518
529
  Args:
519
530
  text (str): The received text.
520
531
  **kwargs (Any): Additional keyword arguments.
521
-
522
- Returns:
523
- Any: The result of the callback.
524
532
  """
525
533
  if not self.handlers:
526
534
  return
@@ -545,6 +553,7 @@ class RunManager(BaseRunManager):
545
553
  Args:
546
554
  retry_state (RetryCallState): The retry state.
547
555
  **kwargs (Any): Additional keyword arguments.
556
+
548
557
  """
549
558
  if not self.handlers:
550
559
  return
@@ -572,6 +581,7 @@ class ParentRunManager(RunManager):
572
581
 
573
582
  Returns:
574
583
  CallbackManager: The child callback manager.
584
+
575
585
  """
576
586
  manager = CallbackManager(handlers=[], parent_run_id=self.run_id)
577
587
  manager.set_handlers(self.inheritable_handlers)
@@ -591,21 +601,19 @@ class AsyncRunManager(BaseRunManager, ABC):
591
601
 
592
602
  Returns:
593
603
  RunManager: The sync RunManager.
604
+
594
605
  """
595
606
 
596
607
  async def on_text(
597
608
  self,
598
609
  text: str,
599
610
  **kwargs: Any,
600
- ) -> Any:
611
+ ) -> None:
601
612
  """Run when a text is received.
602
613
 
603
614
  Args:
604
615
  text (str): The received text.
605
616
  **kwargs (Any): Additional keyword arguments.
606
-
607
- Returns:
608
- Any: The result of the callback.
609
617
  """
610
618
  if not self.handlers:
611
619
  return
@@ -630,6 +638,7 @@ class AsyncRunManager(BaseRunManager, ABC):
630
638
  Args:
631
639
  retry_state (RetryCallState): The retry state.
632
640
  **kwargs (Any): Additional keyword arguments.
641
+
633
642
  """
634
643
  if not self.handlers:
635
644
  return
@@ -657,6 +666,7 @@ class AsyncParentRunManager(AsyncRunManager):
657
666
 
658
667
  Returns:
659
668
  AsyncCallbackManager: The child callback manager.
669
+
660
670
  """
661
671
  manager = AsyncCallbackManager(handlers=[], parent_run_id=self.run_id)
662
672
  manager.set_handlers(self.inheritable_handlers)
@@ -684,6 +694,7 @@ class CallbackManagerForLLMRun(RunManager, LLMManagerMixin):
684
694
  chunk (Optional[Union[GenerationChunk, ChatGenerationChunk]], optional):
685
695
  The chunk. Defaults to None.
686
696
  **kwargs (Any): Additional keyword arguments.
697
+
687
698
  """
688
699
  if not self.handlers:
689
700
  return
@@ -705,6 +716,7 @@ class CallbackManagerForLLMRun(RunManager, LLMManagerMixin):
705
716
  Args:
706
717
  response (LLMResult): The LLM result.
707
718
  **kwargs (Any): Additional keyword arguments.
719
+
708
720
  """
709
721
  if not self.handlers:
710
722
  return
@@ -754,6 +766,7 @@ class AsyncCallbackManagerForLLMRun(AsyncRunManager, LLMManagerMixin):
754
766
 
755
767
  Returns:
756
768
  CallbackManagerForLLMRun: The sync RunManager.
769
+
757
770
  """
758
771
  return CallbackManagerForLLMRun(
759
772
  run_id=self.run_id,
@@ -780,6 +793,7 @@ class AsyncCallbackManagerForLLMRun(AsyncRunManager, LLMManagerMixin):
780
793
  chunk (Optional[Union[GenerationChunk, ChatGenerationChunk]], optional):
781
794
  The chunk. Defaults to None.
782
795
  **kwargs (Any): Additional keyword arguments.
796
+
783
797
  """
784
798
  if not self.handlers:
785
799
  return
@@ -802,6 +816,7 @@ class AsyncCallbackManagerForLLMRun(AsyncRunManager, LLMManagerMixin):
802
816
  Args:
803
817
  response (LLMResult): The LLM result.
804
818
  **kwargs (Any): Additional keyword arguments.
819
+
805
820
  """
806
821
  if not self.handlers:
807
822
  return
@@ -856,6 +871,7 @@ class CallbackManagerForChainRun(ParentRunManager, ChainManagerMixin):
856
871
  Args:
857
872
  outputs (Union[dict[str, Any], Any]): The outputs of the chain.
858
873
  **kwargs (Any): Additional keyword arguments.
874
+
859
875
  """
860
876
  if not self.handlers:
861
877
  return
@@ -880,6 +896,7 @@ class CallbackManagerForChainRun(ParentRunManager, ChainManagerMixin):
880
896
  Args:
881
897
  error (Exception or KeyboardInterrupt): The error.
882
898
  **kwargs (Any): Additional keyword arguments.
899
+
883
900
  """
884
901
  if not self.handlers:
885
902
  return
@@ -894,15 +911,12 @@ class CallbackManagerForChainRun(ParentRunManager, ChainManagerMixin):
894
911
  **kwargs,
895
912
  )
896
913
 
897
- def on_agent_action(self, action: AgentAction, **kwargs: Any) -> Any:
914
+ def on_agent_action(self, action: AgentAction, **kwargs: Any) -> None:
898
915
  """Run when agent action is received.
899
916
 
900
917
  Args:
901
918
  action (AgentAction): The agent action.
902
919
  **kwargs (Any): Additional keyword arguments.
903
-
904
- Returns:
905
- Any: The result of the callback.
906
920
  """
907
921
  if not self.handlers:
908
922
  return
@@ -917,15 +931,12 @@ class CallbackManagerForChainRun(ParentRunManager, ChainManagerMixin):
917
931
  **kwargs,
918
932
  )
919
933
 
920
- def on_agent_finish(self, finish: AgentFinish, **kwargs: Any) -> Any:
934
+ def on_agent_finish(self, finish: AgentFinish, **kwargs: Any) -> None:
921
935
  """Run when agent finish is received.
922
936
 
923
937
  Args:
924
938
  finish (AgentFinish): The agent finish.
925
939
  **kwargs (Any): Additional keyword arguments.
926
-
927
- Returns:
928
- Any: The result of the callback.
929
940
  """
930
941
  if not self.handlers:
931
942
  return
@@ -970,6 +981,7 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
970
981
  Args:
971
982
  outputs (Union[dict[str, Any], Any]): The outputs of the chain.
972
983
  **kwargs (Any): Additional keyword arguments.
984
+
973
985
  """
974
986
  if not self.handlers:
975
987
  return
@@ -995,6 +1007,7 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
995
1007
  Args:
996
1008
  error (Exception or KeyboardInterrupt): The error.
997
1009
  **kwargs (Any): Additional keyword arguments.
1010
+
998
1011
  """
999
1012
  if not self.handlers:
1000
1013
  return
@@ -1009,15 +1022,12 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
1009
1022
  **kwargs,
1010
1023
  )
1011
1024
 
1012
- async def on_agent_action(self, action: AgentAction, **kwargs: Any) -> Any:
1025
+ async def on_agent_action(self, action: AgentAction, **kwargs: Any) -> None:
1013
1026
  """Run when agent action is received.
1014
1027
 
1015
1028
  Args:
1016
1029
  action (AgentAction): The agent action.
1017
1030
  **kwargs (Any): Additional keyword arguments.
1018
-
1019
- Returns:
1020
- Any: The result of the callback.
1021
1031
  """
1022
1032
  if not self.handlers:
1023
1033
  return
@@ -1032,15 +1042,12 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
1032
1042
  **kwargs,
1033
1043
  )
1034
1044
 
1035
- async def on_agent_finish(self, finish: AgentFinish, **kwargs: Any) -> Any:
1045
+ async def on_agent_finish(self, finish: AgentFinish, **kwargs: Any) -> None:
1036
1046
  """Run when agent finish is received.
1037
1047
 
1038
1048
  Args:
1039
1049
  finish (AgentFinish): The agent finish.
1040
1050
  **kwargs (Any): Additional keyword arguments.
1041
-
1042
- Returns:
1043
- Any: The result of the callback.
1044
1051
  """
1045
1052
  if not self.handlers:
1046
1053
  return
@@ -1069,6 +1076,7 @@ class CallbackManagerForToolRun(ParentRunManager, ToolManagerMixin):
1069
1076
  Args:
1070
1077
  output (Any): The output of the tool.
1071
1078
  **kwargs (Any): The keyword arguments to pass to the event handler
1079
+
1072
1080
  """
1073
1081
  if not self.handlers:
1074
1082
  return
@@ -1093,6 +1101,7 @@ class CallbackManagerForToolRun(ParentRunManager, ToolManagerMixin):
1093
1101
  Args:
1094
1102
  error (Exception or KeyboardInterrupt): The error.
1095
1103
  **kwargs (Any): Additional keyword arguments.
1104
+
1096
1105
  """
1097
1106
  if not self.handlers:
1098
1107
  return
@@ -1134,6 +1143,7 @@ class AsyncCallbackManagerForToolRun(AsyncParentRunManager, ToolManagerMixin):
1134
1143
  Args:
1135
1144
  output (Any): The output of the tool.
1136
1145
  **kwargs (Any): Additional keyword arguments.
1146
+
1137
1147
  """
1138
1148
  if not self.handlers:
1139
1149
  return
@@ -1158,6 +1168,7 @@ class AsyncCallbackManagerForToolRun(AsyncParentRunManager, ToolManagerMixin):
1158
1168
  Args:
1159
1169
  error (Exception or KeyboardInterrupt): The error.
1160
1170
  **kwargs (Any): Additional keyword arguments.
1171
+
1161
1172
  """
1162
1173
  if not self.handlers:
1163
1174
  return
@@ -1186,6 +1197,7 @@ class CallbackManagerForRetrieverRun(ParentRunManager, RetrieverManagerMixin):
1186
1197
  Args:
1187
1198
  documents (Sequence[Document]): The retrieved documents.
1188
1199
  **kwargs (Any): Additional keyword arguments.
1200
+
1189
1201
  """
1190
1202
  if not self.handlers:
1191
1203
  return
@@ -1210,6 +1222,7 @@ class CallbackManagerForRetrieverRun(ParentRunManager, RetrieverManagerMixin):
1210
1222
  Args:
1211
1223
  error (BaseException): The error.
1212
1224
  **kwargs (Any): Additional keyword arguments.
1225
+
1213
1226
  """
1214
1227
  if not self.handlers:
1215
1228
  return
@@ -1236,6 +1249,7 @@ class AsyncCallbackManagerForRetrieverRun(
1236
1249
 
1237
1250
  Returns:
1238
1251
  CallbackManagerForRetrieverRun: The sync RunManager.
1252
+
1239
1253
  """
1240
1254
  return CallbackManagerForRetrieverRun(
1241
1255
  run_id=self.run_id,
@@ -1257,6 +1271,7 @@ class AsyncCallbackManagerForRetrieverRun(
1257
1271
  Args:
1258
1272
  documents (Sequence[Document]): The retrieved documents.
1259
1273
  **kwargs (Any): Additional keyword arguments.
1274
+
1260
1275
  """
1261
1276
  if not self.handlers:
1262
1277
  return
@@ -1282,6 +1297,7 @@ class AsyncCallbackManagerForRetrieverRun(
1282
1297
  Args:
1283
1298
  error (BaseException): The error.
1284
1299
  **kwargs (Any): Additional keyword arguments.
1300
+
1285
1301
  """
1286
1302
  if not self.handlers:
1287
1303
  return
@@ -1318,6 +1334,7 @@ class CallbackManager(BaseCallbackManager):
1318
1334
  Returns:
1319
1335
  list[CallbackManagerForLLMRun]: A callback manager for each
1320
1336
  prompt as an LLM run.
1337
+
1321
1338
  """
1322
1339
  managers = []
1323
1340
  for i, prompt in enumerate(prompts):
@@ -1369,6 +1386,7 @@ class CallbackManager(BaseCallbackManager):
1369
1386
  Returns:
1370
1387
  list[CallbackManagerForLLMRun]: A callback manager for each
1371
1388
  list of messages as an LLM run.
1389
+
1372
1390
  """
1373
1391
  managers = []
1374
1392
  for message_list in messages:
@@ -1422,6 +1440,7 @@ class CallbackManager(BaseCallbackManager):
1422
1440
 
1423
1441
  Returns:
1424
1442
  CallbackManagerForChainRun: The callback manager for the chain run.
1443
+
1425
1444
  """
1426
1445
  if run_id is None:
1427
1446
  run_id = uuid.uuid4()
@@ -1476,6 +1495,7 @@ class CallbackManager(BaseCallbackManager):
1476
1495
 
1477
1496
  Returns:
1478
1497
  CallbackManagerForToolRun: The callback manager for the tool run.
1498
+
1479
1499
  """
1480
1500
  if run_id is None:
1481
1501
  run_id = uuid.uuid4()
@@ -1522,6 +1542,9 @@ class CallbackManager(BaseCallbackManager):
1522
1542
  run_id (UUID, optional): The ID of the run. Defaults to None.
1523
1543
  parent_run_id (UUID, optional): The ID of the parent run. Defaults to None.
1524
1544
  **kwargs (Any): Additional keyword arguments.
1545
+
1546
+ Returns:
1547
+ The callback manager for the retriever run.
1525
1548
  """
1526
1549
  if run_id is None:
1527
1550
  run_id = uuid.uuid4()
@@ -1568,7 +1591,11 @@ class CallbackManager(BaseCallbackManager):
1568
1591
  data: The data for the adhoc event.
1569
1592
  run_id: The ID of the run. Defaults to None.
1570
1593
 
1594
+ Raises:
1595
+ ValueError: If additional keyword arguments are passed.
1596
+
1571
1597
  .. versionadded:: 0.2.14
1598
+
1572
1599
  """
1573
1600
  if not self.handlers:
1574
1601
  return
@@ -1623,6 +1650,7 @@ class CallbackManager(BaseCallbackManager):
1623
1650
 
1624
1651
  Returns:
1625
1652
  CallbackManager: The configured callback manager.
1653
+
1626
1654
  """
1627
1655
  return _configure(
1628
1656
  cls,
@@ -1657,6 +1685,7 @@ class CallbackManagerForChainGroup(CallbackManager):
1657
1685
  parent_run_id (Optional[UUID]): The ID of the parent run. Defaults to None.
1658
1686
  parent_run_manager (CallbackManagerForChainRun): The parent run manager.
1659
1687
  **kwargs (Any): Additional keyword arguments.
1688
+
1660
1689
  """
1661
1690
  super().__init__(
1662
1691
  handlers,
@@ -1667,8 +1696,8 @@ class CallbackManagerForChainGroup(CallbackManager):
1667
1696
  self.parent_run_manager = parent_run_manager
1668
1697
  self.ended = False
1669
1698
 
1699
+ @override
1670
1700
  def copy(self) -> CallbackManagerForChainGroup:
1671
- """Copy the callback manager."""
1672
1701
  return self.__class__(
1673
1702
  handlers=self.handlers.copy(),
1674
1703
  inheritable_handlers=self.inheritable_handlers.copy(),
@@ -1697,11 +1726,18 @@ class CallbackManagerForChainGroup(CallbackManager):
1697
1726
 
1698
1727
  .. code-block:: python
1699
1728
 
1700
- from langchain_core.callbacks.manager import CallbackManager, trace_as_chain_group
1729
+ from langchain_core.callbacks.manager import (
1730
+ CallbackManager,
1731
+ trace_as_chain_group,
1732
+ )
1701
1733
  from langchain_core.callbacks.stdout import StdOutCallbackHandler
1702
1734
 
1703
- manager = CallbackManager(handlers=[StdOutCallbackHandler()], tags=["tag2"])
1704
- with trace_as_chain_group("My Group Name", tags=["tag1"]) as group_manager:
1735
+ manager = CallbackManager(
1736
+ handlers=[StdOutCallbackHandler()], tags=["tag2"]
1737
+ )
1738
+ with trace_as_chain_group(
1739
+ "My Group Name", tags=["tag1"]
1740
+ ) as group_manager:
1705
1741
  merged_manager = group_manager.merge(manager)
1706
1742
  print(type(merged_manager))
1707
1743
  # <class 'langchain_core.callbacks.manager.CallbackManagerForChainGroup'>
@@ -1745,6 +1781,7 @@ class CallbackManagerForChainGroup(CallbackManager):
1745
1781
  Args:
1746
1782
  outputs (Union[dict[str, Any], Any]): The outputs of the chain.
1747
1783
  **kwargs (Any): Additional keyword arguments.
1784
+
1748
1785
  """
1749
1786
  self.ended = True
1750
1787
  return self.parent_run_manager.on_chain_end(outputs, **kwargs)
@@ -1759,6 +1796,7 @@ class CallbackManagerForChainGroup(CallbackManager):
1759
1796
  Args:
1760
1797
  error (Exception or KeyboardInterrupt): The error.
1761
1798
  **kwargs (Any): Additional keyword arguments.
1799
+
1762
1800
  """
1763
1801
  self.ended = True
1764
1802
  return self.parent_run_manager.on_chain_error(error, **kwargs)
@@ -2047,6 +2085,9 @@ class AsyncCallbackManager(BaseCallbackManager):
2047
2085
  data: The data for the adhoc event.
2048
2086
  run_id: The ID of the run. Defaults to None.
2049
2087
 
2088
+ Raises:
2089
+ ValueError: If additional keyword arguments are passed.
2090
+
2050
2091
  .. versionadded:: 0.2.14
2051
2092
  """
2052
2093
  if not self.handlers:
@@ -2197,7 +2238,7 @@ class AsyncCallbackManagerForChainGroup(AsyncCallbackManager):
2197
2238
  self.ended = False
2198
2239
 
2199
2240
  def copy(self) -> AsyncCallbackManagerForChainGroup:
2200
- """Copy the async callback manager."""
2241
+ """Return a copy the async callback manager."""
2201
2242
  return self.__class__(
2202
2243
  handlers=self.handlers.copy(),
2203
2244
  inheritable_handlers=self.inheritable_handlers.copy(),
@@ -2219,18 +2260,25 @@ class AsyncCallbackManagerForChainGroup(AsyncCallbackManager):
2219
2260
  from the current object.
2220
2261
 
2221
2262
  Returns:
2222
- AsyncCallbackManagerForChainGroup: A copy of the current AsyncCallbackManagerForChainGroup
2223
- with the handlers, tags, etc. of the other callback manager merged in.
2263
+ A copy of the current AsyncCallbackManagerForChainGroup
2264
+ with the handlers, tags, etc. of the other callback manager merged in.
2224
2265
 
2225
2266
  Example: Merging two callback managers.
2226
2267
 
2227
2268
  .. code-block:: python
2228
2269
 
2229
- from langchain_core.callbacks.manager import CallbackManager, atrace_as_chain_group
2270
+ from langchain_core.callbacks.manager import (
2271
+ CallbackManager,
2272
+ atrace_as_chain_group,
2273
+ )
2230
2274
  from langchain_core.callbacks.stdout import StdOutCallbackHandler
2231
2275
 
2232
- manager = CallbackManager(handlers=[StdOutCallbackHandler()], tags=["tag2"])
2233
- async with atrace_as_chain_group("My Group Name", tags=["tag1"]) as group_manager:
2276
+ manager = CallbackManager(
2277
+ handlers=[StdOutCallbackHandler()], tags=["tag2"]
2278
+ )
2279
+ async with atrace_as_chain_group(
2280
+ "My Group Name", tags=["tag1"]
2281
+ ) as group_manager:
2234
2282
  merged_manager = group_manager.merge(manager)
2235
2283
  print(type(merged_manager))
2236
2284
  # <class 'langchain_core.callbacks.manager.AsyncCallbackManagerForChainGroup'>
@@ -2326,16 +2374,12 @@ def _configure(
2326
2374
  local_metadata (Optional[dict[str, Any]], optional): The local metadata.
2327
2375
  Defaults to None.
2328
2376
 
2377
+ Raises:
2378
+ RuntimeError: If `LANGCHAIN_TRACING` is set but `LANGCHAIN_TRACING_V2` is not.
2379
+
2329
2380
  Returns:
2330
2381
  T: The configured callback manager.
2331
2382
  """
2332
- from langchain_core.tracers.context import (
2333
- _configure_hooks,
2334
- _get_tracer_project,
2335
- _tracing_v2_is_enabled,
2336
- tracing_v2_callback_var,
2337
- )
2338
-
2339
2383
  tracing_context = get_tracing_context()
2340
2384
  tracing_metadata = tracing_context["metadata"]
2341
2385
  tracing_tags = tracing_context["tags"]
@@ -2412,9 +2456,6 @@ def _configure(
2412
2456
  tracer_project = _get_tracer_project()
2413
2457
  debug = _get_debug()
2414
2458
  if verbose or debug or tracing_v2_enabled_:
2415
- from langchain_core.tracers.langchain import LangChainTracer
2416
- from langchain_core.tracers.stdout import ConsoleCallbackHandler
2417
-
2418
2459
  if verbose and not any(
2419
2460
  isinstance(handler, StdOutCallbackHandler)
2420
2461
  for handler in callback_manager.handlers
@@ -2498,6 +2539,10 @@ async def adispatch_custom_event(
2498
2539
  this is not enforced.
2499
2540
  config: Optional config object. Mirrors the async API but not strictly needed.
2500
2541
 
2542
+ Raises:
2543
+ RuntimeError: If there is no parent run ID available to associate
2544
+ the event with.
2545
+
2501
2546
  Example:
2502
2547
 
2503
2548
  .. code-block:: python
@@ -2579,7 +2624,8 @@ async def adispatch_custom_event(
2579
2624
  .. versionadded:: 0.2.15
2580
2625
 
2581
2626
  """
2582
- from langchain_core.runnables.config import (
2627
+ # Import locally to prevent circular imports.
2628
+ from langchain_core.runnables.config import ( # noqa: PLC0415
2583
2629
  ensure_config,
2584
2630
  get_async_callback_manager_for_config,
2585
2631
  )
@@ -2619,6 +2665,10 @@ def dispatch_custom_event(
2619
2665
  this is not enforced.
2620
2666
  config: Optional config object. Mirrors the async API but not strictly needed.
2621
2667
 
2668
+ Raises:
2669
+ RuntimeError: If there is no parent run ID available to associate
2670
+ the event with.
2671
+
2622
2672
  Example:
2623
2673
 
2624
2674
  .. code-block:: python
@@ -2650,7 +2700,8 @@ def dispatch_custom_event(
2650
2700
  .. versionadded:: 0.2.15
2651
2701
 
2652
2702
  """
2653
- from langchain_core.runnables.config import (
2703
+ # Import locally to prevent circular imports.
2704
+ from langchain_core.runnables.config import ( # noqa: PLC0415
2654
2705
  ensure_config,
2655
2706
  get_callback_manager_for_config,
2656
2707
  )