langchain-core 0.3.79__py3-none-any.whl → 1.0.0__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.
Files changed (165) hide show
  1. langchain_core/__init__.py +1 -1
  2. langchain_core/_api/__init__.py +3 -4
  3. langchain_core/_api/beta_decorator.py +23 -26
  4. langchain_core/_api/deprecation.py +52 -65
  5. langchain_core/_api/path.py +3 -6
  6. langchain_core/_import_utils.py +3 -4
  7. langchain_core/agents.py +19 -19
  8. langchain_core/caches.py +53 -63
  9. langchain_core/callbacks/__init__.py +1 -8
  10. langchain_core/callbacks/base.py +323 -334
  11. langchain_core/callbacks/file.py +44 -44
  12. langchain_core/callbacks/manager.py +441 -507
  13. langchain_core/callbacks/stdout.py +29 -30
  14. langchain_core/callbacks/streaming_stdout.py +32 -32
  15. langchain_core/callbacks/usage.py +60 -57
  16. langchain_core/chat_history.py +48 -63
  17. langchain_core/document_loaders/base.py +23 -23
  18. langchain_core/document_loaders/langsmith.py +37 -37
  19. langchain_core/documents/__init__.py +0 -1
  20. langchain_core/documents/base.py +62 -65
  21. langchain_core/documents/compressor.py +4 -4
  22. langchain_core/documents/transformers.py +28 -29
  23. langchain_core/embeddings/fake.py +50 -54
  24. langchain_core/example_selectors/length_based.py +1 -1
  25. langchain_core/example_selectors/semantic_similarity.py +21 -25
  26. langchain_core/exceptions.py +10 -11
  27. langchain_core/globals.py +3 -151
  28. langchain_core/indexing/api.py +61 -66
  29. langchain_core/indexing/base.py +58 -58
  30. langchain_core/indexing/in_memory.py +3 -3
  31. langchain_core/language_models/__init__.py +14 -27
  32. langchain_core/language_models/_utils.py +270 -84
  33. langchain_core/language_models/base.py +55 -162
  34. langchain_core/language_models/chat_models.py +442 -402
  35. langchain_core/language_models/fake.py +11 -11
  36. langchain_core/language_models/fake_chat_models.py +61 -39
  37. langchain_core/language_models/llms.py +123 -231
  38. langchain_core/load/dump.py +4 -5
  39. langchain_core/load/load.py +18 -28
  40. langchain_core/load/mapping.py +2 -4
  41. langchain_core/load/serializable.py +39 -40
  42. langchain_core/messages/__init__.py +61 -22
  43. langchain_core/messages/ai.py +368 -163
  44. langchain_core/messages/base.py +214 -43
  45. langchain_core/messages/block_translators/__init__.py +111 -0
  46. langchain_core/messages/block_translators/anthropic.py +470 -0
  47. langchain_core/messages/block_translators/bedrock.py +94 -0
  48. langchain_core/messages/block_translators/bedrock_converse.py +297 -0
  49. langchain_core/messages/block_translators/google_genai.py +530 -0
  50. langchain_core/messages/block_translators/google_vertexai.py +21 -0
  51. langchain_core/messages/block_translators/groq.py +143 -0
  52. langchain_core/messages/block_translators/langchain_v0.py +301 -0
  53. langchain_core/messages/block_translators/openai.py +1010 -0
  54. langchain_core/messages/chat.py +2 -6
  55. langchain_core/messages/content.py +1423 -0
  56. langchain_core/messages/function.py +6 -10
  57. langchain_core/messages/human.py +41 -38
  58. langchain_core/messages/modifier.py +2 -2
  59. langchain_core/messages/system.py +38 -28
  60. langchain_core/messages/tool.py +96 -103
  61. langchain_core/messages/utils.py +478 -504
  62. langchain_core/output_parsers/__init__.py +1 -14
  63. langchain_core/output_parsers/base.py +58 -61
  64. langchain_core/output_parsers/json.py +7 -8
  65. langchain_core/output_parsers/list.py +5 -7
  66. langchain_core/output_parsers/openai_functions.py +49 -47
  67. langchain_core/output_parsers/openai_tools.py +14 -19
  68. langchain_core/output_parsers/pydantic.py +12 -13
  69. langchain_core/output_parsers/string.py +2 -2
  70. langchain_core/output_parsers/transform.py +15 -17
  71. langchain_core/output_parsers/xml.py +8 -10
  72. langchain_core/outputs/__init__.py +1 -1
  73. langchain_core/outputs/chat_generation.py +18 -18
  74. langchain_core/outputs/chat_result.py +1 -3
  75. langchain_core/outputs/generation.py +8 -8
  76. langchain_core/outputs/llm_result.py +10 -10
  77. langchain_core/prompt_values.py +12 -12
  78. langchain_core/prompts/__init__.py +3 -27
  79. langchain_core/prompts/base.py +45 -55
  80. langchain_core/prompts/chat.py +254 -313
  81. langchain_core/prompts/dict.py +5 -5
  82. langchain_core/prompts/few_shot.py +81 -88
  83. langchain_core/prompts/few_shot_with_templates.py +11 -13
  84. langchain_core/prompts/image.py +12 -14
  85. langchain_core/prompts/loading.py +6 -8
  86. langchain_core/prompts/message.py +3 -3
  87. langchain_core/prompts/prompt.py +24 -39
  88. langchain_core/prompts/string.py +4 -4
  89. langchain_core/prompts/structured.py +42 -50
  90. langchain_core/rate_limiters.py +51 -60
  91. langchain_core/retrievers.py +49 -190
  92. langchain_core/runnables/base.py +1484 -1709
  93. langchain_core/runnables/branch.py +45 -61
  94. langchain_core/runnables/config.py +80 -88
  95. langchain_core/runnables/configurable.py +117 -134
  96. langchain_core/runnables/fallbacks.py +83 -79
  97. langchain_core/runnables/graph.py +85 -95
  98. langchain_core/runnables/graph_ascii.py +27 -28
  99. langchain_core/runnables/graph_mermaid.py +38 -50
  100. langchain_core/runnables/graph_png.py +15 -16
  101. langchain_core/runnables/history.py +135 -148
  102. langchain_core/runnables/passthrough.py +124 -150
  103. langchain_core/runnables/retry.py +46 -51
  104. langchain_core/runnables/router.py +25 -30
  105. langchain_core/runnables/schema.py +79 -74
  106. langchain_core/runnables/utils.py +62 -68
  107. langchain_core/stores.py +81 -115
  108. langchain_core/structured_query.py +8 -8
  109. langchain_core/sys_info.py +27 -29
  110. langchain_core/tools/__init__.py +1 -14
  111. langchain_core/tools/base.py +179 -187
  112. langchain_core/tools/convert.py +131 -139
  113. langchain_core/tools/render.py +10 -10
  114. langchain_core/tools/retriever.py +11 -11
  115. langchain_core/tools/simple.py +19 -24
  116. langchain_core/tools/structured.py +30 -39
  117. langchain_core/tracers/__init__.py +1 -9
  118. langchain_core/tracers/base.py +97 -99
  119. langchain_core/tracers/context.py +29 -52
  120. langchain_core/tracers/core.py +50 -60
  121. langchain_core/tracers/evaluation.py +11 -11
  122. langchain_core/tracers/event_stream.py +115 -70
  123. langchain_core/tracers/langchain.py +21 -21
  124. langchain_core/tracers/log_stream.py +43 -43
  125. langchain_core/tracers/memory_stream.py +3 -3
  126. langchain_core/tracers/root_listeners.py +16 -16
  127. langchain_core/tracers/run_collector.py +2 -4
  128. langchain_core/tracers/schemas.py +0 -129
  129. langchain_core/tracers/stdout.py +3 -3
  130. langchain_core/utils/__init__.py +1 -4
  131. langchain_core/utils/_merge.py +46 -8
  132. langchain_core/utils/aiter.py +57 -61
  133. langchain_core/utils/env.py +9 -9
  134. langchain_core/utils/function_calling.py +89 -191
  135. langchain_core/utils/html.py +7 -8
  136. langchain_core/utils/input.py +6 -6
  137. langchain_core/utils/interactive_env.py +1 -1
  138. langchain_core/utils/iter.py +37 -42
  139. langchain_core/utils/json.py +4 -3
  140. langchain_core/utils/json_schema.py +8 -8
  141. langchain_core/utils/mustache.py +9 -11
  142. langchain_core/utils/pydantic.py +33 -35
  143. langchain_core/utils/strings.py +5 -5
  144. langchain_core/utils/usage.py +1 -1
  145. langchain_core/utils/utils.py +80 -54
  146. langchain_core/vectorstores/base.py +129 -164
  147. langchain_core/vectorstores/in_memory.py +99 -174
  148. langchain_core/vectorstores/utils.py +5 -5
  149. langchain_core/version.py +1 -1
  150. {langchain_core-0.3.79.dist-info → langchain_core-1.0.0.dist-info}/METADATA +28 -27
  151. langchain_core-1.0.0.dist-info/RECORD +172 -0
  152. {langchain_core-0.3.79.dist-info → langchain_core-1.0.0.dist-info}/WHEEL +1 -1
  153. langchain_core/beta/__init__.py +0 -1
  154. langchain_core/beta/runnables/__init__.py +0 -1
  155. langchain_core/beta/runnables/context.py +0 -447
  156. langchain_core/memory.py +0 -120
  157. langchain_core/messages/content_blocks.py +0 -176
  158. langchain_core/prompts/pipeline.py +0 -138
  159. langchain_core/pydantic_v1/__init__.py +0 -30
  160. langchain_core/pydantic_v1/dataclasses.py +0 -23
  161. langchain_core/pydantic_v1/main.py +0 -23
  162. langchain_core/tracers/langchain_v1.py +0 -31
  163. langchain_core/utils/loading.py +0 -35
  164. langchain_core-0.3.79.dist-info/RECORD +0 -174
  165. langchain_core-0.3.79.dist-info/entry_points.txt +0 -4
