langchain 0.3.22__py3-none-any.whl → 0.3.24__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 (170) hide show
  1. langchain/_api/module_import.py +3 -3
  2. langchain/agents/agent.py +104 -109
  3. langchain/agents/agent_iterator.py +11 -15
  4. langchain/agents/agent_toolkits/conversational_retrieval/openai_functions.py +2 -2
  5. langchain/agents/agent_toolkits/vectorstore/base.py +3 -3
  6. langchain/agents/agent_toolkits/vectorstore/toolkit.py +4 -6
  7. langchain/agents/chat/base.py +7 -6
  8. langchain/agents/chat/output_parser.py +2 -1
  9. langchain/agents/conversational/base.py +5 -4
  10. langchain/agents/conversational_chat/base.py +9 -8
  11. langchain/agents/format_scratchpad/log.py +1 -3
  12. langchain/agents/format_scratchpad/log_to_messages.py +3 -5
  13. langchain/agents/format_scratchpad/openai_functions.py +4 -4
  14. langchain/agents/format_scratchpad/tools.py +3 -3
  15. langchain/agents/format_scratchpad/xml.py +1 -3
  16. langchain/agents/initialize.py +2 -1
  17. langchain/agents/json_chat/base.py +3 -2
  18. langchain/agents/loading.py +5 -5
  19. langchain/agents/mrkl/base.py +6 -5
  20. langchain/agents/openai_assistant/base.py +13 -17
  21. langchain/agents/openai_functions_agent/agent_token_buffer_memory.py +6 -6
  22. langchain/agents/openai_functions_agent/base.py +13 -12
  23. langchain/agents/openai_functions_multi_agent/base.py +15 -14
  24. langchain/agents/openai_tools/base.py +2 -1
  25. langchain/agents/output_parsers/openai_functions.py +2 -2
  26. langchain/agents/output_parsers/openai_tools.py +6 -6
  27. langchain/agents/output_parsers/react_json_single_input.py +2 -1
  28. langchain/agents/output_parsers/self_ask.py +2 -1
  29. langchain/agents/output_parsers/tools.py +7 -7
  30. langchain/agents/react/agent.py +3 -2
  31. langchain/agents/react/base.py +4 -3
  32. langchain/agents/schema.py +3 -3
  33. langchain/agents/self_ask_with_search/base.py +2 -1
  34. langchain/agents/structured_chat/base.py +9 -8
  35. langchain/agents/structured_chat/output_parser.py +2 -1
  36. langchain/agents/tool_calling_agent/base.py +3 -2
  37. langchain/agents/tools.py +4 -4
  38. langchain/agents/types.py +3 -3
  39. langchain/agents/utils.py +1 -1
  40. langchain/agents/xml/base.py +7 -6
  41. langchain/callbacks/streaming_aiter.py +3 -2
  42. langchain/callbacks/streaming_aiter_final_only.py +3 -3
  43. langchain/callbacks/streaming_stdout_final_only.py +3 -3
  44. langchain/chains/api/base.py +11 -12
  45. langchain/chains/base.py +47 -50
  46. langchain/chains/combine_documents/base.py +23 -23
  47. langchain/chains/combine_documents/map_reduce.py +12 -12
  48. langchain/chains/combine_documents/map_rerank.py +16 -15
  49. langchain/chains/combine_documents/reduce.py +17 -17
  50. langchain/chains/combine_documents/refine.py +12 -12
  51. langchain/chains/combine_documents/stuff.py +10 -10
  52. langchain/chains/constitutional_ai/base.py +9 -9
  53. langchain/chains/conversation/base.py +2 -4
  54. langchain/chains/conversational_retrieval/base.py +30 -30
  55. langchain/chains/elasticsearch_database/base.py +13 -13
  56. langchain/chains/example_generator.py +1 -3
  57. langchain/chains/flare/base.py +13 -12
  58. langchain/chains/flare/prompts.py +2 -4
  59. langchain/chains/hyde/base.py +8 -8
  60. langchain/chains/llm.py +31 -30
  61. langchain/chains/llm_checker/base.py +6 -6
  62. langchain/chains/llm_math/base.py +10 -10
  63. langchain/chains/llm_summarization_checker/base.py +6 -6
  64. langchain/chains/loading.py +12 -14
  65. langchain/chains/mapreduce.py +7 -6
  66. langchain/chains/moderation.py +8 -8
  67. langchain/chains/natbot/base.py +6 -6
  68. langchain/chains/openai_functions/base.py +8 -10
  69. langchain/chains/openai_functions/citation_fuzzy_match.py +4 -4
  70. langchain/chains/openai_functions/extraction.py +3 -3
  71. langchain/chains/openai_functions/openapi.py +12 -12
  72. langchain/chains/openai_functions/qa_with_structure.py +4 -4
  73. langchain/chains/openai_functions/utils.py +2 -2
  74. langchain/chains/openai_tools/extraction.py +2 -2
  75. langchain/chains/prompt_selector.py +3 -3
  76. langchain/chains/qa_generation/base.py +5 -5
  77. langchain/chains/qa_with_sources/base.py +21 -21
  78. langchain/chains/qa_with_sources/loading.py +2 -1
  79. langchain/chains/qa_with_sources/retrieval.py +6 -6
  80. langchain/chains/qa_with_sources/vector_db.py +8 -8
  81. langchain/chains/query_constructor/base.py +4 -3
  82. langchain/chains/query_constructor/parser.py +5 -4
  83. langchain/chains/question_answering/chain.py +3 -2
  84. langchain/chains/retrieval.py +2 -2
  85. langchain/chains/retrieval_qa/base.py +16 -16
  86. langchain/chains/router/base.py +12 -11
  87. langchain/chains/router/embedding_router.py +12 -11
  88. langchain/chains/router/llm_router.py +12 -12
  89. langchain/chains/router/multi_prompt.py +3 -3
  90. langchain/chains/router/multi_retrieval_qa.py +5 -4
  91. langchain/chains/sequential.py +18 -18
  92. langchain/chains/sql_database/query.py +4 -4
  93. langchain/chains/structured_output/base.py +14 -13
  94. langchain/chains/summarize/chain.py +4 -3
  95. langchain/chains/transform.py +12 -11
  96. langchain/chat_models/base.py +34 -31
  97. langchain/embeddings/__init__.py +1 -1
  98. langchain/embeddings/base.py +4 -4
  99. langchain/embeddings/cache.py +19 -18
  100. langchain/evaluation/agents/trajectory_eval_chain.py +16 -19
  101. langchain/evaluation/comparison/eval_chain.py +10 -10
  102. langchain/evaluation/criteria/eval_chain.py +11 -10
  103. langchain/evaluation/embedding_distance/base.py +21 -21
  104. langchain/evaluation/exact_match/base.py +3 -3
  105. langchain/evaluation/loading.py +7 -8
  106. langchain/evaluation/qa/eval_chain.py +7 -6
  107. langchain/evaluation/regex_match/base.py +3 -3
  108. langchain/evaluation/schema.py +6 -5
  109. langchain/evaluation/scoring/eval_chain.py +9 -9
  110. langchain/evaluation/string_distance/base.py +23 -23
  111. langchain/hub.py +2 -1
  112. langchain/indexes/_sql_record_manager.py +8 -7
  113. langchain/indexes/vectorstore.py +11 -11
  114. langchain/llms/__init__.py +3 -3
  115. langchain/memory/buffer.py +13 -13
  116. langchain/memory/buffer_window.py +5 -5
  117. langchain/memory/chat_memory.py +5 -5
  118. langchain/memory/combined.py +10 -10
  119. langchain/memory/entity.py +8 -7
  120. langchain/memory/readonly.py +4 -4
  121. langchain/memory/simple.py +5 -5
  122. langchain/memory/summary.py +8 -8
  123. langchain/memory/summary_buffer.py +11 -11
  124. langchain/memory/token_buffer.py +5 -5
  125. langchain/memory/utils.py +2 -2
  126. langchain/memory/vectorstore.py +15 -14
  127. langchain/memory/vectorstore_token_buffer_memory.py +7 -7
  128. langchain/model_laboratory.py +4 -3
  129. langchain/output_parsers/combining.py +5 -5
  130. langchain/output_parsers/datetime.py +1 -2
  131. langchain/output_parsers/enum.py +4 -5
  132. langchain/output_parsers/pandas_dataframe.py +5 -5
  133. langchain/output_parsers/regex.py +4 -4
  134. langchain/output_parsers/regex_dict.py +4 -4
  135. langchain/output_parsers/retry.py +2 -2
  136. langchain/output_parsers/structured.py +5 -5
  137. langchain/output_parsers/yaml.py +3 -3
  138. langchain/pydantic_v1/__init__.py +1 -6
  139. langchain/pydantic_v1/dataclasses.py +1 -5
  140. langchain/pydantic_v1/main.py +1 -5
  141. langchain/retrievers/contextual_compression.py +3 -3
  142. langchain/retrievers/document_compressors/base.py +3 -2
  143. langchain/retrievers/document_compressors/chain_extract.py +4 -3
  144. langchain/retrievers/document_compressors/chain_filter.py +3 -2
  145. langchain/retrievers/document_compressors/cohere_rerank.py +4 -3
  146. langchain/retrievers/document_compressors/cross_encoder.py +1 -2
  147. langchain/retrievers/document_compressors/cross_encoder_rerank.py +2 -1
  148. langchain/retrievers/document_compressors/embeddings_filter.py +3 -2
  149. langchain/retrievers/document_compressors/listwise_rerank.py +6 -5
  150. langchain/retrievers/ensemble.py +15 -19
  151. langchain/retrievers/merger_retriever.py +7 -12
  152. langchain/retrievers/multi_query.py +14 -13
  153. langchain/retrievers/multi_vector.py +4 -4
  154. langchain/retrievers/parent_document_retriever.py +9 -8
  155. langchain/retrievers/re_phraser.py +2 -3
  156. langchain/retrievers/self_query/base.py +13 -12
  157. langchain/retrievers/time_weighted_retriever.py +14 -14
  158. langchain/runnables/openai_functions.py +4 -3
  159. langchain/smith/evaluation/config.py +7 -6
  160. langchain/smith/evaluation/progress.py +3 -2
  161. langchain/smith/evaluation/runner_utils.py +58 -61
  162. langchain/smith/evaluation/string_run_evaluator.py +29 -29
  163. langchain/storage/encoder_backed.py +7 -11
  164. langchain/storage/file_system.py +5 -4
  165. {langchain-0.3.22.dist-info → langchain-0.3.24.dist-info}/METADATA +5 -3
  166. {langchain-0.3.22.dist-info → langchain-0.3.24.dist-info}/RECORD +169 -169
  167. {langchain-0.3.22.dist-info → langchain-0.3.24.dist-info}/WHEEL +1 -1
  168. langchain-0.3.24.dist-info/entry_points.txt +4 -0
  169. langchain-0.3.22.dist-info/entry_points.txt +0 -5
  170. {langchain-0.3.22.dist-info → langchain-0.3.24.dist-info}/licenses/LICENSE +0 -0
