langchain-core 0.4.0.dev0__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 (172) hide show
  1. langchain_core/__init__.py +1 -1
  2. langchain_core/_api/__init__.py +3 -4
  3. langchain_core/_api/beta_decorator.py +45 -70
  4. langchain_core/_api/deprecation.py +80 -80
  5. langchain_core/_api/path.py +22 -8
  6. langchain_core/_import_utils.py +10 -4
  7. langchain_core/agents.py +25 -21
  8. langchain_core/caches.py +53 -63
  9. langchain_core/callbacks/__init__.py +1 -8
  10. langchain_core/callbacks/base.py +341 -348
  11. langchain_core/callbacks/file.py +55 -44
  12. langchain_core/callbacks/manager.py +546 -683
  13. langchain_core/callbacks/stdout.py +29 -30
  14. langchain_core/callbacks/streaming_stdout.py +35 -36
  15. langchain_core/callbacks/usage.py +65 -70
  16. langchain_core/chat_history.py +48 -55
  17. langchain_core/document_loaders/base.py +46 -21
  18. langchain_core/document_loaders/langsmith.py +39 -36
  19. langchain_core/documents/__init__.py +0 -1
  20. langchain_core/documents/base.py +96 -74
  21. langchain_core/documents/compressor.py +12 -9
  22. langchain_core/documents/transformers.py +29 -28
  23. langchain_core/embeddings/fake.py +56 -57
  24. langchain_core/env.py +2 -3
  25. langchain_core/example_selectors/base.py +12 -0
  26. langchain_core/example_selectors/length_based.py +1 -1
  27. langchain_core/example_selectors/semantic_similarity.py +21 -25
  28. langchain_core/exceptions.py +15 -9
  29. langchain_core/globals.py +4 -163
  30. langchain_core/indexing/api.py +132 -125
  31. langchain_core/indexing/base.py +64 -67
  32. langchain_core/indexing/in_memory.py +26 -6
  33. langchain_core/language_models/__init__.py +15 -27
  34. langchain_core/language_models/_utils.py +267 -117
  35. langchain_core/language_models/base.py +92 -177
  36. langchain_core/language_models/chat_models.py +547 -407
  37. langchain_core/language_models/fake.py +11 -11
  38. langchain_core/language_models/fake_chat_models.py +72 -118
  39. langchain_core/language_models/llms.py +168 -242
  40. langchain_core/load/dump.py +8 -11
  41. langchain_core/load/load.py +32 -28
  42. langchain_core/load/mapping.py +2 -4
  43. langchain_core/load/serializable.py +50 -56
  44. langchain_core/messages/__init__.py +36 -51
  45. langchain_core/messages/ai.py +377 -150
  46. langchain_core/messages/base.py +239 -47
  47. langchain_core/messages/block_translators/__init__.py +111 -0
  48. langchain_core/messages/block_translators/anthropic.py +470 -0
  49. langchain_core/messages/block_translators/bedrock.py +94 -0
  50. langchain_core/messages/block_translators/bedrock_converse.py +297 -0
  51. langchain_core/messages/block_translators/google_genai.py +530 -0
  52. langchain_core/messages/block_translators/google_vertexai.py +21 -0
  53. langchain_core/messages/block_translators/groq.py +143 -0
  54. langchain_core/messages/block_translators/langchain_v0.py +301 -0
  55. langchain_core/messages/block_translators/openai.py +1010 -0
  56. langchain_core/messages/chat.py +2 -3
  57. langchain_core/messages/content.py +1423 -0
  58. langchain_core/messages/function.py +7 -7
  59. langchain_core/messages/human.py +44 -38
  60. langchain_core/messages/modifier.py +3 -2
  61. langchain_core/messages/system.py +40 -27
  62. langchain_core/messages/tool.py +160 -58
  63. langchain_core/messages/utils.py +527 -638
  64. langchain_core/output_parsers/__init__.py +1 -14
  65. langchain_core/output_parsers/base.py +68 -104
  66. langchain_core/output_parsers/json.py +13 -17
  67. langchain_core/output_parsers/list.py +11 -33
  68. langchain_core/output_parsers/openai_functions.py +56 -74
  69. langchain_core/output_parsers/openai_tools.py +68 -109
  70. langchain_core/output_parsers/pydantic.py +15 -13
  71. langchain_core/output_parsers/string.py +6 -2
  72. langchain_core/output_parsers/transform.py +17 -60
  73. langchain_core/output_parsers/xml.py +34 -44
  74. langchain_core/outputs/__init__.py +1 -1
  75. langchain_core/outputs/chat_generation.py +26 -11
  76. langchain_core/outputs/chat_result.py +1 -3
  77. langchain_core/outputs/generation.py +17 -6
  78. langchain_core/outputs/llm_result.py +15 -8
  79. langchain_core/prompt_values.py +29 -123
  80. langchain_core/prompts/__init__.py +3 -27
  81. langchain_core/prompts/base.py +48 -63
  82. langchain_core/prompts/chat.py +259 -288
  83. langchain_core/prompts/dict.py +19 -11
  84. langchain_core/prompts/few_shot.py +84 -90
  85. langchain_core/prompts/few_shot_with_templates.py +14 -12
  86. langchain_core/prompts/image.py +19 -14
  87. langchain_core/prompts/loading.py +6 -8
  88. langchain_core/prompts/message.py +7 -8
  89. langchain_core/prompts/prompt.py +42 -43
  90. langchain_core/prompts/string.py +37 -16
  91. langchain_core/prompts/structured.py +43 -46
  92. langchain_core/rate_limiters.py +51 -60
  93. langchain_core/retrievers.py +52 -192
  94. langchain_core/runnables/base.py +1727 -1683
  95. langchain_core/runnables/branch.py +52 -73
  96. langchain_core/runnables/config.py +89 -103
  97. langchain_core/runnables/configurable.py +128 -130
  98. langchain_core/runnables/fallbacks.py +93 -82
  99. langchain_core/runnables/graph.py +127 -127
  100. langchain_core/runnables/graph_ascii.py +63 -41
  101. langchain_core/runnables/graph_mermaid.py +87 -70
  102. langchain_core/runnables/graph_png.py +31 -36
  103. langchain_core/runnables/history.py +145 -161
  104. langchain_core/runnables/passthrough.py +141 -144
  105. langchain_core/runnables/retry.py +84 -68
  106. langchain_core/runnables/router.py +33 -37
  107. langchain_core/runnables/schema.py +79 -72
  108. langchain_core/runnables/utils.py +95 -139
  109. langchain_core/stores.py +85 -131
  110. langchain_core/structured_query.py +11 -15
  111. langchain_core/sys_info.py +31 -32
  112. langchain_core/tools/__init__.py +1 -14
  113. langchain_core/tools/base.py +221 -247
  114. langchain_core/tools/convert.py +144 -161
  115. langchain_core/tools/render.py +10 -10
  116. langchain_core/tools/retriever.py +12 -19
  117. langchain_core/tools/simple.py +52 -29
  118. langchain_core/tools/structured.py +56 -60
  119. langchain_core/tracers/__init__.py +1 -9
  120. langchain_core/tracers/_streaming.py +6 -7
  121. langchain_core/tracers/base.py +103 -112
  122. langchain_core/tracers/context.py +29 -48
  123. langchain_core/tracers/core.py +142 -105
  124. langchain_core/tracers/evaluation.py +30 -34
  125. langchain_core/tracers/event_stream.py +162 -117
  126. langchain_core/tracers/langchain.py +34 -36
  127. langchain_core/tracers/log_stream.py +87 -49
  128. langchain_core/tracers/memory_stream.py +3 -3
  129. langchain_core/tracers/root_listeners.py +18 -34
  130. langchain_core/tracers/run_collector.py +8 -20
  131. langchain_core/tracers/schemas.py +0 -125
  132. langchain_core/tracers/stdout.py +3 -3
  133. langchain_core/utils/__init__.py +1 -4
  134. langchain_core/utils/_merge.py +47 -9
  135. langchain_core/utils/aiter.py +70 -66
  136. langchain_core/utils/env.py +12 -9
  137. langchain_core/utils/function_calling.py +139 -206
  138. langchain_core/utils/html.py +7 -8
  139. langchain_core/utils/input.py +6 -6
  140. langchain_core/utils/interactive_env.py +6 -2
  141. langchain_core/utils/iter.py +48 -45
  142. langchain_core/utils/json.py +14 -4
  143. langchain_core/utils/json_schema.py +159 -43
  144. langchain_core/utils/mustache.py +32 -25
  145. langchain_core/utils/pydantic.py +67 -40
  146. langchain_core/utils/strings.py +5 -5
  147. langchain_core/utils/usage.py +1 -1
  148. langchain_core/utils/utils.py +104 -62
  149. langchain_core/vectorstores/base.py +131 -179
  150. langchain_core/vectorstores/in_memory.py +113 -182
  151. langchain_core/vectorstores/utils.py +23 -17
  152. langchain_core/version.py +1 -1
  153. langchain_core-1.0.0.dist-info/METADATA +68 -0
  154. langchain_core-1.0.0.dist-info/RECORD +172 -0
  155. {langchain_core-0.4.0.dev0.dist-info → langchain_core-1.0.0.dist-info}/WHEEL +1 -1
  156. langchain_core/beta/__init__.py +0 -1
  157. langchain_core/beta/runnables/__init__.py +0 -1
  158. langchain_core/beta/runnables/context.py +0 -448
  159. langchain_core/memory.py +0 -116
  160. langchain_core/messages/content_blocks.py +0 -1435
  161. langchain_core/prompts/pipeline.py +0 -133
  162. langchain_core/pydantic_v1/__init__.py +0 -30
  163. langchain_core/pydantic_v1/dataclasses.py +0 -23
  164. langchain_core/pydantic_v1/main.py +0 -23
  165. langchain_core/tracers/langchain_v1.py +0 -23
  166. langchain_core/utils/loading.py +0 -31
  167. langchain_core/v1/__init__.py +0 -1
  168. langchain_core/v1/chat_models.py +0 -1047
  169. langchain_core/v1/messages.py +0 -755
  170. langchain_core-0.4.0.dev0.dist-info/METADATA +0 -108
  171. langchain_core-0.4.0.dev0.dist-info/RECORD +0 -177
  172. langchain_core-0.4.0.dev0.dist-info/entry_points.txt +0 -4
