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.

Potentially problematic release.


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

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
@@ -42,10 +42,9 @@ def dumps(obj: Any, *, pretty: bool = False, **kwargs: Any) -> str:
42
42
 
43
43
  Args:
44
44
  obj: The object to dump.
45
- pretty: Whether to pretty print the json. If true, the json will be
46
- indented with 2 spaces (if no indent is provided as part of kwargs).
47
- Default is False.
48
- kwargs: Additional arguments to pass to json.dumps
45
+ pretty: Whether to pretty print the json. If `True`, the json will be
46
+ indented with 2 spaces (if no indent is provided as part of `kwargs`).
47
+ **kwargs: Additional arguments to pass to `json.dumps`
49
48
 
50
49
  Returns:
51
50
  A json string representation of the object.
@@ -72,7 +71,7 @@ def dumps(obj: Any, *, pretty: bool = False, **kwargs: Any) -> str:
72
71
  def dumpd(obj: Any) -> Any:
73
72
  """Return a dict representation of an object.
74
73
 
75
- .. note::
74
+ !!! note
76
75
  Unfortunately this function is not as efficient as it could be because it first
77
76
  dumps the object to a json string and then loads it back into a dictionary.
78
77
 
@@ -3,7 +3,7 @@
3
3
  import importlib
4
4
  import json
5
5
  import os
6
- from typing import Any, Optional
6
+ from typing import Any
7
7
 
8
8
  from langchain_core._api import beta
9
9
  from langchain_core.load.mapping import (
@@ -50,12 +50,11 @@ class Reviver:
50
50
 
51
51
  def __init__(
52
52
  self,
53
- secrets_map: Optional[dict[str, str]] = None,
54
- valid_namespaces: Optional[list[str]] = None,
53
+ secrets_map: dict[str, str] | None = None,
54
+ valid_namespaces: list[str] | None = None,
55
55
  secrets_from_env: bool = True, # noqa: FBT001,FBT002
56
- additional_import_mappings: Optional[
57
- dict[tuple[str, ...], tuple[str, ...]]
58
- ] = None,
56
+ additional_import_mappings: dict[tuple[str, ...], tuple[str, ...]]
57
+ | None = None,
59
58
  *,
60
59
  ignore_unserializable_fields: bool = False,
61
60
  ) -> None:
@@ -64,16 +63,13 @@ class Reviver:
64
63
  Args:
65
64
  secrets_map: A map of secrets to load. If a secret is not found in
66
65
  the map, it will be loaded from the environment if `secrets_from_env`
67
- is True. Defaults to None.
66
+ is True.
68
67
  valid_namespaces: A list of additional namespaces (modules)
69
- to allow to be deserialized. Defaults to None.
68
+ to allow to be deserialized.
70
69
  secrets_from_env: Whether to load secrets from the environment.
71
- Defaults to True.
72
70
  additional_import_mappings: A dictionary of additional namespace mappings
73
71
  You can use this to override default mappings or add new mappings.
74
- Defaults to None.
75
72
  ignore_unserializable_fields: Whether to ignore unserializable fields.
76
- Defaults to False.
77
73
  """
78
74
  self.secrets_from_env = secrets_from_env
79
75
  self.secrets_map = secrets_map or {}
@@ -108,7 +104,7 @@ class Reviver:
108
104
  ValueError: If trying to deserialize something that cannot
109
105
  be deserialized in the current version of langchain-core.
110
106
  NotImplementedError: If the object is not implemented and
111
- ``ignore_unserializable_fields`` is False.
107
+ `ignore_unserializable_fields` is False.
112
108
  """
113
109
  if (
114
110
  value.get("lc") == 1
@@ -187,10 +183,10 @@ class Reviver:
187
183
  def loads(
188
184
  text: str,
189
185
  *,
190
- secrets_map: Optional[dict[str, str]] = None,
191
- valid_namespaces: Optional[list[str]] = None,
186
+ secrets_map: dict[str, str] | None = None,
187
+ valid_namespaces: list[str] | None = None,
192
188
  secrets_from_env: bool = True,
193
- additional_import_mappings: Optional[dict[tuple[str, ...], tuple[str, ...]]] = None,
189
+ additional_import_mappings: dict[tuple[str, ...], tuple[str, ...]] | None = None,
194
190
  ignore_unserializable_fields: bool = False,
195
191
  ) -> Any:
196
192
  """Revive a LangChain class from a JSON string.