@@ -1,6 +1,6 @@
1
1
  """Memory used to save agent output AND intermediate steps."""
2
2
 
3
- from typing import Any, Dict, List
3
+ from typing import Any
4
4
 
5
5
  from langchain_core.language_models import BaseLanguageModel
6
6
  from langchain_core.messages import BaseMessage, get_buffer_string
@@ -12,7 +12,7 @@ from langchain.agents.format_scratchpad import (
12
12
  from langchain.memory.chat_memory import BaseChatMemory
13
13
 
14
14
 
15
- class AgentTokenBufferMemory(BaseChatMemory): # type: ignore[override]
15
+ class AgentTokenBufferMemory(BaseChatMemory):
16
16
  """Memory used to save agent output AND intermediate steps.
17
17
 
18
18
  Parameters:
@@ -43,19 +43,19 @@ class AgentTokenBufferMemory(BaseChatMemory): # type: ignore[override]
43
43
  format_as_tools: bool = False
44
44
 
45
45
  @property
46
- def buffer(self) -> List[BaseMessage]:
46
+ def buffer(self) -> list[BaseMessage]:
47
47
  """String buffer of memory."""
48
48
  return self.chat_memory.messages
49
49
 
50
50
  @property
51
- def memory_variables(self) -> List[str]:
51
+ def memory_variables(self) -> list[str]:
52
52
  """Always return list of memory variables.
53
53
 
54
54
  :meta private:
55
55
  """
56
56
  return [self.memory_key]
57
57
 
58
- def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, Any]:
58
+ def load_memory_variables(self, inputs: dict[str, Any]) -> dict[str, Any]:
59
59
  """Return history buffer.
60
60
 
61
61
  Args:
@@ -74,7 +74,7 @@ class AgentTokenBufferMemory(BaseChatMemory): # type: ignore[override]
74
74
  )