@@ -2,7 +2,7 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
- from typing import TYPE_CHECKING, Any, Optional
5
+ from typing import TYPE_CHECKING, Any
6
6
 
7
7
  from typing_extensions import override
8
8
 
@@ -16,11 +16,11 @@ if TYPE_CHECKING:
16
16
  class StdOutCallbackHandler(BaseCallbackHandler):
17
17
  """Callback Handler that prints to std out."""
18
18
 
19
- def __init__(self, color: Optional[str] = None) -> None:
19
+ def __init__(self, color: str | None = None) -> None:
20
20
  """Initialize callback handler.
21
21
 
22
22
  Args:
23
- color: The color to use for the text. Defaults to None.
23
+ color: The color to use for the text.
24
24
  """
25
25
  self.color = color
26
26
 
@@ -31,9 +31,9 @@ class StdOutCallbackHandler(BaseCallbackHandler):
31
31
  """Print out that we are entering a chain.
32
32
 
33
33
  Args:
34
- serialized (dict[str, Any]): The serialized chain.
35
- inputs (dict[str, Any]): The inputs to the chain.
36
- **kwargs (Any): Additional keyword arguments.
34
+ serialized: The serialized chain.
35
+ inputs: The inputs to the chain.
36
+ **kwargs: Additional keyword arguments.
37
37
  """
