17
17
from langchain_core .messages import ToolCall
18
18
from langchain_core .output_parsers import BaseGenerationOutputParser
19
19
from langchain_core .outputs import ChatGeneration , Generation
20
+ from langchain_core .prompts .chat import AIMessage
20
21
from langchain_core .pydantic_v1 import BaseModel
21
22
from langchain_core .tools import BaseTool
22
23
from langchain_core .utils .function_calling import convert_to_openai_tool
@@ -177,17 +178,13 @@ def parse_result(self, result: List[Generation], *, partial: bool = False) -> An
177
178
if not result or not isinstance (result [0 ], ChatGeneration ):
178
179
return None if self .first_tool_only else []
179
180
message = result [0 ].message
180
- if isinstance (message .content , str ) :
181
+ if len (message .content ) > 0 :
181
182
tool_calls : List = []
182
183
else :
183
- content : List = message . content
184
- _tool_calls = [dict (tc ) for tc in extract_tool_calls ( content ) ]
184
+ content = cast ( AIMessage , message )
185
+ _tool_calls = [dict (tc ) for tc in content . tool_calls ]
185
186
# Map tool call id to index
186
- id_to_index = {
187
- block ["id" ]: i
188
- for i , block in enumerate (content )
189
- if block ["type" ] == "tool_use"
190
- }
187
+ id_to_index = {block ["id" ]: i for i , block in enumerate (_tool_calls )}
191
188
tool_calls = [{** tc , "index" : id_to_index [tc ["id" ]]} for tc in _tool_calls ]
192
189
if self .pydantic_schemas :
193
190
tool_calls = [self ._pydantic_parse (tc ) for tc in tool_calls ]
@@ -208,17 +205,6 @@ def _pydantic_parse(self, tool_call: dict) -> BaseModel:
208
205
return cls_ (** tool_call ["args" ])
209
206
210
207
211
- def extract_tool_calls (content : List [dict ]) -> List [ToolCall ]:
212
- tool_calls = []
213
- for block in content :
214
- if block ["type" ] != "tool_use" :
215
- continue
216
- tool_calls .append (
217
- ToolCall (name = block ["name" ], args = block ["input" ], id = block ["id" ])
218
- )
219
- return tool_calls
220
-
221
-
222
208
def convert_to_anthropic_tool (
223
209
tool : Union [Dict [str , Any ], Type [BaseModel ], Callable , BaseTool ],
224
210
) -> AnthropicTool :
0 commit comments