75
75
  return {self.memory_key: final_buffer}
76
76
 
77
- def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, Any]) -> None:
77
+ def save_context(self, inputs: dict[str, Any], outputs: dict[str, Any]) -> None:
78
78
  """Save context from this conversation to buffer. Pruned.
79
79
 
80
80
  Args:
@@ -1,6 +1,7 @@
1
1
  """Module implements an agent that uses OpenAI's APIs function enabled API."""
2
2
 
3
- from typing import Any, List, Optional, Sequence, Tuple, Type, Union
3
+ from collections.abc import Sequence
4
+ from typing import Any, Optional, Union
4
5
 
5
6
  from langchain_core._api import deprecated
6
7
  from langchain_core.agents import AgentAction, AgentFinish
@@ -51,11 +52,11 @@ class OpenAIFunctionsAgent(BaseSingleActionAgent):
51
52
  llm: BaseLanguageModel
52
53
  tools: Sequence[BaseTool]
53
54
  prompt: BasePromptTemplate
54
- output_parser: Type[OpenAIFunctionsAgentOutputParser] = (
55
+ output_parser: type[OpenAIFunctionsAgentOutputParser] = (
55
56
  OpenAIFunctionsAgentOutputParser
56
57
  )
57
58
 
58
- def get_allowed_tools(self) -> List[str]:
59
+ def get_allowed_tools(self) -> list[str]:
59
60
  """Get allowed tools."""
60
61
  return [t.name for t in self.tools]
61
62
 
@@ -81,19 +82,19 @@ class OpenAIFunctionsAgent(BaseSingleActionAgent):
81
82
  return self
82
83
 
83
84
  @property
84
- def input_keys(self) -> List[str]:
85
+ def input_keys(self) -> list[str]:
85
86
  """Get input keys. Input refers to user input here."""
86
87
  return ["input"]
87
88
 
88
89
  @property
89
- def functions(self) -> List[dict]:
90
+ def functions(self) -> list[dict]:
90
91
  """Get functions."""
91
92
 
92
93
  return [dict(convert_to_openai_function(t)) for t in self.tools]
93
94
 
94
95
  def plan(
95
96
  self,
96
- intermediate_steps: List[Tuple[AgentAction, str]],
97
+ intermediate_steps: list[tuple[AgentAction, str]],
97
98
  callbacks: Callbacks = None,
98
99
  with_functions: bool = True,
99
100
  **kwargs: Any,
@@ -135,7 +136,7 @@ class OpenAIFunctionsAgent(BaseSingleActionAgent):
135
136
 
136
137
  async def aplan(
137
138
  self,
138
- intermediate_steps: List[Tuple[AgentAction, str]],
139
+ intermediate_steps: list[tuple[AgentAction, str]],
139
140
  callbacks: Callbacks = None,
140
141
  **kwargs: Any,
141
142
  ) -> Union[AgentAction, AgentFinish]:
@@ -168,7 +169,7 @@ class OpenAIFunctionsAgent(BaseSingleActionAgent):
168
169
  def return_stopped_response(
169
170
  self,
170
171
  early_stopping_method: str,
171
- intermediate_steps: List[Tuple[AgentAction, str]],
172
+ intermediate_steps: list[tuple[AgentAction, str]],
172
173
  **kwargs: Any,
173
174
  ) -> AgentFinish:
174
175
  """Return response when agent has been stopped due to max iterations.
@@ -213,7 +214,7 @@ class OpenAIFunctionsAgent(BaseSingleActionAgent):
213
214
  system_message: Optional[SystemMessage] = SystemMessage(
214
215
  content="You are a helpful AI assistant."
215
216
  ),
216
- extra_prompt_messages: Optional[List[BaseMessagePromptTemplate]] = None,
217
+ extra_prompt_messages: Optional[list[BaseMessagePromptTemplate]] = None,
217
218
  ) -> ChatPromptTemplate:
218
219
  """Create prompt for this agent.
219
220
 
@@ -227,7 +228,7 @@ class OpenAIFunctionsAgent(BaseSingleActionAgent):
227
228
  A prompt template to pass into this agent.
228
229
  """
229
230
  _prompts = extra_prompt_messages or []
230
- messages: List[Union[BaseMessagePromptTemplate, BaseMessage]]
231
+ messages: list[Union[BaseMessagePromptTemplate, BaseMessage]]
231
232
  if system_message:
232
233
  messages = [system_message]
233
234
  else:
@@ -240,7 +241,7 @@ class OpenAIFunctionsAgent(BaseSingleActionAgent):
240
241
  MessagesPlaceholder(variable_name="agent_scratchpad"),
241
242
  ]
242
243
  )
243
- return ChatPromptTemplate(messages=messages) # type: ignore[arg-type, call-arg]
244
+ return ChatPromptTemplate(messages=messages)
244
245
 
245
246
  @classmethod
246
247
  def from_llm_and_tools(
@@ -248,7 +249,7 @@ class OpenAIFunctionsAgent(BaseSingleActionAgent):
248
249
  llm: BaseLanguageModel,
249
250
  tools: Sequence[BaseTool],
250
251
  callback_manager: Optional[BaseCallbackManager] = None,
251
- extra_prompt_messages: Optional[List[BaseMessagePromptTemplate]] = None,
252
+ extra_prompt_messages: Optional[list[BaseMessagePromptTemplate]] = None,
252
253
  system_message: Optional[SystemMessage] = SystemMessage(
253
254
  content="You are a helpful AI assistant."
254
255
  ),
@@ -1,8 +1,9 @@
1
1
  """Module implements an agent that uses OpenAI's APIs function enabled API."""
2
2
 
3
3
  import json
4
+ from collections.abc import Sequence
4
5
  from json import JSONDecodeError
5
- from typing import Any, List, Optional, Sequence, Tuple, Union
6
+ from typing import Any, Optional, Union
6
7
 
7
8
  from langchain_core._api import deprecated
8
9
  from langchain_core.agents import AgentAction, AgentActionMessageLog, AgentFinish
@@ -34,7 +35,7 @@ from langchain.agents.format_scratchpad.openai_functions import (
34
35
  _FunctionsAgentAction = AgentActionMessageLog
35
36
 
36
37
 
37
- def _parse_ai_message(message: BaseMessage) -> Union[List[AgentAction], AgentFinish]:
38
+ def _parse_ai_message(message: BaseMessage) -> Union[list[AgentAction], AgentFinish]:
38
39
  """Parse an AI message."""
39
40
  if not isinstance(message, AIMessage):
40
41
  raise TypeError(f"Expected an AI message got {type(message)}")
@@ -58,7 +59,7 @@ def _parse_ai_message(message: BaseMessage) -> Union[List[AgentAction], AgentFin
58
59
  f"the `arguments` JSON does not contain `actions` key."
59
60
  )
60
61
 
61
- final_tools: List[AgentAction] = []
62
+ final_tools: list[AgentAction] = []
62
63
  for tool_schema in tools:
63
64
  if "action" in tool_schema:
64
65
  _tool_input = tool_schema["action"]
@@ -112,7 +113,7 @@ class OpenAIMultiFunctionsAgent(BaseMultiActionAgent):
112
113
  tools: Sequence[BaseTool]
113
114
  prompt: BasePromptTemplate
114
115
 
115
- def get_allowed_tools(self) -> List[str]:
116
+ def get_allowed_tools(self) -> list[str]:
116
117
  """Get allowed tools."""
117
118
  return [t.name for t in self.tools]
118
119
 
@@ -127,12 +128,12 @@ class OpenAIMultiFunctionsAgent(BaseMultiActionAgent):
127
128
  return self
128
129
 
129
130
  @property
130
- def input_keys(self) -> List[str]:
131
+ def input_keys(self) -> list[str]:
131
132
  """Get input keys. Input refers to user input here."""
132
133
  return ["input"]
133
134
 
134
135
  @property
135
- def functions(self) -> List[dict]:
136
+ def functions(self) -> list[dict]:
136
137
  """Get the functions for the agent."""
137
138
  enum_vals = [t.name for t in self.tools]
138
139
  tool_selection = {
@@ -194,10 +195,10 @@ class OpenAIMultiFunctionsAgent(BaseMultiActionAgent):
194
195
 
195
196
  def plan(
196
197
  self,
197
- intermediate_steps: List[Tuple[AgentAction, str]],
198
+ intermediate_steps: list[tuple[AgentAction, str]],
198
199
  callbacks: Callbacks = None,
199
200
  **kwargs: Any,
200
- ) -> Union[List[AgentAction], AgentFinish]:
201
+ ) -> Union[list[AgentAction], AgentFinish]:
201
202
  """Given input, decided what to do.
202
203
 
203
204
  Args:
@@ -224,10 +225,10 @@ class OpenAIMultiFunctionsAgent(BaseMultiActionAgent):
224
225
 
225
226
  async def aplan(
226
227
  self,
227
- intermediate_steps: List[Tuple[AgentAction, str]],
228
+ intermediate_steps: list[tuple[AgentAction, str]],
228
229
  callbacks: Callbacks = None,
229
230
  **kwargs: Any,
230
- ) -> Union[List[AgentAction], AgentFinish]:
231
+ ) -> Union[list[AgentAction], AgentFinish]:
231
232
  """Async given input, decided what to do.
232
233
 
233
234
  Args:
@@ -258,7 +259,7 @@ class OpenAIMultiFunctionsAgent(BaseMultiActionAgent):
258
259
  system_message: Optional[SystemMessage] = SystemMessage(
259
260
  content="You are a helpful AI assistant."
260
261
  ),
261
- extra_prompt_messages: Optional[List[BaseMessagePromptTemplate]] = None,
262
+ extra_prompt_messages: Optional[list[BaseMessagePromptTemplate]] = None,
262
263
  ) -> BasePromptTemplate:
263
264
  """Create prompt for this agent.
264
265
 
@@ -272,7 +273,7 @@ class OpenAIMultiFunctionsAgent(BaseMultiActionAgent):
272
273
  A prompt template to pass into this agent.
273
274
  """
274
275
  _prompts = extra_prompt_messages or []
275
- messages: List[Union[BaseMessagePromptTemplate, BaseMessage]]
276
+ messages: list[Union[BaseMessagePromptTemplate, BaseMessage]]
276
277
  if system_message:
277
278
  messages = [system_message]
278
279
  else:
@@ -285,7 +286,7 @@ class OpenAIMultiFunctionsAgent(BaseMultiActionAgent):
285
286
  MessagesPlaceholder(variable_name="agent_scratchpad"),
286
287
  ]
287
288
  )