38
38
  if "name" in kwargs:
39
39
  name = kwargs["name"]
@@ -48,21 +48,21 @@ class StdOutCallbackHandler(BaseCallbackHandler):
48
48
  """Print out that we finished a chain.
49
49
 
50
50
  Args:
51
- outputs (dict[str, Any]): The outputs of the chain.
52
- **kwargs (Any): Additional keyword arguments.
51
+ outputs: The outputs of the chain.
52
+ **kwargs: Additional keyword arguments.
53
53
  """
54
54
  print("\n\033[1m> Finished chain.\033[0m") # noqa: T201
55
55
 
56
56
  @override
57
57
  def on_agent_action(
58
- self, action: AgentAction, color: Optional[str] = None, **kwargs: Any
58
+ self, action: AgentAction, color: str | None = None, **kwargs: Any
59
59
  ) -> Any:
60
60
  """Run on agent action.
61
61
 
62
62
  Args:
63
- action (AgentAction): The agent action.
64
- color (Optional[str]): The color to use for the text. Defaults to None.
65
- **kwargs (Any): Additional keyword arguments.
63
+ action: The agent action.
64
+ color: The color to use for the text.
65
+ **kwargs: Additional keyword arguments.
66
66
  """
67
67
  print_text(action.log, color=color or self.color)
68
68
 