@@ -1,7 +1,8 @@
1
1
  """Convert functions and runnables to tools."""
2
2
 
3
3
  import inspect
4
- from typing import Any, Callable, Literal, Optional, Union, get_type_hints, overload
4
+ from collections.abc import Callable
5
+ from typing import Any, Literal, get_type_hints, overload
5
6
 
6
7
  from pydantic import BaseModel, Field, create_model
7
8
 
@@ -15,15 +16,14 @@ from langchain_core.tools.structured import StructuredTool
15
16
  @overload
16
17
  def tool(
17
18
  *,
18
- description: Optional[str] = None,
19
+ description: str | None = None,
19
20
  return_direct: bool = False,
20
- args_schema: Optional[ArgsSchema] = None,
21
+ args_schema: ArgsSchema | None = None,
21
22
  infer_schema: bool = True,
22
23
  response_format: Literal["content", "content_and_artifact"] = "content",
23
24
  parse_docstring: bool = False,
24
25
  error_on_invalid_docstring: bool = True,
25
- message_version: Literal["v0", "v1"] = "v0",
26
- ) -> Callable[[Union[Callable, Runnable]], BaseTool]: ...
26
+ ) -> Callable[[Callable | Runnable], BaseTool]: ...
27
27
 
28
28
 
29
29
  @overload