288
- return ChatPromptTemplate(messages=messages) # type: ignore[arg-type, call-arg]
289
+ return ChatPromptTemplate(messages=messages)
289
290
 
290
291
  @classmethod
291
292
  def from_llm_and_tools(
@@ -293,7 +294,7 @@ class OpenAIMultiFunctionsAgent(BaseMultiActionAgent):
293
294
  llm: BaseLanguageModel,
294
295
  tools: Sequence[BaseTool],
295
296
  callback_manager: Optional[BaseCallbackManager] = None,
296
- extra_prompt_messages: Optional[List[BaseMessagePromptTemplate]] = None,
297
+ extra_prompt_messages: Optional[list[BaseMessagePromptTemplate]] = None,
297
298
  system_message: Optional[SystemMessage] = SystemMessage(
298
299
  content="You are a helpful AI assistant."
299
300
  ),
@@ -1,4 +1,5 @@
1
- from typing import Optional, Sequence
1
+ from collections.abc import Sequence
2
+ from typing import Optional
2
3
 
3
4
  from langchain_core.language_models import BaseLanguageModel
4
5
  from langchain_core.prompts.chat import ChatPromptTemplate
@@ -1,6 +1,6 @@
1
1
  import json
2
2
  from json import JSONDecodeError
3
- from typing import List, Union
3
+ from typing import Union
4
4
 
5
5
  from langchain_core.agents import AgentAction, AgentActionMessageLog, AgentFinish
6
6
  from langchain_core.exceptions import OutputParserException
@@ -77,7 +77,7 @@ class OpenAIFunctionsAgentOutputParser(AgentOutputParser):
77
77
  )