@@ -70,20 +70,19 @@ class StdOutCallbackHandler(BaseCallbackHandler):
70
70
  def on_tool_end(
71
71
  self,
72
72
  output: Any,
73
- color: Optional[str] = None,
74
- observation_prefix: Optional[str] = None,
75
- llm_prefix: Optional[str] = None,
73
+ color: str | None = None,
74
+ observation_prefix: str | None = None,
75
+ llm_prefix: str | None = None,
76
76
  **kwargs: Any,
77
77
  ) -> None:
78
78
  """If not the final action, print out observation.
79
79
 
80
80
  Args:
81
- output (Any): The output to print.
82
- color (Optional[str]): The color to use for the text. Defaults to None.
83
- observation_prefix (Optional[str]): The observation prefix.
84
- Defaults to None.
85
- llm_prefix (Optional[str]): The LLM prefix. Defaults to None.
86
- **kwargs (Any): Additional keyword arguments.
81
+ output: The output to print.
82
+ color: The color to use for the text.
83
+ observation_prefix: The observation prefix.
84
+ llm_prefix: The LLM prefix.
85
+ **kwargs: Additional keyword arguments.
87
86
  """
88
87
  output = str(output)
89
88
  if observation_prefix is not None:
@@ -96,29 +95,29 @@ class StdOutCallbackHandler(BaseCallbackHandler):
96
95
  def on_text(
97
96
  self,
98
97
  text: str,
99
- color: Optional[str] = None,
98
+ color: str | None = None,
100
99
  end: str = "",
101
100
  **kwargs: Any,
102
101
  ) -> None:
103
102
  """Run when the agent ends.
104
103
 
105
104
  Args:
106
- text (str): The text to print.
107
- color (Optional[str]): The color to use for the text. Defaults to None.
108
- end (str): The end character to use. Defaults to "".
109
- **kwargs (Any): Additional keyword arguments.
105
+ text: The text to print.
106
+ color: The color to use for the text.
107
+ end: The end character to use.
108
+ **kwargs: Additional keyword arguments.
110
109
  """
111
110
  print_text(text, color=color or self.color, end=end)
112
111
 
113
112
  @override
114
113
  def on_agent_finish(
115
- self, finish: AgentFinish, color: Optional[str] = None, **kwargs: Any
114
+ self, finish: AgentFinish, color: str | None = None, **kwargs: Any
116
115
  ) -> None:
117
116
  """Run on the agent end.
118
117
 
119
118
  Args:
120
- finish (AgentFinish): The agent finish.
121
- color (Optional[str]): The color to use for the text. Defaults to None.
122
- **kwargs (Any): Additional keyword arguments.
119
+ finish: The agent finish.
120
+ color: The color to use for the text.
121
+ **kwargs: Additional keyword arguments.
123
122
  """
124
123
  print_text(finish.log, color=color or self.color, end="\n")
@@ -24,9 +24,9 @@ class StreamingStdOutCallbackHandler(BaseCallbackHandler):
24
24
  """Run when LLM starts running.
25
25
 
26
26
  Args:
27
- serialized (dict[str, Any]): The serialized LLM.
28
- prompts (list[str]): The prompts to run.
29
- **kwargs (Any): Additional keyword arguments.
27
+ serialized: The serialized LLM.
28
+ prompts: The prompts to run.
29
+ **kwargs: Additional keyword arguments.
30
30
  """
31
31
 