@@ -201,16 +197,13 @@ def loads(
201
197
  text: The string to load.
202
198
  secrets_map: A map of secrets to load. If a secret is not found in
203
199
  the map, it will be loaded from the environment if `secrets_from_env`
204
- is True. Defaults to None.
200
+ is True.
205
201
  valid_namespaces: A list of additional namespaces (modules)
206
- to allow to be deserialized. Defaults to None.
202
+ to allow to be deserialized.
207
203
  secrets_from_env: Whether to load secrets from the environment.
208
- Defaults to True.
209
204
  additional_import_mappings: A dictionary of additional namespace mappings
210
205
  You can use this to override default mappings or add new mappings.
211
- Defaults to None.
212
206
  ignore_unserializable_fields: Whether to ignore unserializable fields.
213
- Defaults to False.
214
207
 
215
208
  Returns:
216
209
  Revived LangChain objects.
@@ -231,10 +224,10 @@ def loads(
231
224
  def load(
232
225
  obj: Any,
233
226
  *,
234
- secrets_map: Optional[dict[str, str]] = None,
235
- valid_namespaces: Optional[list[str]] = None,
227
+ secrets_map: dict[str, str] | None = None,
228
+ valid_namespaces: list[str] | None = None,
236
229
  secrets_from_env: bool = True,
237
- additional_import_mappings: Optional[dict[tuple[str, ...], tuple[str, ...]]] = None,
230
+ additional_import_mappings: dict[tuple[str, ...], tuple[str, ...]] | None = None,
238
231
  ignore_unserializable_fields: bool = False,
239
232
  ) -> Any:
240
233
  """Revive a LangChain class from a JSON object.
@@ -246,16 +239,13 @@ def load(
246
239
  obj: The object to load.
247
240
  secrets_map: A map of secrets to load. If a secret is not found in
248
241
  the map, it will be loaded from the environment if `secrets_from_env`
249
- is True. Defaults to None.
242
+ is True.
250
243
  valid_namespaces: A list of additional namespaces (modules)
251
- to allow to be deserialized. Defaults to None.
244
+ to allow to be deserialized.
252
245
  secrets_from_env: Whether to load secrets from the environment.
253
- Defaults to True.
254
246
  additional_import_mappings: A dictionary of additional namespace mappings
255
247
  You can use this to override default mappings or add new mappings.
256
- Defaults to None.
257
248
  ignore_unserializable_fields: Whether to ignore unserializable fields.
258
- Defaults to False.
259
249
 
260
250
  Returns:
261
251
  Revived LangChain objects.
@@ -413,11 +413,10 @@ SERIALIZABLE_MAPPING: dict[tuple[str, ...], tuple[str, ...]] = {
413
413
  "few_shot_with_templates",
414
414
  "FewShotPromptWithTemplates",
415
415
  ),
416
- ("langchain", "prompts", "pipeline", "PipelinePromptTemplate"): (
416
+ ("langchain", "prompts", "pipeline"): (
417
417
  "langchain_core",
418
418
  "prompts",
419
419
  "pipeline",
420
- "PipelinePromptTemplate",
421
420
  ),
422
421
  ("langchain", "prompts", "base", "StringPromptTemplate"): (
423
422
  "langchain_core",
@@ -846,11 +845,10 @@ OLD_CORE_NAMESPACES_MAPPING: dict[tuple[str, ...], tuple[str, ...]] = {
846
845
  "few_shot_with_templates",
847
846
  "FewShotPromptWithTemplates",
848
847
  ),
849
- ("langchain_core", "prompts", "pipeline", "PipelinePromptTemplate"): (
848
+ ("langchain_core", "prompts", "pipeline"): (
850
849
  "langchain_core",
851
850
  "prompts",
852
851
  "pipeline",
853
- "PipelinePromptTemplate",
854
852
  ),
855
853
  ("langchain_core", "prompts", "string", "StringPromptTemplate"): (
856
854
  "langchain_core",
@@ -6,9 +6,7 @@ from abc import ABC
6
6
  from typing import (
7
7
  Any,
8
8
  Literal,
9
- Optional,
10
9
  TypedDict,
11
- Union,
12
10
  cast,
13
11
  )
14
12
 
@@ -27,16 +25,16 @@ class BaseSerialized(TypedDict):
27
25
  id: list[str]
28
26
  """The unique identifier of the object."""
29
27
  name: NotRequired[str]
30
- """The name of the object. Optional."""
28
+ """The name of the object."""
31
29
  graph: NotRequired[dict[str, Any]]
32
- """The graph of the object. Optional."""
30
+ """The graph of the object."""
33
31
 
34
32
 
35
33
  class SerializedConstructor(BaseSerialized):
36
34
  """Serialized constructor."""
37
35
 
38
36
  type: Literal["constructor"]
39
- """The type of the object. Must be ``'constructor'``."""
37
+ """The type of the object. Must be `'constructor'`."""
40
38
  kwargs: dict[str, Any]
41
39
  """The constructor arguments."""
42
40
 
@@ -45,16 +43,16 @@ class SerializedSecret(BaseSerialized):
45
43
  """Serialized secret."""
46
44
 
47
45
  type: Literal["secret"]
48
- """The type of the object. Must be ``'secret'``."""
46
+ """The type of the object. Must be `'secret'`."""
49
47
 
50
48
 
51
49
  class SerializedNotImplemented(BaseSerialized):
52
50
  """Serialized not implemented."""
53
51
 
54
52
  type: Literal["not_implemented"]
55
- """The type of the object. Must be ``'not_implemented'``."""
56
- repr: Optional[str]
57
- """The representation of the object. Optional."""
53
+ """The type of the object. Must be `'not_implemented'`."""
54
+ repr: str | None
55
+ """The representation of the object."""
58
56
 
59
57
 
60
58
  def try_neq_default(value: Any, key: str, model: BaseModel) -> bool:
@@ -63,7 +61,7 @@ def try_neq_default(value: Any, key: str, model: BaseModel) -> bool:
63
61
  Args:
64
62
  value: The value.
65
63
  key: The key.
66
- model: The pydantic model.
64
+ model: The Pydantic model.
67
65
 
68
66
  Returns:
69
67
  Whether the value is different from the default.
@@ -94,19 +92,19 @@ class Serializable(BaseModel, ABC):
94
92
 
95
93
  It relies on the following methods and properties:
96
94
 
97
- - ``is_lc_serializable``: Is this class serializable?
98
- By design, even if a class inherits from Serializable, it is not serializable by
99
- default. This is to prevent accidental serialization of objects that should not
100
- be serialized.
101
- - ``get_lc_namespace``: Get the namespace of the langchain object.
102
- During deserialization, this namespace is used to identify
103
- the correct class to instantiate.
104
- Please see the ``Reviver`` class in ``langchain_core.load.load`` for more details.
105
- During deserialization an additional mapping is handle
106
- classes that have moved or been renamed across package versions.
107
- - ``lc_secrets``: A map of constructor argument names to secret ids.
108
- - ``lc_attributes``: List of additional attribute names that should be included
109
- as part of the serialized representation.
95
+ - `is_lc_serializable`: Is this class serializable?
96
+ By design, even if a class inherits from `Serializable`, it is not serializable
97
+ by default. This is to prevent accidental serialization of objects that should
98
+ not be serialized.
99
+ - `get_lc_namespace`: Get the namespace of the LangChain object.
100
+ During deserialization, this namespace is used to identify
101
+ the correct class to instantiate.
102
+ Please see the `Reviver` class in `langchain_core.load.load` for more details.
103
+ During deserialization an additional mapping is handle classes that have moved
104
+ or been renamed across package versions.
105
+ - `lc_secrets`: A map of constructor argument names to secret ids.
106
+ - `lc_attributes`: List of additional attribute names that should be included
107
+ as part of the serialized representation.
110
108
  """
111
109
 
112
110
  # Remove default BaseModel init docstring.
@@ -118,24 +116,24 @@ class Serializable(BaseModel, ABC):
118
116
  def is_lc_serializable(cls) -> bool:
119
117
  """Is this class serializable?
120
118
 
121
- By design, even if a class inherits from Serializable, it is not serializable by
122
- default. This is to prevent accidental serialization of objects that should not
123
- be serialized.
119
+ By design, even if a class inherits from `Serializable`, it is not serializable
120
+ by default. This is to prevent accidental serialization of objects that should
121
+ not be serialized.
124
122
 
125
123
  Returns:
126
- Whether the class is serializable. Default is False.
124
+ Whether the class is serializable. Default is `False`.
127
125
  """
128
126
  return False
129
127
 
130
128
  @classmethod
131
129
  def get_lc_namespace(cls) -> list[str]:
132
- """Get the namespace of the langchain object.
130
+ """Get the namespace of the LangChain object.
133
131
 
134
132
  For example, if the class is `langchain.llms.openai.OpenAI`, then the
135
- namespace is ["langchain", "llms", "openai"]
133
+ namespace is `["langchain", "llms", "openai"]`
136
134
 
137
135
  Returns:
138
- The namespace as a list of strings.
136
+ The namespace.
139
137
  """
140
138
  return cls.__module__.split(".")
141
139
 
@@ -143,8 +141,7 @@ class Serializable(BaseModel, ABC):
143
141
  def lc_secrets(self) -> dict[str, str]:
144
142
  """A map of constructor argument names to secret ids.
145
143
 
146
- For example,
147
- {"openai_api_key": "OPENAI_API_KEY"}
144
+ For example, `{"openai_api_key": "OPENAI_API_KEY"}`
148
145
  """
149
146
  return {}
150
147
 
@@ -153,6 +150,7 @@ class Serializable(BaseModel, ABC):
153
150
  """List of attribute names that should be included in the serialized kwargs.
154
151
 
155
152
  These attributes must be accepted by the constructor.
153
+
156
154
  Default is an empty dictionary.
157
155
  """
158
156
  return {}
@@ -163,8 +161,9 @@ class Serializable(BaseModel, ABC):
163
161
 
164
162
  The unique identifier is a list of strings that describes the path
165
163
  to the object.
164
+
166
165
  For example, for the class `langchain.llms.openai.OpenAI`, the id is
167
- ["langchain", "llms", "openai", "OpenAI"].
166
+ `["langchain", "llms", "openai", "OpenAI"]`.
168
167
  """
169
168
  # Pydantic generics change the class name. So we need to do the following
170
169
  if (
@@ -188,14 +187,14 @@ class Serializable(BaseModel, ABC):
188
187
  if (k not in type(self).model_fields or try_neq_default(v, k, self))
189
188
  ]
190
189
 
191
- def to_json(self) -> Union[SerializedConstructor, SerializedNotImplemented]:
190
+ def to_json(self) -> SerializedConstructor | SerializedNotImplemented:
192
191
  """Serialize the object to JSON.
193
192
 
194
193
  Raises:
195
194
  ValueError: If the class has deprecated attributes.
196
195
 
197
196
  Returns:
198
- A json serializable object or a SerializedNotImplemented object.
197
+ A json serializable object or a `SerializedNotImplemented` object.
199
198
  """
200
199
  if not self.is_lc_serializable():
201
200
  return self.to_json_not_implemented()
@@ -270,7 +269,7 @@ class Serializable(BaseModel, ABC):
270
269
  """Serialize a "not implemented" object.
271
270
 
272
271
  Returns:
273
- SerializedNotImplemented.
272
+ `SerializedNotImplemented`.
274
273
  """
275
274
  return to_json_not_implemented(self)
276
275
 
@@ -285,8 +284,8 @@ def _is_field_useful(inst: Serializable, key: str, value: Any) -> bool:
285
284
 
286
285
  Returns:
287
286
  Whether the field is useful. If the field is required, it is useful.
288
- If the field is not required, it is useful if the value is not None.
289
- If the field is not required and the value is None, it is useful if the
287
+ If the field is not required, it is useful if the value is not `None`.
288
+ If the field is not required and the value is `None`, it is useful if the
290
289
  default value is different from the value.
291
290
  """
292
291
  field = type(inst).model_fields.get(key)
@@ -345,10 +344,10 @@ def to_json_not_implemented(obj: object) -> SerializedNotImplemented:
345
344
  """Serialize a "not implemented" object.
346
345
 
347
346
  Args:
348
- obj: object to serialize.
347
+ obj: Object to serialize.
349
348
 
350
349
  Returns:
351
- SerializedNotImplemented
350
+ `SerializedNotImplemented`
352
351
  """
353
352
  id_: list[str] = []
354
353
  try:
@@ -1,23 +1,9 @@
1
- """**Messages** are objects used in prompts and chat conversations.
2
-
3
- **Class hierarchy:**
4
-
5
- .. code-block::
6
-
7
- BaseMessage --> SystemMessage, AIMessage, HumanMessage, ChatMessage, FunctionMessage, ToolMessage
8
- --> BaseMessageChunk --> SystemMessageChunk, AIMessageChunk, HumanMessageChunk, ChatMessageChunk, FunctionMessageChunk, ToolMessageChunk
9
-
10
- **Main helpers:**
11
-
12
- .. code-block::
13
-
14
- ChatPromptTemplate
15
-
16
- """ # noqa: E501
1
+ """**Messages** are objects used in prompts and chat conversations."""
17
2
 
18
3
  from typing import TYPE_CHECKING
19
4
 
20
5
  from langchain_core._import_utils import import_attr
6
+ from langchain_core.utils.utils import LC_AUTO_PREFIX, LC_ID_PREFIX, ensure_id
21
7
 
22
8
  if TYPE_CHECKING:
23
9
  from langchain_core.messages.ai import (
@@ -31,10 +17,29 @@ if TYPE_CHECKING:
31
17
  message_to_dict,
32
18
  messages_to_dict,
33
19
  )
34
- from langchain_core.messages.chat import ChatMessage, ChatMessageChunk
35
- from langchain_core.messages.content_blocks import (
20
+ from langchain_core.messages.block_translators.openai import (
36
21
  convert_to_openai_data_block,
37
22
  convert_to_openai_image_block,
23
+ )
24
+ from langchain_core.messages.chat import ChatMessage, ChatMessageChunk
25
+ from langchain_core.messages.content import (
26
+ Annotation,
27
+ AudioContentBlock,
28
+ Citation,
29
+ ContentBlock,
30
+ DataContentBlock,
31
+ FileContentBlock,
32
+ ImageContentBlock,
33
+ InvalidToolCall,
34
+ NonStandardAnnotation,
35
+ NonStandardContentBlock,
36
+ PlainTextContentBlock,
37
+ ReasoningContentBlock,
38
+ ServerToolCall,
39
+ ServerToolCallChunk,
40
+ ServerToolResult,
41
+ TextContentBlock,
42
+ VideoContentBlock,
38
43
  is_data_content_block,
39
44
  )
40
45
  from langchain_core.messages.function import FunctionMessage, FunctionMessageChunk
@@ -42,7 +47,6 @@ if TYPE_CHECKING:
42
47
  from langchain_core.messages.modifier import RemoveMessage
43
48
  from langchain_core.messages.system import SystemMessage, SystemMessageChunk
44
49
  from langchain_core.messages.tool import (
45
- InvalidToolCall,
46
50
  ToolCall,
47
51
  ToolCallChunk,
48
52
  ToolMessage,
@@ -63,31 +67,50 @@ if TYPE_CHECKING:
63
67
  )
64
68
 
65
69
  __all__ = (
70
+ "LC_AUTO_PREFIX",
71
+ "LC_ID_PREFIX",
66
72
  "AIMessage",
67
73
  "AIMessageChunk",
74
+ "Annotation",
68
75
  "AnyMessage",
76
+ "AudioContentBlock",
69
77
  "BaseMessage",
70
78
  "BaseMessageChunk",
71
79
  "ChatMessage",
72
80
  "ChatMessageChunk",
81
+ "Citation",
82
+ "ContentBlock",
83
+ "DataContentBlock",
84
+ "FileContentBlock",
73
85
  "FunctionMessage",
74
86
  "FunctionMessageChunk",
75
87
  "HumanMessage",
76
88
  "HumanMessageChunk",
89
+ "ImageContentBlock",
77
90
  "InvalidToolCall",
78
91
  "MessageLikeRepresentation",
92
+ "NonStandardAnnotation",
93
+ "NonStandardContentBlock",
94
+ "PlainTextContentBlock",
95
+ "ReasoningContentBlock",
79
96
  "RemoveMessage",
97
+ "ServerToolCall",
98
+ "ServerToolCallChunk",
99
+ "ServerToolResult",
80
100
  "SystemMessage",
81
101
  "SystemMessageChunk",
102
+ "TextContentBlock",
82
103
  "ToolCall",
83
104
  "ToolCallChunk",
84
105
  "ToolMessage",
85
106
  "ToolMessageChunk",
107
+ "VideoContentBlock",
86
108
  "_message_from_dict",
87
109
  "convert_to_messages",
88
110
  "convert_to_openai_data_block",
89
111
  "convert_to_openai_image_block",
90
112
  "convert_to_openai_messages",
113
+ "ensure_id",
91
114
  "filter_messages",
92
115
  "get_buffer_string",
93
116
  "is_data_content_block",
@@ -103,35 +126,51 @@ __all__ = (
103
126
  _dynamic_imports = {
104
127
  "AIMessage": "ai",
105
128
  "AIMessageChunk": "ai",
129
+ "Annotation": "content",
130
+ "AudioContentBlock": "content",
106
131
  "BaseMessage": "base",
107
132
  "BaseMessageChunk": "base",
108
133
  "merge_content": "base",
109
134
  "message_to_dict": "base",
110
135
  "messages_to_dict": "base",
136
+ "Citation": "content",
137
+ "ContentBlock": "content",
111
138
  "ChatMessage": "chat",
112
139
  "ChatMessageChunk": "chat",
140
+ "DataContentBlock": "content",
141
+ "FileContentBlock": "content",
113
142
  "FunctionMessage": "function",
114
143
  "FunctionMessageChunk": "function",
115
144
  "HumanMessage": "human",
116
145
  "HumanMessageChunk": "human",
146
+ "NonStandardAnnotation": "content",
147
+ "NonStandardContentBlock": "content",
148
+ "PlainTextContentBlock": "content",
149
+ "ReasoningContentBlock": "content",
117
150
  "RemoveMessage": "modifier",
151
+ "ServerToolCall": "content",
152
+ "ServerToolCallChunk": "content",
153
+ "ServerToolResult": "content",
118
154
  "SystemMessage": "system",
119
155
  "SystemMessageChunk": "system",
156
+ "ImageContentBlock": "content",
120
157
  "InvalidToolCall": "tool",
158
+ "TextContentBlock": "content",
121
159
  "ToolCall": "tool",
122
160
  "ToolCallChunk": "tool",
123
161
  "ToolMessage": "tool",
124
162
  "ToolMessageChunk": "tool",
163
+ "VideoContentBlock": "content",
125
164
  "AnyMessage": "utils",
126
165
  "MessageLikeRepresentation": "utils",
127
166
  "_message_from_dict": "utils",
128
167
  "convert_to_messages": "utils",
129
- "convert_to_openai_data_block": "content_blocks",
130
- "convert_to_openai_image_block": "content_blocks",
168
+ "convert_to_openai_data_block": "block_translators.openai",
169
+ "convert_to_openai_image_block": "block_translators.openai",
131
170
  "convert_to_openai_messages": "utils",
132
171
  "filter_messages": "utils",
133
172
  "get_buffer_string": "utils",
134
- "is_data_content_block": "content_blocks",
173
+ "is_data_content_block": "content",
135
174
  "merge_message_runs": "utils",
136
175
  "message_chunk_to_message": "utils",
137
176
  "messages_from_dict": "utils",