78
78
 
79
79
  def parse_result(
80
- self, result: List[Generation], *, partial: bool = False
80
+ self, result: list[Generation], *, partial: bool = False
81
81
  ) -> Union[AgentAction, AgentFinish]:
82
82
  if not isinstance(result[0], ChatGeneration):
83
83
  raise ValueError("This output parser only works on ChatGeneration output")
@@ -1,4 +1,4 @@
1
- from typing import List, Union
1
+ from typing import Union
2
2
 
3
3
  from langchain_core.agents import AgentAction, AgentFinish
4
4
  from langchain_core.messages import BaseMessage
@@ -15,12 +15,12 @@ OpenAIToolAgentAction = ToolAgentAction
15
15
 
16
16
  def parse_ai_message_to_openai_tool_action(
17
17
  message: BaseMessage,
18
- ) -> Union[List[AgentAction], AgentFinish]:
18
+ ) -> Union[list[AgentAction], AgentFinish]:
19
19
  """Parse an AI message potentially containing tool_calls."""
20
20
  tool_actions = parse_ai_message_to_tool_action(message)
21
21
  if isinstance(tool_actions, AgentFinish):
22
22
  return tool_actions
23
- final_actions: List[AgentAction] = []
23
+ final_actions: list[AgentAction] = []
24
24
  for action in tool_actions:
25
25
  if isinstance(action, ToolAgentAction):
26
26
  final_actions.append(
@@ -54,12 +54,12 @@ class OpenAIToolsAgentOutputParser(MultiActionAgentOutputParser):
54
54
  return "openai-tools-agent-output-parser"
55
55
 
56
56
  def parse_result(
57
- self, result: List[Generation], *, partial: bool = False
58
- ) -> Union[List[AgentAction], AgentFinish]:
57
+ self, result: list[Generation], *, partial: bool = False
58
+ ) -> Union[list[AgentAction], AgentFinish]:
59
59
  if not isinstance(result[0], ChatGeneration):
60
60
  raise ValueError("This output parser only works on ChatGeneration output")