32
32
  def on_chat_model_start(
@@ -38,9 +38,9 @@ class StreamingStdOutCallbackHandler(BaseCallbackHandler):
38
38
  """Run when LLM starts running.
39
39
 
40
40
  Args:
41
- serialized (dict[str, Any]): The serialized LLM.
42
- messages (list[list[BaseMessage]]): The messages to run.
43
- **kwargs (Any): Additional keyword arguments.
41
+ serialized: The serialized LLM.
42
+ messages: The messages to run.
43
+ **kwargs: Additional keyword arguments.
44
44
  """
45
45
 
46
46
  @override
@@ -48,8 +48,8 @@ class StreamingStdOutCallbackHandler(BaseCallbackHandler):
48
48
  """Run on new LLM token. Only available when streaming is enabled.
49
49
 
50
50
  Args:
51
- token (str): The new token.
52
- **kwargs (Any): Additional keyword arguments.
51
+ token: The new token.
52
+ **kwargs: Additional keyword arguments.
53
53
  """
54
54
  sys.stdout.write(token)
55
55
  sys.stdout.flush()
@@ -58,16 +58,16 @@ class StreamingStdOutCallbackHandler(BaseCallbackHandler):
58
58
  """Run when LLM ends running.
59
59
 
60
60
  Args:
61
- response (LLMResult): The response from the LLM.
62
- **kwargs (Any): Additional keyword arguments.
61
+ response: The response from the LLM.
62
+ **kwargs: Additional keyword arguments.
63
63
  """
64
64
 
65
65
  def on_llm_error(self, error: BaseException, **kwargs: Any) -> None:
66
66
  """Run when LLM errors.
67
67
 
68
68
  Args:
69
- error (BaseException): The error that occurred.
70
- **kwargs (Any): Additional keyword arguments.
69
+ error: The error that occurred.
70
+ **kwargs: Additional keyword arguments.
71
71
  """
72
72
 
73
73
  def on_chain_start(
@@ -76,25 +76,25 @@ class StreamingStdOutCallbackHandler(BaseCallbackHandler):
76
76
  """Run when a chain starts running.
77
77
 
78
78
  Args:
79
- serialized (dict[str, Any]): The serialized chain.
80
- inputs (dict[str, Any]): The inputs to the chain.
81
- **kwargs (Any): Additional keyword arguments.
79
+ serialized: The serialized chain.
80
+ inputs: The inputs to the chain.
81
+ **kwargs: Additional keyword arguments.
82
82
  """
83
83
 
84
84
  def on_chain_end(self, outputs: dict[str, Any], **kwargs: Any) -> None:
85
85
  """Run when a chain ends running.
86
86
 
87
87
  Args:
88
- outputs (dict[str, Any]): The outputs of the chain.
89
- **kwargs (Any): Additional keyword arguments.
88
+ outputs: The outputs of the chain.
89
+ **kwargs: Additional keyword arguments.
90
90
  """
91
91
 
92
92
  def on_chain_error(self, error: BaseException, **kwargs: Any) -> None:
93
93
  """Run when chain errors.
94
94
 
95
95
  Args:
96
- error (BaseException): The error that occurred.
97
- **kwargs (Any): Additional keyword arguments.
96
+ error: The error that occurred.
97
+ **kwargs: Additional keyword arguments.
98
98
  """
99
99
 
100
100
  def on_tool_start(
@@ -103,47 +103,47 @@ class StreamingStdOutCallbackHandler(BaseCallbackHandler):
103
103
  """Run when the tool starts running.
104
104
 
105
105
  Args:
106
- serialized (dict[str, Any]): The serialized tool.
107
- input_str (str): The input string.
108
- **kwargs (Any): Additional keyword arguments.
106
+ serialized: The serialized tool.
107
+ input_str: The input string.
108
+ **kwargs: Additional keyword arguments.
109
109
  """
110
110
 
111
111
  def on_agent_action(self, action: AgentAction, **kwargs: Any) -> Any:
112
112
  """Run on agent action.
113
113
 
114
114
  Args:
115
- action (AgentAction): The agent action.
116
- **kwargs (Any): Additional keyword arguments.
115
+ action: The agent action.
116
+ **kwargs: Additional keyword arguments.
117
117
  """
118
118
 
119
119
  def on_tool_end(self, output: Any, **kwargs: Any) -> None:
120
120
  """Run when tool ends running.
121
121
 
122
122
  Args:
123
- output (Any): The output of the tool.
124
- **kwargs (Any): Additional keyword arguments.
123
+ output: The output of the tool.
124
+ **kwargs: Additional keyword arguments.
125
125
  """
126
126
 
127
127
  def on_tool_error(self, error: BaseException, **kwargs: Any) -> None:
128
128
  """Run when tool errors.
129
129
 
130
130
  Args:
131
- error (BaseException): The error that occurred.
132
- **kwargs (Any): Additional keyword arguments.
131
+ error: The error that occurred.
132
+ **kwargs: Additional keyword arguments.
133
133
  """
134
134
 
135
135
  def on_text(self, text: str, **kwargs: Any) -> None:
136
136
  """Run on an arbitrary text.
137
137
 
138
138
  Args:
139
- text (str): The text to print.
140
- **kwargs (Any): Additional keyword arguments.
139
+ text: The text to print.
140
+ **kwargs: Additional keyword arguments.
141
141
  """
142
142
 
143
143
  def on_agent_finish(self, finish: AgentFinish, **kwargs: Any) -> None:
144
144
  """Run on the agent end.
145
145
 
146
146
  Args:
147
- finish (AgentFinish): The agent finish.
148
- **kwargs (Any): Additional keyword arguments.
147
+ finish: The agent finish.
148
+ **kwargs: Additional keyword arguments.
149
149
  """
@@ -4,7 +4,7 @@ import threading
4
4
  from collections.abc import Generator
5
5
  from contextlib import contextmanager
6
6
  from contextvars import ContextVar
7
- from typing import Any, Optional
7
+ from typing import Any
8
8
 
9
9
  from typing_extensions import override
10
10
 
@@ -19,32 +19,31 @@ class UsageMetadataCallbackHandler(BaseCallbackHandler):
19
19
  """Callback Handler that tracks AIMessage.usage_metadata.
20
20
 
21
21
  Example:
22
- .. code-block:: python
23
-
24
- from langchain.chat_models import init_chat_model
25
- from langchain_core.callbacks import UsageMetadataCallbackHandler
26
-
27
- llm_1 = init_chat_model(model="openai:gpt-4o-mini")
28
- llm_2 = init_chat_model(model="anthropic:claude-3-5-haiku-latest")
29
-
30
- callback = UsageMetadataCallbackHandler()
31
- result_1 = llm_1.invoke("Hello", config={"callbacks": [callback]})
32
- result_2 = llm_2.invoke("Hello", config={"callbacks": [callback]})
33
- callback.usage_metadata
34
-
35
- .. code-block::
36
-
37
- {'gpt-4o-mini-2024-07-18': {'input_tokens': 8,
38
- 'output_tokens': 10,
39
- 'total_tokens': 18,
40
- 'input_token_details': {'audio': 0, 'cache_read': 0},
41
- 'output_token_details': {'audio': 0, 'reasoning': 0}},
42
- 'claude-3-5-haiku-20241022': {'input_tokens': 8,
43
- 'output_tokens': 21,
44
- 'total_tokens': 29,
45
- 'input_token_details': {'cache_read': 0, 'cache_creation': 0}}}
46
-
47
- .. versionadded:: 0.3.49
22
+ ```python
23
+ from langchain.chat_models import init_chat_model
24
+ from langchain_core.callbacks import UsageMetadataCallbackHandler
25
+
26
+ llm_1 = init_chat_model(model="openai:gpt-4o-mini")
27
+ llm_2 = init_chat_model(model="anthropic:claude-3-5-haiku-latest")
28
+
29
+ callback = UsageMetadataCallbackHandler()
30
+ result_1 = llm_1.invoke("Hello", config={"callbacks": [callback]})
31
+ result_2 = llm_2.invoke("Hello", config={"callbacks": [callback]})
32
+ callback.usage_metadata
33
+ ```
34
+ ```txt
35
+ {'gpt-4o-mini-2024-07-18': {'input_tokens': 8,
36
+ 'output_tokens': 10,
37
+ 'total_tokens': 18,
38
+ 'input_token_details': {'audio': 0, 'cache_read': 0},
39
+ 'output_token_details': {'audio': 0, 'reasoning': 0}},
40
+ 'claude-3-5-haiku-20241022': {'input_tokens': 8,
41
+ 'output_tokens': 21,
42
+ 'total_tokens': 29,
43
+ 'input_token_details': {'cache_read': 0, 'cache_creation': 0}}}
44
+ ```
45
+
46
+ !!! version-added "Added in version 0.3.49"
48
47
 
49
48
  """
50
49
 
@@ -96,45 +95,49 @@ def get_usage_metadata_callback(
96
95
  """Get usage metadata callback.
97
96
 
98
97
  Get context manager for tracking usage metadata across chat model calls using
99
- ``AIMessage.usage_metadata``.
98
+ `AIMessage.usage_metadata`.
100
99
 
101
100
  Args:
102
- name (str): The name of the context variable. Defaults to
103
- ``'usage_metadata_callback'``.
101
+ name: The name of the context variable.
104
102
 
105
103
  Yields:
106
104
  The usage metadata callback.
107
105
 
108
106
  Example:
109
- .. code-block:: python
110
-
111
- from langchain.chat_models import init_chat_model
112
- from langchain_core.callbacks import get_usage_metadata_callback
113
-
114
- llm_1 = init_chat_model(model="openai:gpt-4o-mini")
115
- llm_2 = init_chat_model(model="anthropic:claude-3-5-haiku-latest")
116
-
117
- with get_usage_metadata_callback() as cb:
118
- llm_1.invoke("Hello")
119
- llm_2.invoke("Hello")
120
- print(cb.usage_metadata)
121
-
122
- .. code-block::
123
-
124
- {'gpt-4o-mini-2024-07-18': {'input_tokens': 8,
125
- 'output_tokens': 10,
126
- 'total_tokens': 18,
127
- 'input_token_details': {'audio': 0, 'cache_read': 0},
128
- 'output_token_details': {'audio': 0, 'reasoning': 0}},
129
- 'claude-3-5-haiku-20241022': {'input_tokens': 8,
130
- 'output_tokens': 21,
131
- 'total_tokens': 29,
132
- 'input_token_details': {'cache_read': 0, 'cache_creation': 0}}}
133
-
134
- .. versionadded:: 0.3.49
107
+ ```python
108
+ from langchain.chat_models import init_chat_model
109
+ from langchain_core.callbacks import get_usage_metadata_callback
110
+
111
+ llm_1 = init_chat_model(model="openai:gpt-4o-mini")
112
+ llm_2 = init_chat_model(model="anthropic:claude-3-5-haiku-latest")
113
+
114
+ with get_usage_metadata_callback() as cb:
115
+ llm_1.invoke("Hello")
116
+ llm_2.invoke("Hello")
117
+ print(cb.usage_metadata)
118
+ ```
119
+ ```txt
120
+ {
121
+ "gpt-4o-mini-2024-07-18": {
122
+ "input_tokens": 8,
123
+ "output_tokens": 10,
124
+ "total_tokens": 18,
125
+ "input_token_details": {"audio": 0, "cache_read": 0},
126
+ "output_token_details": {"audio": 0, "reasoning": 0},
127
+ },
128
+ "claude-3-5-haiku-20241022": {
129
+ "input_tokens": 8,
130
+ "output_tokens": 21,
131
+ "total_tokens": 29,
132
+ "input_token_details": {"cache_read": 0, "cache_creation": 0},
133
+ },
134
+ }
135
+ ```
136
+
137
+ !!! version-added "Added in version 0.3.49"
135
138
 
136
139
  """
137
- usage_metadata_callback_var: ContextVar[Optional[UsageMetadataCallbackHandler]] = (
140
+ usage_metadata_callback_var: ContextVar[UsageMetadataCallbackHandler | None] = (
138
141
  ContextVar(name, default=None)
139
142
  )
140
143
  register_configure_hook(usage_metadata_callback_var, inheritable=True)
@@ -1,23 +1,9 @@
1
- """**Chat message history** stores a history of the message interactions in a chat.
2
-
3
- **Class hierarchy:**
4
-
5
- .. code-block::
6
-
7
- BaseChatMessageHistory --> <name>ChatMessageHistory # Examples: FileChatMessageHistory, PostgresChatMessageHistory
8
-
9
- **Main helpers:**
10
-
11
- .. code-block::
12
-
13
- AIMessage, HumanMessage, BaseMessage
14
-
15
- """ # noqa: E501
1
+ """**Chat message history** stores a history of the message interactions in a chat."""
16
2
 
17
3
  from __future__ import annotations
18
4
 
19
5
  from abc import ABC, abstractmethod
20
- from typing import TYPE_CHECKING, Union
6
+ from typing import TYPE_CHECKING
21
7
 
22
8
  from pydantic import BaseModel, Field
23
9
 
@@ -63,46 +49,45 @@ class BaseChatMessageHistory(ABC):
63
49
 
64
50
  Example: Shows a default implementation.
65
51
 
66
- .. code-block:: python
67
-
68
- import json
69
- import os
70
- from langchain_core.messages import messages_from_dict, message_to_dict
71
-
72
-
73
- class FileChatMessageHistory(BaseChatMessageHistory):
74
- storage_path: str
75
- session_id: str
76
-
77
- @property
78
- def messages(self) -> list[BaseMessage]:
79
- try:
80
- with open(
81
- os.path.join(self.storage_path, self.session_id),
82
- "r",
83
- encoding="utf-8",
84
- ) as f:
85
- messages_data = json.load(f)
86
- return messages_from_dict(messages_data)
87
- except FileNotFoundError:
88
- return []
89
-
90
- def add_messages(self, messages: Sequence[BaseMessage]) -> None:
91
- all_messages = list(self.messages) # Existing messages
92
- all_messages.extend(messages) # Add new messages
93
-
94
- serialized = [message_to_dict(message) for message in all_messages]
95
- file_path = os.path.join(self.storage_path, self.session_id)
96
- os.makedirs(os.path.dirname(file_path), exist_ok=True)
97
- with open(file_path, "w", encoding="utf-8") as f:
98
- json.dump(serialized, f)
99
-
100
- def clear(self) -> None:
101
- file_path = os.path.join(self.storage_path, self.session_id)
102
- os.makedirs(os.path.dirname(file_path), exist_ok=True)
103
- with open(file_path, "w", encoding="utf-8") as f:
104
- json.dump([], f)
105
-
52
+ ```python
53
+ import json
54
+ import os
55
+ from langchain_core.messages import messages_from_dict, message_to_dict
56
+
57
+
58
+ class FileChatMessageHistory(BaseChatMessageHistory):
59
+ storage_path: str
60
+ session_id: str
61
+
62
+ @property
63
+ def messages(self) -> list[BaseMessage]:
64
+ try:
65
+ with open(
66
+ os.path.join(self.storage_path, self.session_id),
67
+ "r",
68
+ encoding="utf-8",
69
+ ) as f:
70
+ messages_data = json.load(f)
71
+ return messages_from_dict(messages_data)
72
+ except FileNotFoundError:
73
+ return []
74
+
75
+ def add_messages(self, messages: Sequence[BaseMessage]) -> None:
76
+ all_messages = list(self.messages) # Existing messages
77
+ all_messages.extend(messages) # Add new messages
78
+
79
+ serialized = [message_to_dict(message) for message in all_messages]
80
+ file_path = os.path.join(self.storage_path, self.session_id)
81
+ os.makedirs(os.path.dirname(file_path), exist_ok=True)
82
+ with open(file_path, "w", encoding="utf-8") as f:
83
+ json.dump(serialized, f)
84
+
85
+ def clear(self) -> None:
86
+ file_path = os.path.join(self.storage_path, self.session_id)
87
+ os.makedirs(os.path.dirname(file_path), exist_ok=True)
88
+ with open(file_path, "w", encoding="utf-8") as f:
89
+ json.dump([], f)
90
+ ```
106
91
  """
107
92
 
108
93
  messages: list[BaseMessage]
@@ -126,11 +111,11 @@ class BaseChatMessageHistory(ABC):
126
111
  """
127
112
  return await run_in_executor(None, lambda: self.messages)
128
113
 
129
- def add_user_message(self, message: Union[HumanMessage, str]) -> None:
114
+ def add_user_message(self, message: HumanMessage | str) -> None:
130
115
  """Convenience method for adding a human message string to the store.
131
116
 
132
- .. note::
133
- This is a convenience method. Code should favor the bulk ``add_messages``
117
+ !!! note
118
+ This is a convenience method. Code should favor the bulk `add_messages`
134
119
  interface instead to save on round-trips to the persistence layer.
135
120
 
136
121
  This method may be deprecated in a future release.
@@ -143,11 +128,11 @@ class BaseChatMessageHistory(ABC):
143
128
  else:
144
129
  self.add_message(HumanMessage(content=message))
145
130
 
146
- def add_ai_message(self, message: Union[AIMessage, str]) -> None:
131
+ def add_ai_message(self, message: AIMessage | str) -> None:
147
132
  """Convenience method for adding an AI message string to the store.
148
133
 
149
- .. note::
150
- This is a convenience method. Code should favor the bulk ``add_messages``
134
+ !!! note
135
+ This is a convenience method. Code should favor the bulk `add_messages`
151
136
  interface instead to save on round-trips to the persistence layer.
152
137
 
153
138
  This method may be deprecated in a future release.
@@ -168,7 +153,7 @@ class BaseChatMessageHistory(ABC):
168
153
 
169
154
  Raises:
170
155
  NotImplementedError: If the sub-class has not implemented an efficient
171
- add_messages method.
156
+ `add_messages` method.
172
157
  """
173
158
  if type(self).add_messages != BaseChatMessageHistory.add_messages:
174
159
  # This means that the sub-class has implemented an efficient add_messages