@@ -31,14 +31,13 @@ def tool(
31
31
  name_or_callable: str,
32
32
  runnable: Runnable,
33
33
  *,
34
- description: Optional[str] = None,
34
+ description: str | None = None,
35
35
  return_direct: bool = False,
36
- args_schema: Optional[ArgsSchema] = None,
36
+ args_schema: ArgsSchema | None = None,
37
37
  infer_schema: bool = True,
38
38
  response_format: Literal["content", "content_and_artifact"] = "content",
39
39
  parse_docstring: bool = False,
40
40
  error_on_invalid_docstring: bool = True,
41
- message_version: Literal["v0", "v1"] = "v0",
42
41
  ) -> BaseTool: ...
43
42
 
44
43
 
@@ -46,14 +45,13 @@ def tool(
46
45
  def tool(
47
46
  name_or_callable: Callable,
48
47
  *,
49
- description: Optional[str] = None,
48
+ description: str | None = None,
50
49
  return_direct: bool = False,
51
- args_schema: Optional[ArgsSchema] = None,
50
+ args_schema: ArgsSchema | None = None,
52
51
  infer_schema: bool = True,
53
52
  response_format: Literal["content", "content_and_artifact"] = "content",
54
53
  parse_docstring: bool = False,
55
54
  error_on_invalid_docstring: bool = True,
56
- message_version: Literal["v0", "v1"] = "v0",
57
55
  ) -> BaseTool: ...
58
56
 
59
57
 
@@ -61,34 +59,29 @@ def tool(
61
59
  def tool(
62
60
  name_or_callable: str,
63
61
  *,
64
- description: Optional[str] = None,
62
+ description: str | None = None,
65
63
  return_direct: bool = False,
66
- args_schema: Optional[ArgsSchema] = None,
64
+ args_schema: ArgsSchema | None = None,
67
65
  infer_schema: bool = True,
68
66
  response_format: Literal["content", "content_and_artifact"] = "content",
69
67
  parse_docstring: bool = False,
70
68
  error_on_invalid_docstring: bool = True,
71
- message_version: Literal["v0", "v1"] = "v0",
72
- ) -> Callable[[Union[Callable, Runnable]], BaseTool]: ...
69
+ ) -> Callable[[Callable | Runnable], BaseTool]: ...
73
70
 
74
71
 
75
72
  def tool(
76
- name_or_callable: Optional[Union[str, Callable]] = None,
77
- runnable: Optional[Runnable] = None,
73
+ name_or_callable: str | Callable | None = None,
74
+ runnable: Runnable | None = None,
78
75
  *args: Any,
79
- description: Optional[str] = None,
76
+ description: str | None = None,
80
77
  return_direct: bool = False,
81
- args_schema: Optional[ArgsSchema] = None,
78
+ args_schema: ArgsSchema | None = None,
82
79
  infer_schema: bool = True,
83
80
  response_format: Literal["content", "content_and_artifact"] = "content",
84
81
  parse_docstring: bool = False,
85
82
  error_on_invalid_docstring: bool = True,
86
- message_version: Literal["v0", "v1"] = "v0",
87
- ) -> Union[
88
- BaseTool,
89
- Callable[[Union[Callable, Runnable]], BaseTool],
90
- ]:
91
- """Make tools out of functions, can be used with or without arguments.
83
+ ) -> BaseTool | Callable[[Callable | Runnable], BaseTool]:
84
+ """Make tools out of Python functions, can be used with or without arguments.
92
85
 
93
86
  Args:
94
87
  name_or_callable: Optional name of the tool or the callable to be
@@ -97,140 +90,142 @@ def tool(
97
90
  positional argument.
98
91
  description: Optional description for the tool.
99
92
  Precedence for the tool description value is as follows:
100
- - `description` argument
101
- (used even if docstring and/or `args_schema` are provided)
102
- - tool function docstring
103
- (used even if `args_schema` is provided)
104
- - `args_schema` description
105
- (used only if `description` / docstring are not provided)
93
+
94
+ - `description` argument
95
+ (used even if docstring and/or `args_schema` are provided)
96
+ - Tool function docstring
97
+ (used even if `args_schema` is provided)
98
+ - `args_schema` description
99
+ (used only if `description` / docstring are not provided)
106
100
  *args: Extra positional arguments. Must be empty.
107
101
  return_direct: Whether to return directly from the tool rather
108
- than continuing the agent loop. Defaults to False.
109
- args_schema: optional argument schema for user to specify.
110
- Defaults to None.
102
+ than continuing the agent loop.
103
+ args_schema: Optional argument schema for user to specify.
104
+
111
105
  infer_schema: Whether to infer the schema of the arguments from
112
106
  the function's signature. This also makes the resultant tool
113
107
  accept a dictionary input to its `run()` function.
114
- Defaults to True.
115
- response_format: The tool response format. If "content" then the output of
116
- the tool is interpreted as the contents of a ToolMessage. If
117
- "content_and_artifact" then the output is expected to be a two-tuple
118
- corresponding to the (content, artifact) of a ToolMessage.
119
- Defaults to "content".
120
- parse_docstring: if ``infer_schema`` and ``parse_docstring``, will attempt to
108
+ response_format: The tool response format. If `"content"` then the output of
109
+ the tool is interpreted as the contents of a `ToolMessage`. If
110
+ `"content_and_artifact"` then the output is expected to be a two-tuple
111
+ corresponding to the `(content, artifact)` of a `ToolMessage`.
112
+ parse_docstring: if `infer_schema` and `parse_docstring`, will attempt to
121
113
  parse parameter descriptions from Google Style function docstrings.
122
- Defaults to False.
123
- error_on_invalid_docstring: if ``parse_docstring`` is provided, configure
124
- whether to raise ValueError on invalid Google Style docstrings.
125
- Defaults to True.
126
- message_version: Version of ToolMessage to return given
127
- :class:`~langchain_core.messages.content_blocks.ToolCall` input.
128
-
129
- If ``"v0"``, output will be a v0 :class:`~langchain_core.messages.tool.ToolMessage`.
130
- If ``"v1"``, output will be a v1 :class:`~langchain_core.messages.v1.ToolMessage`.
114
+ error_on_invalid_docstring: if `parse_docstring` is provided, configure
115
+ whether to raise `ValueError` on invalid Google Style docstrings.
116
+
117
+ Raises:
118
+ ValueError: If too many positional arguments are provided.
119
+ ValueError: If a runnable is provided without a string name.
120
+ ValueError: If the first argument is not a string or callable with
121
+ a `__name__` attribute.
122
+ ValueError: If the function does not have a docstring and description
123
+ is not provided and `infer_schema` is `False`.
124
+ ValueError: If `parse_docstring` is `True` and the function has an invalid
125
+ Google-style docstring and `error_on_invalid_docstring` is True.
126
+ ValueError: If a Runnable is provided that does not have an object schema.
131
127
 
132
128
  Returns:
133
129
  The tool.
134
130
 
135
131
  Requires:
136
- - Function must be of type (str) -> str
132
+ - Function must be of type `(str) -> str`
137
133
  - Function must have a docstring
138
134
 
139
135
  Examples:
140
- .. code-block:: python
136
+ ```python
137
+ @tool
138
+ def search_api(query: str) -> str:
139
+ # Searches the API for the query.
140
+ return
141
141
 
142
- @tool
143
- def search_api(query: str) -> str:
144
- # Searches the API for the query.
145
- return
146
142
 
147
- @tool("search", return_direct=True)
148
- def search_api(query: str) -> str:
149
- # Searches the API for the query.
150
- return
143
+ @tool("search", return_direct=True)
144
+ def search_api(query: str) -> str:
145
+ # Searches the API for the query.
146
+ return
151
147
 
152
- @tool(response_format="content_and_artifact")
153
- def search_api(query: str) -> tuple[str, dict]:
154
- return "partial json of results", {"full": "object of results"}
155
148
 
156
- .. versionadded:: 0.2.14
149
+ @tool(response_format="content_and_artifact")
150
+ def search_api(query: str) -> tuple[str, dict]:
151
+ return "partial json of results", {"full": "object of results"}
152
+ ```
153
+
154
+ !!! version-added "Added in version 0.2.14"
157
155
 
158
156
  Parse Google-style docstrings:
159
157
 
160
- .. code-block:: python
161
-
162
- @tool(parse_docstring=True)
163
- def foo(bar: str, baz: int) -> str:
164
- \"\"\"The foo.
165
-
166
- Args:
167
- bar: The bar.
168
- baz: The baz.
169
- \"\"\"
170
- return bar
171
-
172
- foo.args_schema.model_json_schema()
173
-
174
- .. code-block:: python
175
-
176
- {
177
- "title": "foo",
178
- "description": "The foo.",
179
- "type": "object",
180
- "properties": {
181
- "bar": {
182
- "title": "Bar",
183
- "description": "The bar.",
184
- "type": "string"
185
- },
186
- "baz": {
187
- "title": "Baz",
188
- "description": "The baz.",
189
- "type": "integer"
190
- }
158
+ ```python
159
+ @tool(parse_docstring=True)
160
+ def foo(bar: str, baz: int) -> str:
161
+ \"\"\"The foo.
162
+
163
+ Args:
164
+ bar: The bar.
165
+ baz: The baz.
166
+ \"\"\"
167
+ return bar
168
+
169
+ foo.args_schema.model_json_schema()
170
+ ```
171
+
172
+ ```python
173
+ {
174
+ "title": "foo",
175
+ "description": "The foo.",
176
+ "type": "object",
177
+ "properties": {
178
+ "bar": {
179
+ "title": "Bar",
180
+ "description": "The bar.",
181
+ "type": "string",
182
+ },
183
+ "baz": {
184
+ "title": "Baz",
185
+ "description": "The baz.",
186
+ "type": "integer",
191
187
  },
192
- "required": [
193
- "bar",
194
- "baz"
195
- ]
196
- }
188
+ },
189
+ "required": ["bar", "baz"],
190
+ }
191
+ ```
197
192
 
198
- Note that parsing by default will raise ``ValueError`` if the docstring
193
+ Note that parsing by default will raise `ValueError` if the docstring
199
194
  is considered invalid. A docstring is considered invalid if it contains
200
195
  arguments not in the function signature, or is unable to be parsed into
201
- a summary and "Args:" blocks. Examples below:
202
-
203
- .. code-block:: python
204
-
205
- # No args section
206
- def invalid_docstring_1(bar: str, baz: int) -> str:
207
- \"\"\"The foo.\"\"\"
208
- return bar
209
-
210
- # Improper whitespace between summary and args section
211
- def invalid_docstring_2(bar: str, baz: int) -> str:
212
- \"\"\"The foo.
213
- Args:
214
- bar: The bar.
215
- baz: The baz.
216
- \"\"\"
217
- return bar
218
-
219
- # Documented args absent from function signature
220
- def invalid_docstring_3(bar: str, baz: int) -> str:
221
- \"\"\"The foo.
222
-
223
- Args:
224
- banana: The bar.
225
- monkey: The baz.
226
- \"\"\"
227
- return bar
228
-
229
- """ # noqa: D214, D410, D411, E501
196
+ a summary and `"Args:"` blocks. Examples below:
197
+
198
+ ```python
199
+ # No args section
200
+ def invalid_docstring_1(bar: str, baz: int) -> str:
201
+ \"\"\"The foo.\"\"\"
202
+ return bar
203
+
204
+ # Improper whitespace between summary and args section
205
+ def invalid_docstring_2(bar: str, baz: int) -> str:
206
+ \"\"\"The foo.
207
+ Args:
208
+ bar: The bar.
209
+ baz: The baz.
210
+ \"\"\"
211
+ return bar
212
+
213
+ # Documented args absent from function signature
214
+ def invalid_docstring_3(bar: str, baz: int) -> str:
215
+ \"\"\"The foo.
216
+
217
+ Args:
218
+ banana: The bar.
219
+ monkey: The baz.
220
+ \"\"\"
221
+ return bar
222
+
223
+ ```
224
+ """ # noqa: D214, D410, D411 # We're intentionally showing bad formatting in examples
230
225
 
231
226
  def _create_tool_factory(
232
227
  tool_name: str,
233
- ) -> Callable[[Union[Callable, Runnable]], BaseTool]:
228
+ ) -> Callable[[Callable | Runnable], BaseTool]:
234
229
  """Create a decorator that takes a callable and returns a tool.
235
230
 
236
231
  Args:
@@ -240,7 +235,7 @@ def tool(
240
235
  A function that takes a callable or Runnable and returns a tool.
241
236
  """
242
237
 
243
- def _tool_factory(dec_func: Union[Callable, Runnable]) -> BaseTool:
238
+ def _tool_factory(dec_func: Callable | Runnable) -> BaseTool:
244
239
  tool_description = description
245
240
  if isinstance(dec_func, Runnable):
246
241
  runnable = dec_func
@@ -250,18 +245,18 @@ def tool(
250
245
  raise ValueError(msg)
251
246
 
252
247
  async def ainvoke_wrapper(
253
- callbacks: Optional[Callbacks] = None, **kwargs: Any
248
+ callbacks: Callbacks | None = None, **kwargs: Any
254
249
  ) -> Any:
255
250
  return await runnable.ainvoke(kwargs, {"callbacks": callbacks})
256
251
 
257
252
  def invoke_wrapper(
258
- callbacks: Optional[Callbacks] = None, **kwargs: Any
253
+ callbacks: Callbacks | None = None, **kwargs: Any
259
254
  ) -> Any:
260
255
  return runnable.invoke(kwargs, {"callbacks": callbacks})
261
256
 
262
257
  coroutine = ainvoke_wrapper
263
258
  func = invoke_wrapper
264
- schema: Optional[ArgsSchema] = runnable.input_schema
259
+ schema: ArgsSchema | None = runnable.input_schema
265
260
  tool_description = description or repr(runnable)
266
261
  elif inspect.iscoroutinefunction(dec_func):
267
262
  coroutine = dec_func
@@ -284,7 +279,6 @@ def tool(
284
279
  response_format=response_format,
285
280
  parse_docstring=parse_docstring,
286
281
  error_on_invalid_docstring=error_on_invalid_docstring,
287
- message_version=message_version,
288
282
  )
289
283
  # If someone doesn't want a schema applied, we must treat it as
290
284
  # a simple string->string function
@@ -301,7 +295,6 @@ def tool(
301
295
  return_direct=return_direct,
302
296
  coroutine=coroutine,
303
297
  response_format=response_format,
304
- message_version=message_version,
305
298
  )
306
299
 
307
300
  return _tool_factory
@@ -316,7 +309,7 @@ def tool(
316
309
 
317
310
  if runnable is not None:
318
311
  # tool is used as a function
319
- # tool_from_runnable = tool("name", runnable)
312
+ # for instance tool_from_runnable = tool("name", runnable)
320
313
  if not name_or_callable:
321
314
  msg = "Runnable without name for tool constructor"
322
315
  raise ValueError(msg)
@@ -353,7 +346,7 @@ def tool(
353
346
  # @tool(parse_docstring=True)
354
347
  # def my_tool():
355
348
  # pass
356
- def _partial(func: Union[Callable, Runnable]) -> BaseTool:
349
+ def _partial(func: Callable | Runnable) -> BaseTool:
357
350
  """Partial function that takes a callable and returns a tool."""
358
351
  name_ = func.get_name() if isinstance(func, Runnable) else func.__name__
359
352
  tool_factory = _create_tool_factory(name_)
@@ -371,7 +364,7 @@ def _get_description_from_runnable(runnable: Runnable) -> str:
371
364
  def _get_schema_from_runnable_and_arg_types(
372
365
  runnable: Runnable,
373
366
  name: str,
374
- arg_types: Optional[dict[str, type]] = None,
367
+ arg_types: dict[str, type] | None = None,
375
368
  ) -> type[BaseModel]:
376
369
  """Infer args_schema for tool."""
377
370
  if arg_types is None:
@@ -390,30 +383,24 @@ def _get_schema_from_runnable_and_arg_types(
390
383
 
391
384
  def convert_runnable_to_tool(
392
385
  runnable: Runnable,
393
- args_schema: Optional[type[BaseModel]] = None,
386
+ args_schema: type[BaseModel] | None = None,
394
387
  *,
395
- name: Optional[str] = None,
396
- description: Optional[str] = None,
397
- arg_types: Optional[dict[str, type]] = None,
398
- message_version: Literal["v0", "v1"] = "v0",
388
+ name: str | None = None,
389
+ description: str | None = None,
390
+ arg_types: dict[str, type] | None = None,
399
391
  ) -> BaseTool:
400
392
  """Convert a Runnable into a BaseTool.
401
393
 
402
394
  Args:
403
395
  runnable: The runnable to convert.
404
- args_schema: The schema for the tool's input arguments. Defaults to None.
405
- name: The name of the tool. Defaults to None.
406
- description: The description of the tool. Defaults to None.
407
- arg_types: The types of the arguments. Defaults to None.
408
- message_version: Version of ToolMessage to return given
409
- :class:`~langchain_core.messages.content_blocks.ToolCall` input.
410
-
411
- If ``"v0"``, output will be a v0 :class:`~langchain_core.messages.tool.ToolMessage`.
412
- If ``"v1"``, output will be a v1 :class:`~langchain_core.messages.v1.ToolMessage`.
396
+ args_schema: The schema for the tool's input arguments.
397
+ name: The name of the tool.
398
+ description: The description of the tool.
399
+ arg_types: The types of the arguments.
413
400
 
414
401
  Returns:
415
402
  The tool.
416
- """ # noqa: E501
403
+ """
417
404
  if args_schema:
418
405
  runnable = runnable.with_types(input_type=args_schema)
419
406
  description = description or _get_description_from_runnable(runnable)
@@ -426,15 +413,12 @@ def convert_runnable_to_tool(
426
413
  func=runnable.invoke,
427
414
  coroutine=runnable.ainvoke,
428
415
  description=description,
429
- message_version=message_version,
430
416
  )
431
417
 
432
- async def ainvoke_wrapper(
433
- callbacks: Optional[Callbacks] = None, **kwargs: Any
434
- ) -> Any:
418
+ async def ainvoke_wrapper(callbacks: Callbacks | None = None, **kwargs: Any) -> Any:
435
419
  return await runnable.ainvoke(kwargs, config={"callbacks": callbacks})
436
420
 
437
- def invoke_wrapper(callbacks: Optional[Callbacks] = None, **kwargs: Any) -> Any:
421
+ def invoke_wrapper(callbacks: Callbacks | None = None, **kwargs: Any) -> Any:
438
422
  return runnable.invoke(kwargs, config={"callbacks": callbacks})
439
423
 
440
424
  if (
@@ -454,5 +438,4 @@ def convert_runnable_to_tool(
454
438
  coroutine=ainvoke_wrapper,
455
439
  description=description,
456
440
  args_schema=args_schema,
457
- message_version=message_version,
458
441
  )
@@ -2,8 +2,8 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
+ from collections.abc import Callable
5
6
  from inspect import signature
6
- from typing import Callable
7
7
 
8
8
  from langchain_core.tools.base import BaseTool
9
9
 
@@ -21,10 +21,10 @@ def render_text_description(tools: list[BaseTool]) -> str:
21
21
 
22
22
  Output will be in the format of:
23
23
 
24
- .. code-block:: markdown
25
-
26
- search: This tool is used for search
27
- calculator: This tool is used for math
24
+ ```txt
25
+ search: This tool is used for search
26
+ calculator: This tool is used for math
27
+ ```
28
28
  """
29
29
  descriptions = []
30
30
  for tool in tools:
@@ -49,11 +49,11 @@ def render_text_description_and_args(tools: list[BaseTool]) -> str:
49
49
 
50
50
  Output will be in the format of:
51
51
 
52
- .. code-block:: markdown
53
-
54
- search: This tool is used for search, args: {"query": {"type": "string"}}
55
- calculator: This tool is used for math, \
56
- args: {"expression": {"type": "string"}}
52
+ ```txt
53
+ search: This tool is used for search, args: {"query": {"type": "string"}}
54
+ calculator: This tool is used for math, \
55
+ args: {"expression": {"type": "string"}}
56
+ ```
57
57
  """
58
58
  tool_strings = []
59
59
  for tool in tools:
@@ -3,7 +3,7 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  from functools import partial
6
- from typing import TYPE_CHECKING, Literal, Optional, Union
6
+ from typing import TYPE_CHECKING, Literal
7
7
 
8
8
  from pydantic import BaseModel, Field
9
9
 
@@ -34,7 +34,7 @@ def _get_relevant_documents(
34
34
  document_separator: str,
35
35
  callbacks: Callbacks = None,
36
36
  response_format: Literal["content", "content_and_artifact"] = "content",
37
- ) -> Union[str, tuple[str, list[Document]]]:
37
+ ) -> str | tuple[str, list[Document]]:
38
38
  docs = retriever.invoke(query, config={"callbacks": callbacks})
39
39
  content = document_separator.join(
40
40
  format_document(doc, document_prompt) for doc in docs
@@ -52,7 +52,7 @@ async def _aget_relevant_documents(
52
52
  document_separator: str,
53
53
  callbacks: Callbacks = None,
54
54
  response_format: Literal["content", "content_and_artifact"] = "content",
55
- ) -> Union[str, tuple[str, list[Document]]]:
55
+ ) -> str | tuple[str, list[Document]]:
56
56
  docs = await retriever.ainvoke(query, config={"callbacks": callbacks})
57
57
  content = document_separator.join(
58
58
  [await aformat_document(doc, document_prompt) for doc in docs]
@@ -69,10 +69,9 @@ def create_retriever_tool(
69
69
  name: str,
70
70
  description: str,
71
71
  *,
72
- document_prompt: Optional[BasePromptTemplate] = None,
72
+ document_prompt: BasePromptTemplate | None = None,
73
73
  document_separator: str = "\n\n",
74
74
  response_format: Literal["content", "content_and_artifact"] = "content",
75
- message_version: Literal["v0", "v1"] = "v1",
76
75
  ) -> Tool:
77
76
  r"""Create a tool to do retrieval of documents.
78
77
 
@@ -82,22 +81,17 @@ def create_retriever_tool(
82
81
  so should be unique and somewhat descriptive.
83
82
  description: The description for the tool. This will be passed to the language
84
83
  model, so should be descriptive.
85
- document_prompt: The prompt to use for the document. Defaults to None.
86
- document_separator: The separator to use between documents. Defaults to "\n\n".
87
- response_format: The tool response format. If "content" then the output of
88
- the tool is interpreted as the contents of a ToolMessage. If
89
- "content_and_artifact" then the output is expected to be a two-tuple
90
- corresponding to the (content, artifact) of a ToolMessage (artifact
91
- being a list of documents in this case). Defaults to "content".
92
- message_version: Version of ToolMessage to return given
93
- :class:`~langchain_core.messages.content_blocks.ToolCall` input.
94
-
95
- If ``"v0"``, output will be a v0 :class:`~langchain_core.messages.tool.ToolMessage`.
96
- If ``"v1"``, output will be a v1 :class:`~langchain_core.messages.v1.ToolMessage`.
84
+ document_prompt: The prompt to use for the document.
85
+ document_separator: The separator to use between documents.
86
+ response_format: The tool response format. If `"content"` then the output of
87
+ the tool is interpreted as the contents of a `ToolMessage`. If
88
+ `"content_and_artifact"` then the output is expected to be a two-tuple
89
+ corresponding to the `(content, artifact)` of a `ToolMessage` (artifact
90
+ being a list of documents in this case).
97
91
 
98
92
  Returns:
99
93
  Tool class to pass to an agent.
100
- """ # noqa: E501
94
+ """
101
95
  document_prompt = document_prompt or PromptTemplate.from_template("{page_content}")
102
96
  func = partial(
103
97
  _get_relevant_documents,
@@ -120,5 +114,4 @@ def create_retriever_tool(
120
114
  coroutine=afunc,
121
115
  args_schema=RetrieverInput,
122
116
  response_format=response_format,
123
- message_version=message_version,
124
117
  )