61
61
  message = result[0].message
62
62
  return parse_ai_message_to_openai_tool_action(message)
63
63
 
64
- def parse(self, text: str) -> Union[List[AgentAction], AgentFinish]:
64
+ def parse(self, text: str) -> Union[list[AgentAction], AgentFinish]:
65
65
  raise ValueError("Can only parse messages")
@@ -1,6 +1,7 @@
1
1
  import json
2
2
  import re
3
- from typing import Pattern, Union
3
+ from re import Pattern
4
+ from typing import Union
4
5
 
5
6
  from langchain_core.agents import AgentAction, AgentFinish
6
7
  from langchain_core.exceptions import OutputParserException
@@ -1,4 +1,5 @@
1
- from typing import Sequence, Union
1
+ from collections.abc import Sequence
2
+ from typing import Union
2
3
 
3
4
  from langchain_core.agents import AgentAction, AgentFinish
4
5
  from langchain_core.exceptions import OutputParserException
@@ -1,6 +1,6 @@
1
1
  import json
2
2
  from json import JSONDecodeError
3
- from typing import List, Union
3
+ from typing import Union
4
4
 
5
5
  from langchain_core.agents import AgentAction, AgentActionMessageLog, AgentFinish
6
6
  from langchain_core.exceptions import OutputParserException
@@ -14,19 +14,19 @@ from langchain_core.outputs import ChatGeneration, Generation
14
14
  from langchain.agents.agent import MultiActionAgentOutputParser
15
15
 
16
16
 
17
- class ToolAgentAction(AgentActionMessageLog): # type: ignore[override]
17
+ class ToolAgentAction(AgentActionMessageLog):
18
18
  tool_call_id: str
19
19
  """Tool call that this message is responding to."""
20
20
 
21
21
 
22
22
  def parse_ai_message_to_tool_action(
23
23
  message: BaseMessage,
24
- ) -> Union[List[AgentAction], AgentFinish]:
24
+ ) -> Union[list[AgentAction], AgentFinish]:
25
25
  """Parse an AI message potentially containing tool_calls."""
26
26
  if not isinstance(message, AIMessage):
27
27
  raise TypeError(f"Expected an AI message got {type(message)}")
28
28
 
29
- actions: List = []
29
+ actions: list = []
30
30
  if message.tool_calls:
31
31
  tool_calls = message.tool_calls
32
32
  else:
@@ -91,12 +91,12 @@ class ToolsAgentOutputParser(MultiActionAgentOutputParser):
91
91
  return "tools-agent-output-parser"
92
92
 
93
93
  def parse_result(
94
- self, result: List[Generation], *, partial: bool = False
95
- ) -> Union[List[AgentAction], AgentFinish]:
94
+ self, result: list[Generation], *, partial: bool = False
95
+ ) -> Union[list[AgentAction], AgentFinish]:
96
96
  if not isinstance(result[0], ChatGeneration):
97
97
  raise ValueError("This output parser only works on ChatGeneration output")
98
98
  message = result[0].message
99
99
  return parse_ai_message_to_tool_action(message)
100
100
 
101
- def parse(self, text: str) -> Union[List[AgentAction], AgentFinish]:
101
+ def parse(self, text: str) -> Union[list[AgentAction], AgentFinish]:
102
102
  raise ValueError("Can only parse messages")
@@ -1,6 +1,7 @@
1
1
  from __future__ import annotations
2
2
 
3
- from typing import List, Optional, Sequence, Union
3
+ from collections.abc import Sequence
4
+ from typing import Optional, Union
4
5
 
5
6
  from langchain_core.language_models import BaseLanguageModel
6
7
  from langchain_core.prompts import BasePromptTemplate
@@ -20,7 +21,7 @@ def create_react_agent(
20
21
  output_parser: Optional[AgentOutputParser] = None,
21
22
  tools_renderer: ToolsRenderer = render_text_description,
22
23
  *,
23
- stop_sequence: Union[bool, List[str]] = True,
24
+ stop_sequence: Union[bool, list[str]] = True,
24
25
  ) -> Runnable:
25
26
  """Create an agent that uses ReAct prompting.
26
27
 
@@ -2,7 +2,8 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
- from typing import TYPE_CHECKING, Any, List, Optional, Sequence
5
+ from collections.abc import Sequence
6
+ from typing import TYPE_CHECKING, Any, Optional
6
7
 
7
8
  from langchain_core._api import deprecated
8
9
  from langchain_core.documents import Document
@@ -65,7 +66,7 @@ class ReActDocstoreAgent(Agent):
65
66
  return "Observation: "
66
67
 
67
68
  @property
68
- def _stop(self) -> List[str]:
69
+ def _stop(self) -> list[str]:
69
70
  return ["\nObservation:"]
70
71
 
71
72
  @property
@@ -122,7 +123,7 @@ class DocstoreExplorer:
122
123
  return self._paragraphs[0]
123
124
 
124
125
  @property
125
- def _paragraphs(self) -> List[str]:
126
+ def _paragraphs(self) -> list[str]:
126
127
  if self.document is None:
127
128
  raise ValueError("Cannot get paragraphs without a document")
128
129
  return self.document.page_content.split("\n\n")
@@ -1,4 +1,4 @@
1
- from typing import Any, Dict, List, Tuple
1
+ from typing import Any
2
2
 
3
3
  from langchain_core.agents import AgentAction
4
4
  from langchain_core.prompts.chat import ChatPromptTemplate
@@ -12,7 +12,7 @@ class AgentScratchPadChatPromptTemplate(ChatPromptTemplate):
12
12
  return False
13
13
 
14
14
  def _construct_agent_scratchpad(
15
- self, intermediate_steps: List[Tuple[AgentAction, str]]
15
+ self, intermediate_steps: list[tuple[AgentAction, str]]
16
16
  ) -> str:
17
17
  if len(intermediate_steps) == 0:
18
18
  return ""
@@ -26,7 +26,7 @@ class AgentScratchPadChatPromptTemplate(ChatPromptTemplate):
26
26
  f"you return as final answer):\n{thoughts}"
27
27
  )
28
28
 
29
- def _merge_partial_and_user_variables(self, **kwargs: Any) -> Dict[str, Any]:
29
+ def _merge_partial_and_user_variables(self, **kwargs: Any) -> dict[str, Any]:
30
30
  intermediate_steps = kwargs.pop("intermediate_steps")
31
31
  kwargs["agent_scratchpad"] = self._construct_agent_scratchpad(
32
32
  intermediate_steps
@@ -2,7 +2,8 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
- from typing import TYPE_CHECKING, Any, Sequence, Union
5
+ from collections.abc import Sequence
6
+ from typing import TYPE_CHECKING, Any, Union
6
7
 
7
8
  from langchain_core._api import deprecated
8
9
  from langchain_core.language_models import BaseLanguageModel
@@ -1,5 +1,6 @@
1
1
  import re
2
- from typing import Any, List, Optional, Sequence, Tuple, Union
2
+ from collections.abc import Sequence
3
+ from typing import Any, Optional, Union
3
4
 
4
5
  from langchain_core._api import deprecated
5
6
  from langchain_core.agents import AgentAction
@@ -49,7 +50,7 @@ class StructuredChatAgent(Agent):
49
50
  return "Thought:"
50
51
 
51
52
  def _construct_scratchpad(
52
- self, intermediate_steps: List[Tuple[AgentAction, str]]
53
+ self, intermediate_steps: list[tuple[AgentAction, str]]
53
54
  ) -> str:
54
55
  agent_scratchpad = super()._construct_scratchpad(intermediate_steps)
55
56
  if not isinstance(agent_scratchpad, str):
@@ -74,7 +75,7 @@ class StructuredChatAgent(Agent):
74
75
  return StructuredChatOutputParserWithRetries.from_llm(llm=llm)
75
76
 
76
77
  @property
77
- def _stop(self) -> List[str]:
78
+ def _stop(self) -> list[str]:
78
79
  return ["Observation:"]
79
80
 
80
81
  @classmethod
@@ -85,8 +86,8 @@ class StructuredChatAgent(Agent):
85
86
  suffix: str = SUFFIX,
86
87
  human_message_template: str = HUMAN_MESSAGE_TEMPLATE,
87
88
  format_instructions: str = FORMAT_INSTRUCTIONS,
88
- input_variables: Optional[List[str]] = None,
89
- memory_prompts: Optional[List[BasePromptTemplate]] = None,
89
+ input_variables: Optional[list[str]] = None,
90
+ memory_prompts: Optional[list[BasePromptTemplate]] = None,
90
91
  ) -> BasePromptTemplate:
91
92
  tool_strings = []
92
93
  for tool in tools:
@@ -117,8 +118,8 @@ class StructuredChatAgent(Agent):
117
118
  suffix: str = SUFFIX,
118
119
  human_message_template: str = HUMAN_MESSAGE_TEMPLATE,
119
120
  format_instructions: str = FORMAT_INSTRUCTIONS,
120
- input_variables: Optional[List[str]] = None,
121
- memory_prompts: Optional[List[BasePromptTemplate]] = None,
121
+ input_variables: Optional[list[str]] = None,
122
+ memory_prompts: Optional[list[BasePromptTemplate]] = None,
122
123
  **kwargs: Any,
123
124
  ) -> Agent:
124
125
  """Construct an agent from an LLM and tools."""
@@ -157,7 +158,7 @@ def create_structured_chat_agent(
157
158
  prompt: ChatPromptTemplate,
158
159
  tools_renderer: ToolsRenderer = render_text_description_and_args,
159
160
  *,
160
- stop_sequence: Union[bool, List[str]] = True,
161
+ stop_sequence: Union[bool, list[str]] = True,
161
162
  ) -> Runnable:
162
163
  """Create an agent aimed at supporting tools with multiple inputs.
163
164
 
@@ -3,7 +3,8 @@ from __future__ import annotations
3
3
  import json
4
4
  import logging
5
5
  import re
6
- from typing import Optional, Pattern, Union
6
+ from re import Pattern
7
+ from typing import Optional, Union
7
8
 
8
9
  from langchain_core.agents import AgentAction, AgentFinish
9
10
  from langchain_core.exceptions import OutputParserException
@@ -1,4 +1,5 @@
1
- from typing import Callable, List, Sequence, Tuple
1
+ from collections.abc import Sequence
2
+ from typing import Callable
2
3
 
3
4
  from langchain_core.agents import AgentAction
4
5
  from langchain_core.language_models import BaseLanguageModel
@@ -12,7 +13,7 @@ from langchain.agents.format_scratchpad.tools import (
12
13
  )
13
14
  from langchain.agents.output_parsers.tools import ToolsAgentOutputParser
14
15
 
15
- MessageFormatter = Callable[[Sequence[Tuple[AgentAction, str]]], List[BaseMessage]]
16
+ MessageFormatter = Callable[[Sequence[tuple[AgentAction, str]]], list[BaseMessage]]
16
17
 
17
18
 
18
19
  def create_tool_calling_agent(
langchain/agents/tools.py CHANGED
@@ -1,6 +1,6 @@
1
1
  """Interface for tools."""
2
2
 
3
- from typing import List, Optional
3
+ from typing import Optional
4
4
 
5
5
  from langchain_core.callbacks import (
6
6
  AsyncCallbackManagerForToolRun,
@@ -9,7 +9,7 @@ from langchain_core.callbacks import (
9
9
  from langchain_core.tools import BaseTool, tool
10
10
 
11
11
 
12
- class InvalidTool(BaseTool): # type: ignore[override]
12
+ class InvalidTool(BaseTool):
13
13
  """Tool that is run when invalid tool name is encountered by agent."""
14
14
 
15
15
  name: str = "invalid_tool"
@@ -20,7 +20,7 @@ class InvalidTool(BaseTool): # type: ignore[override]
20
20
  def _run(
21
21
  self,
22
22
  requested_tool_name: str,
23
- available_tool_names: List[str],
23
+ available_tool_names: list[str],
24
24
  run_manager: Optional[CallbackManagerForToolRun] = None,
25
25
  ) -> str:
26
26
  """Use the tool."""
@@ -33,7 +33,7 @@ class InvalidTool(BaseTool): # type: ignore[override]
33
33
  async def _arun(
34
34
  self,
35
35
  requested_tool_name: str,
36
- available_tool_names: List[str],
36
+ available_tool_names: list[str],
37
37
  run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
38
38
  ) -> str:
39
39
  """Use the tool asynchronously."""
langchain/agents/types.py CHANGED
@@ -1,4 +1,4 @@
1
- from typing import Dict, Type, Union
1
+ from typing import Union
2
2
 
3
3
  from langchain.agents.agent import BaseSingleActionAgent
4
4
  from langchain.agents.agent_types import AgentType
@@ -12,9 +12,9 @@ from langchain.agents.react.base import ReActDocstoreAgent
12
12
  from langchain.agents.self_ask_with_search.base import SelfAskWithSearchAgent
13
13
  from langchain.agents.structured_chat.base import StructuredChatAgent
14
14
 
15
- AGENT_TYPE = Union[Type[BaseSingleActionAgent], Type[OpenAIMultiFunctionsAgent]]
15
+ AGENT_TYPE = Union[type[BaseSingleActionAgent], type[OpenAIMultiFunctionsAgent]]
16
16
 
17
- AGENT_TO_CLASS: Dict[AgentType, AGENT_TYPE] = {
17
+ AGENT_TO_CLASS: dict[AgentType, AGENT_TYPE] = {
18
18
  AgentType.ZERO_SHOT_REACT_DESCRIPTION: ZeroShotAgent,
19
19
  AgentType.REACT_DOCSTORE: ReActDocstoreAgent,
20
20
  AgentType.SELF_ASK_WITH_SEARCH: SelfAskWithSearchAgent,
langchain/agents/utils.py CHANGED
@@ -1,4 +1,4 @@
1
- from typing import Sequence
1
+ from collections.abc import Sequence
2
2
 
3
3
  from langchain_core.tools import BaseTool
4
4
 
@@ -1,4 +1,5 @@
1
- from typing import Any, List, Sequence, Tuple, Union
1
+ from collections.abc import Sequence
2
+ from typing import Any, Union
2
3
 
3
4
  from langchain_core._api import deprecated
4
5
  from langchain_core.agents import AgentAction, AgentFinish
@@ -38,13 +39,13 @@ class XMLAgent(BaseSingleActionAgent):
38
39
 
39
40
  """
40
41
 
41
- tools: List[BaseTool]
42
+ tools: list[BaseTool]
42
43
  """List of tools this agent has access to."""
43
44
  llm_chain: LLMChain
44
45
  """Chain to use to predict action."""
45
46
 
46
47
  @property
47
- def input_keys(self) -> List[str]:
48
+ def input_keys(self) -> list[str]:
48
49
  return ["input"]
49
50
 
50
51
  @staticmethod
@@ -60,7 +61,7 @@ class XMLAgent(BaseSingleActionAgent):
60
61
 
61
62
  def plan(
62
63
  self,
63
- intermediate_steps: List[Tuple[AgentAction, str]],
64
+ intermediate_steps: list[tuple[AgentAction, str]],
64
65
  callbacks: Callbacks = None,
65
66
  **kwargs: Any,
66
67
  ) -> Union[AgentAction, AgentFinish]:
@@ -84,7 +85,7 @@ class XMLAgent(BaseSingleActionAgent):
84
85
 
85
86
  async def aplan(
86
87
  self,
87
- intermediate_steps: List[Tuple[AgentAction, str]],
88
+ intermediate_steps: list[tuple[AgentAction, str]],
88
89
  callbacks: Callbacks = None,
89
90
  **kwargs: Any,
90
91
  ) -> Union[AgentAction, AgentFinish]:
@@ -113,7 +114,7 @@ def create_xml_agent(
113
114
  prompt: BasePromptTemplate,
114
115
  tools_renderer: ToolsRenderer = render_text_description,
115
116
  *,
116
- stop_sequence: Union[bool, List[str]] = True,
117
+ stop_sequence: Union[bool, list[str]] = True,
117
118
  ) -> Runnable:
118
119
  """Create an agent that uses XML to format its logic.
119
120