587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714 | class ReActAgent(Agent):
"""Agent that uses the ReAct strategy for processing tasks by interacting with tools in a loop."""
name: str = "React Agent"
max_loops: int = Field(default=15, ge=2)
inference_mode: InferenceMode = InferenceMode.DEFAULT
behaviour_on_max_loops: Behavior = Field(
default=Behavior.RAISE,
description="Define behavior when max loops are exceeded. Options are 'raise' or 'return'.",
)
parallel_tool_calls_enabled: bool = Field(
default=False,
description="Enable multi-tool execution in a single step. "
"When True, the agent can call multiple tools in parallel.",
)
format_schema: list = []
summarization_config: SummarizationConfig = Field(default_factory=SummarizationConfig)
_tool_cache: dict[ToolCacheEntry, Any] = {}
_tools: list[Tool] = []
_response_format: dict[str, Any] | None = None
def log_reasoning(self, thought: str, action: str, action_input: str, loop_num: int) -> None:
"""
Logs reasoning step of agent.
Args:
thought (str): Reasoning about next step.
action (str): Chosen action.
action_input (str): Input to the tool chosen by action.
loop_num (int): Number of reasoning loop.
"""
logger.info(
"\n------------------------------------------\n"
f"Agent {self.name}: Loop {loop_num}:\n"
f"Thought: {thought}\n"
f"Action: {action}\n"
f"Action Input: {action_input}"
"\n------------------------------------------"
)
def log_final_output(self, thought: str, final_output: str, loop_num: int) -> None:
"""
Logs final output of the agent.
Args:
final_output (str): Final output of agent.
loop_num (int): Number of reasoning loop
"""
logger.info(
"\n------------------------------------------\n"
f"Agent {self.name}: Loop {loop_num}\n"
f"Thought: {thought}\n"
f"Final answer: {final_output}"
"\n------------------------------------------\n"
)
@model_validator(mode="after")
def validate_inference_mode(self):
"""Validate whether specified model can be inferenced in provided mode."""
match self.inference_mode:
case InferenceMode.FUNCTION_CALLING:
if not supports_function_calling(model=self.llm.model):
raise ValueError(f"Model {self.llm.model} does not support function calling")
case InferenceMode.STRUCTURED_OUTPUT:
params = get_supported_openai_params(model=self.llm.model)
if "response_format" not in params:
raise ValueError(f"Model {self.llm.model} does not support structured output")
return self
def _parse_thought(self, output: str) -> tuple[str | None, str | None]:
"""Extracts thought from the output string."""
thought_match = re.search(
r"Thought:\s*(.*?)Action",
output,
re.DOTALL,
)
if thought_match:
return thought_match.group(1).strip()
return ""
def _parse_action(self, output: str) -> tuple[str | None, str | None, dict | list | None]:
"""
Parses the action(s), input(s),
and thought from the output string.
Supports both single tool actions
and multiple sequential tool calls when multi-tool is enabled.
Args:
output (str): The output string from the LLM containing Thought, Action, and Action Input.
Returns:
tuple: (thought, action_type, actions_data) where:
- thought is the extracted reasoning
- action_type is either a tool name (for single tool) or "multiple_tools" (for multiple tools)
- actions_data is either a dict (for single tool) or a list of dicts (for multiple tools)
"""
try:
thought_pattern = r"Thought:\s*(.*?)(?:Action:|$)"
thought_match = re.search(thought_pattern, output, re.DOTALL)
thought = thought_match.group(1).strip() if thought_match else None
action_pattern = r"Action:\s*(.*?)\nAction Input:\s*((?:[\[{][\s\S]*?[\]}]))"
remaining_text = output
actions = []
while "Action:" in remaining_text:
action_match = re.search(action_pattern, remaining_text, re.DOTALL)
if not action_match:
break
action_name = action_match.group(1).strip()
raw_input = action_match.group(2).strip()
for marker in ["```json", "```JSON", "```"]:
raw_input = raw_input.replace(marker, "").strip()
try:
action_input = json.loads(raw_input)
actions.append({"tool_name": action_name, "tool_input": action_input})
except json.JSONDecodeError as e:
raise ActionParsingException(
f"Invalid JSON in Action Input for {action_name}: {str(e)}",
recoverable=True,
)
end_pos = action_match.end()
remaining_text = remaining_text[end_pos:]
if not actions:
raise ActionParsingException(
"No valid Action and Action Input pairs found in the output.",
recoverable=True,
)
if not self.parallel_tool_calls_enabled or len(actions) == 1:
action = actions[0]["tool_name"]
action_input = actions[0]["tool_input"]
return thought, action, action_input
else:
return thought, "multiple_tools", actions
except Exception as e:
if isinstance(e, ActionParsingException):
raise
raise ActionParsingException(
f"Error parsing action(s): {str(e)}. "
f"Please ensure the output follows the format 'Thought: <text> "
f"Action: <action> Action Input: <valid JSON>' "
f"{'with possible multiple Action/Action Input pairs.' if self.parallel_tool_calls_enabled else ''}",
recoverable=True,
)
def tracing_final(self, loop_num, final_answer, config, kwargs):
self._intermediate_steps[loop_num]["final_answer"] = final_answer
def tracing_intermediate(self, loop_num, formatted_prompt, llm_generated_output):
self._intermediate_steps[loop_num] = AgentIntermediateStep(
input_data={"prompt": formatted_prompt},
model_observation=AgentIntermediateStepModelObservation(
initial=llm_generated_output,
),
).model_dump(by_alias=True)
def _extract_final_answer(self, output: str) -> str:
"""Extracts the final thought and answer as a tuple from the output string."""
match = re.search(r"Thought:\s*(.*?)\s*Answer:\s*(.*)", output, re.DOTALL)
if match:
thought = match.group(1).strip()
answer = match.group(2).strip()
return thought, answer
else:
return "", ""
def stream_reasoning(self, content: dict[str, Any], config: RunnableConfig, **kwargs) -> None:
"""
Streams intermediate reasoning of the Agent.
Args:
content (dict[str, Any]): Content that will be sent.
config (RunnableConfig | None): Configuration for the agent run.
**kwargs: Additional parameters for running the agent.
"""
if self.streaming.enabled and self.streaming.mode == StreamingMode.ALL:
self.stream_content(
content=content,
source=self.name,
step="reasoning",
config=config,
by_tokens=False,
**kwargs,
)
def is_token_limit_exceeded(self) -> bool:
"""Check whether token limit for summarization is exceeded.
Returns:
bool: Whether token limit is exceeded.
"""
prompt_tokens = self._prompt.count_tokens(self.llm.model)
return (
self.summarization_config.max_token_context_length
and prompt_tokens > self.summarization_config.max_token_context_length
) or (prompt_tokens / self.llm.get_token_limit() > self.summarization_config.context_usage_ratio)
def summarize_history(
self,
input_message,
history_offset: int,
summary_offset: int,
config: RunnableConfig | None = None,
**kwargs,
) -> None:
"""
Summarizes history and saves relevant information in the context
Args:
input_message (Message | VisionMessage): User request message.
history_offset (int): Offset to the first message in the conversation history within the prompt.
summary_offset (int): Offset to the position of the first message in prompt that was not summarized.
config (RunnableConfig | None): Configuration for the agent run.
**kwargs: Additional parameters for running the agent.
Returns:
int: Number of summarized messages.
"""
logger.info(f"Agent {self.name} - {self.id}: Summarization of tool output started.")
messages_history = "\nHistory to extract information from: \n"
summary_sections = []
offset = max(history_offset, summary_offset - self.summarization_config.context_history_length)
for index, message in enumerate(self._prompt.messages[offset:]):
if message.role == MessageRole.USER:
if index + offset >= summary_offset:
messages_history += (
f"=== TOOL_OUTPUT: {index + offset} === \n {message.content}"
f"\n === TOOL_OUTPUT: {index + offset} === \n"
)
summary_sections.append(index + offset)
else:
messages_history += f"\n{message.content}\n"
messages_history = (
messages_history + f"\n Required tags in the output {[f'tool_output{index}' for index in summary_sections]}"
)
summary_messages = [
Message(content=HISTORY_SUMMARIZATION_PROMPT, role=MessageRole.SYSTEM, static=True),
input_message,
Message(content=messages_history, role=MessageRole.USER, static=True),
]
summary_tags = [f"tool_output{index}" for index in summary_sections]
for _ in range(self.max_loops):
llm_result = self._run_llm(
messages=summary_messages,
config=config,
**kwargs,
)
output = llm_result.output["content"]
summary_messages.append(Message(content=output, role=MessageRole.ASSISTANT, static=True))
try:
parsed_data = XMLParser.parse(
f"<root>{output}</root>",
required_tags=summary_tags,
optional_tags=[],
)
except ParsingError as e:
logger.error(f"Error: {e}. Make sure you have provided all tags: {summary_tags}")
summary_messages.append(Message(content=str(e), role=MessageRole.USER, static=True))
continue
for summary_index, message_index in enumerate(summary_sections[:-1]):
self._prompt.messages[message_index].content = (
f"Observation (shortened): \n{parsed_data.get(summary_tags[summary_index])}"
)
if self.is_token_limit_exceeded():
self._prompt.messages[summary_sections[-1]].content = (
f"Observation (shortened): \n{parsed_data.get(summary_tags[-1])}"
)
summary_offset = len(self._prompt.messages)
else:
summary_offset = len(self._prompt.messages) - 2
logger.info(f"Agent {self.name} - {self.id}: Summarization of tool output finished.")
return summary_offset
def _run_agent(
self,
input_message: Message | VisionMessage,
history_messages: list[Message] | None = None,
config: RunnableConfig | None = None,
**kwargs,
) -> str:
"""
Executes the ReAct strategy by iterating through thought, action, and observation cycles.
Args:
config (RunnableConfig | None): Configuration for the agent run.
**kwargs: Additional parameters for running the agent.
Returns:
str: Final answer provided by the agent.
Raises:
RuntimeError: If the maximum number of loops is reached without finding a final answer.
Exception: If an error occurs during execution.
"""
if self.verbose:
logger.info(f"Agent {self.name} - {self.id}: Running ReAct strategy")
system_message = Message(
role=MessageRole.SYSTEM,
content=self.generate_prompt(
tools_name=self.tool_names, input_formats=self.generate_input_formats(self.tools)
),
static=True,
)
if history_messages:
self._prompt.messages = [system_message, *history_messages, input_message]
else:
self._prompt.messages = [system_message, input_message]
summary_offset = history_offset = len(self._prompt.messages)
stop_sequences = []
if self.inference_mode in [InferenceMode.XML, InferenceMode.DEFAULT]:
stop_sequences.extend(["Observation: ", "\nObservation:"])
self.llm.stop = stop_sequences
for loop_num in range(1, self.max_loops + 1):
try:
llm_result = self._run_llm(
messages=self._prompt.messages,
tools=self._tools,
response_format=self._response_format,
config=config,
**kwargs,
)
action, action_input = None, None
llm_generated_output = ""
llm_reasoning = (
llm_result.output.get("content")[:200]
if llm_result.output.get("content")
else str(llm_result.output.get("tool_calls", ""))[:200]
)
logger.info(f"Agent {self.name} - {self.id}: Loop {loop_num}, " f"reasoning:\n{llm_reasoning}...")
match self.inference_mode:
case InferenceMode.DEFAULT:
llm_generated_output = llm_result.output.get("content", "")
self.tracing_intermediate(loop_num, self._prompt.messages, llm_generated_output)
if "Answer:" in llm_generated_output:
thought, final_answer = self._extract_final_answer(llm_generated_output)
self.log_final_output(thought, final_answer, loop_num)
self.tracing_final(loop_num, final_answer, config, kwargs)
if self.streaming.enabled:
if self.streaming.mode == StreamingMode.ALL:
self.stream_content(
content={"thought": thought, "loop_num": loop_num},
source=self.name,
step="reasoning",
config=config,
**kwargs,
)
self.stream_content(
content=final_answer,
source=self.name,
step="answer",
config=config,
**kwargs,
)
return final_answer
thought, action, action_input = self._parse_action(llm_generated_output)
self.log_reasoning(thought, action, action_input, loop_num)
case InferenceMode.FUNCTION_CALLING:
if self.verbose:
logger.info(f"Agent {self.name} - {self.id}: using function calling inference mode")
if "tool_calls" not in dict(llm_result.output):
logger.error("Error: No function called.")
raise ActionParsingException(
"Error: No function called, you need to call the correct function."
)
action = list(llm_result.output["tool_calls"].values())[0]["function"]["name"].strip()
llm_generated_output_json = list(llm_result.output["tool_calls"].values())[0]["function"][
"arguments"
]
llm_generated_output = json.dumps(llm_generated_output_json)
self.tracing_intermediate(loop_num, self._prompt.messages, llm_generated_output)
thought = llm_generated_output_json["thought"]
if action == "provide_final_answer":
final_answer = llm_generated_output_json["answer"]
self.log_final_output(thought, final_answer, loop_num)
self.tracing_final(loop_num, final_answer, config, kwargs)
if self.streaming.enabled:
if self.streaming.mode == StreamingMode.ALL:
self.stream_content(
content={"thought": thought, "loop_num": loop_num},
source=self.name,
step="reasoning",
config=config,
**kwargs,
)
self.stream_content(
content=final_answer,
source=self.name,
step="answer",
config=config,
**kwargs,
)
return final_answer
action_input = llm_generated_output_json["action_input"]
if isinstance(action_input, str):
try:
action_input = json.loads(action_input)
except json.JSONDecodeError as e:
raise ActionParsingException(
f"Error parsing action_input string. {e}", recoverable=True
)
self.log_reasoning(thought, action, action_input, loop_num)
case InferenceMode.STRUCTURED_OUTPUT:
if self.verbose:
logger.info(f"Agent {self.name} - {self.id}: using structured output inference mode")
llm_generated_output = llm_result.output["content"]
self.tracing_intermediate(loop_num, self._prompt.messages, llm_generated_output)
try:
llm_generated_output_json = json.loads(llm_generated_output)
except json.JSONDecodeError as e:
raise ActionParsingException(f"Error parsing action. {e}", recoverable=True)
thought = llm_generated_output_json["thought"]
action = llm_generated_output_json["action"]
action_input = llm_generated_output_json["action_input"]
if action == "finish":
self.log_final_output(thought, action_input, loop_num)
self.tracing_final(loop_num, action_input, config, kwargs)
if self.streaming.enabled:
if self.streaming.mode == StreamingMode.ALL:
self.stream_content(
content={"thought": thought, "loop_num": loop_num},
source=self.name,
step="reasoning",
config=config,
**kwargs,
)
self.stream_content(
content=action_input,
source=self.name,
step="answer",
config=config,
**kwargs,
)
return action_input
try:
action_input = json.loads(action_input)
except json.JSONDecodeError as e:
raise ActionParsingException(f"Error parsing action_input string. {e}", recoverable=True)
self.log_reasoning(thought, action, action_input, loop_num)
case InferenceMode.XML:
if self.verbose:
logger.info(f"Agent {self.name} - {self.id}: using XML inference mode")
llm_generated_output = llm_result.output["content"]
self.tracing_intermediate(loop_num, self._prompt.messages, llm_generated_output)
if self.parallel_tool_calls_enabled:
try:
parsed_result = XMLParser.parse_unified_xml_format(llm_generated_output)
thought = parsed_result.get("thought", "")
if parsed_result.get("is_final", False):
final_answer = parsed_result.get("answer", "")
self.log_final_output(thought, final_answer, loop_num)
self.tracing_final(loop_num, final_answer, config, kwargs)
if self.streaming.enabled:
if self.streaming.mode == StreamingMode.ALL:
self.stream_content(
content={"thought": thought, "loop_num": loop_num},
source=self.name,
step="reasoning",
config=config,
**kwargs,
)
self.stream_content(
content=final_answer,
source=self.name,
step="answer",
config=config,
**kwargs,
)
return final_answer
tools_data = parsed_result.get("tools", [])
action = tools_data
if len(tools_data) == 1:
self.log_reasoning(
thought,
tools_data[0].get("name", "unknown_tool"),
tools_data[0].get("input", {}),
loop_num,
)
else:
self.log_reasoning(thought, "multiple_tools", str(tools_data), loop_num)
self.stream_reasoning(
{
"thought": thought,
"tools": tools_data,
"loop_num": loop_num,
},
config,
**kwargs,
)
except (XMLParsingError, TagNotFoundError, JSONParsingError) as e:
self._prompt.messages.append(
Message(role=MessageRole.ASSISTANT, content=llm_generated_output)
)
self._prompt.messages.append(
Message(
role=MessageRole.SYSTEM,
content=f"Correction Instruction: "
f"The previous response could not be parsed due to "
f"the following error: '{type(e).__name__}: {e}'. "
f"Please regenerate the response strictly following the "
f"required XML format, ensuring all tags are present and "
f"correctly structured, and that any JSON content is valid.",
)
)
continue
else:
try:
parsed_data = XMLParser.parse(
llm_generated_output, required_tags=["thought", "answer"], optional_tags=["output"]
)
thought = parsed_data.get("thought")
final_answer = parsed_data.get("answer")
self.log_final_output(thought, final_answer, loop_num)
self.tracing_final(loop_num, final_answer, config, kwargs)
if self.streaming.enabled:
if self.streaming.mode == StreamingMode.ALL:
self.stream_content(
content={"thought": thought, "loop_num": loop_num},
source=self.name,
step="reasoning",
config=config,
**kwargs,
)
self.stream_content(
content=final_answer,
source=self.name,
step="answer",
config=config,
**kwargs,
)
return final_answer
except TagNotFoundError:
logger.debug("XMLParser: Not a final answer structure, trying action structure.")
try:
parsed_data = XMLParser.parse(
llm_generated_output,
required_tags=["thought", "action", "action_input"],
optional_tags=["output"],
json_fields=["action_input"],
)
thought = parsed_data.get("thought")
action = parsed_data.get("action")
action_input = parsed_data.get("action_input")
self.log_reasoning(thought, action, action_input, loop_num)
except (XMLParsingError, TagNotFoundError, JSONParsingError) as e:
logger.error(f"XMLParser: Failed to parse XML for action or answer: {e}")
raise ActionParsingException(f"Error parsing LLM output: {e}", recoverable=True)
except (XMLParsingError, JSONParsingError) as e:
logger.error(f"XMLParser: Error parsing potential final answer XML: {e}")
raise ActionParsingException(f"Error parsing LLM output: {e}", recoverable=True)
self._prompt.messages.append(
Message(role=MessageRole.ASSISTANT, content=llm_generated_output, static=True)
)
if action and self.tools:
tool_result = None
if self.inference_mode == InferenceMode.XML and self.parallel_tool_calls_enabled:
tool_result = self._execute_tools(tools_data, config, **kwargs)
elif self.inference_mode == InferenceMode.DEFAULT and self.parallel_tool_calls_enabled:
if action == "multiple_tools":
tools_data = []
for tool_call in action_input:
if (
not isinstance(tool_call, dict)
or "tool_name" not in tool_call
or "tool_input" not in tool_call
):
raise ActionParsingException(
"Invalid tool call format. "
"Each tool call must have 'tool_name' and 'tool_input'.",
recoverable=True,
)
tools_data.append({"name": tool_call["tool_name"], "input": tool_call["tool_input"]})
self.stream_reasoning(
{
"thought": thought,
"action": "multiple_tools",
"tools": tools_data,
"loop_num": loop_num,
},
config,
**kwargs,
)
tool_result = self._execute_tools(tools_data, config, **kwargs)
action_input_json = json.dumps(action_input)
self._intermediate_steps[loop_num]["model_observation"].update(
AgentIntermediateStepModelObservation(
tool_using="multiple_tools",
tool_input=str(action_input_json),
tool_output=str(tool_result),
updated=llm_generated_output,
).model_dump()
)
else:
try:
tool = self.tool_by_names.get(self.sanitize_tool_name(action))
if not tool:
raise AgentUnknownToolException(
f"Unknown tool: {action}."
"Use only available tools and provide "
"only the tool's name in the action field. "
"Do not include any additional reasoning. "
"Please correct the action field or state that you cannot answer the question. "
)
self.stream_reasoning(
{
"thought": thought,
"action": action,
"tool": tool,
"action_input": action_input,
"loop_num": loop_num,
},
config,
**kwargs,
)
tool_result = self._run_tool(tool, action_input, config, **kwargs)
except RecoverableAgentException as e:
tool_result = f"{type(e).__name__}: {e}"
else:
# Handle single tool execution
try:
tool = self.tool_by_names.get(self.sanitize_tool_name(action))
if not tool:
raise AgentUnknownToolException(
f"Unknown tool: {action}."
"Use only available tools and provide only the tool's name in the action field. "
"Do not include any additional reasoning. "
"Please correct the action field or state that you cannot answer the question."
)
self.stream_reasoning(
{
"thought": thought,
"action": action,
"tool": tool,
"action_input": action_input,
"loop_num": loop_num,
},
config,
**kwargs,
)
# Check tool cache first
tool_cache_entry = ToolCacheEntry(action=action, action_input=action_input)
tool_result = self._tool_cache.get(tool_cache_entry, None)
if not tool_result:
tool_result = self._run_tool(tool, action_input, config, **kwargs)
self._tool_cache[tool_cache_entry] = tool_result
else:
logger.info(f"Agent {self.name} - {self.id}: Cached output of {action} found.")
except RecoverableAgentException as e:
tool_result = f"{type(e).__name__}: {e}"
# Add observation to prompt
observation = f"\nObservation: {tool_result}\n"
self._prompt.messages.append(Message(role=MessageRole.USER, content=observation, static=True))
if self.streaming.enabled and self.streaming.mode == StreamingMode.ALL:
self.stream_content(
content={
"name": tool.name if "tool" in locals() else action,
"input": action_input,
"result": tool_result,
},
source=tool.name if "tool" in locals() else action,
step="tool",
config=config,
by_tokens=False,
**kwargs,
)
self._intermediate_steps[loop_num]["model_observation"].update(
AgentIntermediateStepModelObservation(
tool_using=action,
tool_input=action_input,
tool_output=tool_result,
updated=llm_generated_output,
).model_dump()
)
else:
self.stream_reasoning(
{
"thought": thought,
"action": action,
"action_input": action_input,
"loop_num": loop_num,
},
config,
**kwargs,
)
except ActionParsingException as e:
self._prompt.messages.append(
Message(role=MessageRole.ASSISTANT, content="Response is:" + llm_generated_output, static=True)
)
self._prompt.messages.append(
Message(
role=MessageRole.ASSISTANT,
content=f"Correction Instruction: The previous response could not be parsed due to "
f"the following error: '{type(e).__name__}: {e}'. "
f"Please regenerate the response strictly following the "
f"required XML format, ensuring all tags are present and "
f"correctly structured, and that any JSON content (like action_input) is valid.",
static=True,
)
)
continue
if self.summarization_config.enabled:
if self.is_token_limit_exceeded():
summary_offset = self.summarize_history(
input_message, history_offset, summary_offset, config=config, **kwargs
)
if self.behaviour_on_max_loops == Behavior.RAISE:
error_message = (
f"Agent {self.name} (ID: {self.id}) "
f"has reached the maximum loop limit of {self.max_loops} "
f"without finding a final answer. "
f"Last response: {self._prompt.messages[-1].content}\n"
f"Consider increasing the maximum number of loops or "
f"reviewing the task complexity to ensure completion."
)
raise MaxLoopsExceededException(message=error_message)
else:
max_loop_final_answer = self._handle_max_loops_exceeded(input_message, config, **kwargs)
if self.streaming.enabled:
self.stream_content(
content=max_loop_final_answer,
source=self.name,
step="answer",
config=config,
**kwargs,
)
return max_loop_final_answer
def aggregate_history(self, messages: list[Message, VisionMessage]) -> str:
"""
Concatenates multiple history messages into one unified string.
Args:
messages (list[Message, VisionMessage]): List of messages to aggregate.
Returns:
str: Aggregated content.
"""
history = ""
for message in messages:
if isinstance(message, VisionMessage):
for content in message.content:
if isinstance(content, VisionMessageTextContent):
history += content.text
else:
if message.role == MessageRole.ASSISTANT:
history += f"-TOOL DESCRIPTION START-\n{message.content}\n-TOOL DESCRIPTION END-\n"
elif message.role == MessageRole.USER:
history += f"-TOOL OUTPUT START-\n{message.content}\n-TOOL OUTPUT END-\n"
return history
def _handle_max_loops_exceeded(
self, input_message: Message | VisionMessage, config: RunnableConfig | None = None, **kwargs
) -> str:
"""
Handle the case where max loops are exceeded by crafting a thoughtful response.
Uses XMLParser to extract the final answer from the LLM's last attempt.
Args:
input_message (Message | VisionMessage): Initial user message.
config (RunnableConfig | None): Configuration for the agent run.
**kwargs: Additional parameters for running the agent.
Returns:
str: Final answer provided by the agent.
"""
system_message = Message(content=REACT_MAX_LOOPS_PROMPT, role=MessageRole.SYSTEM, static=True)
conversation_history = Message(
content=self.aggregate_history(self._prompt.messages), role=MessageRole.USER, static=True
)
llm_final_attempt_result = self._run_llm(
[system_message, input_message, conversation_history], config=config, **kwargs
)
llm_final_attempt = llm_final_attempt_result.output["content"]
self._run_depends = [NodeDependency(node=self.llm).to_dict()]
try:
final_answer = XMLParser.extract_first_tag_lxml(llm_final_attempt, ["answer"])
if final_answer is None:
logger.warning("Max loops handler: lxml failed to extract <answer>, falling back to regex.")
final_answer = XMLParser.extract_first_tag_regex(llm_final_attempt, ["answer"])
if final_answer is None:
logger.error(
"Max loops handler: Failed to extract <answer> tag even with fallbacks. Returning raw output."
)
final_answer = llm_final_attempt
except Exception as e:
logger.error(f"Max loops handler: Error during final answer extraction: {e}. Returning raw output.")
final_answer = llm_final_attempt
return f"{final_answer}"
def generate_input_formats(self, tools: list[Node]) -> str:
"""Generate formatted input descriptions for each tool."""
input_formats = []
for tool in tools:
params = []
for name, field in tool.input_schema.model_fields.items():
if not field.json_schema_extra or field.json_schema_extra.get("is_accessible_to_agent", True):
if get_origin(field.annotation) in (Union, types.UnionType):
type_str = str(field.annotation)
else:
type_str = getattr(field.annotation, "__name__", str(field.annotation))
description = field.description or "No description"
params.append(f"{name} ({type_str}): {description}")
if params:
input_formats.append(f" - {self.sanitize_tool_name(tool.name)}\n \t* " + "\n\t* ".join(params))
return "\n".join(input_formats)
def generate_structured_output_schemas(self):
tool_names = [self.sanitize_tool_name(tool.name) for tool in self.tools]
schema = {
"type": "json_schema",
"json_schema": {
"name": "plan_next_action",
"strict": True,
"schema": {
"type": "object",
"required": ["thought", "action", "action_input"],
"properties": {
"thought": {
"type": "string",
"description": "Your reasoning about the next step.",
},
"action": {
"type": "string",
"description": f"Next action to make (choose from [{tool_names}, finish]).",
},
"action_input": {
"type": "string",
"description": "Input for chosen action.",
},
},
"additionalProperties": False,
},
},
}
self._response_format = schema
@staticmethod
def filter_format_type(param_annotation: Any) -> list[str]:
"""
Filters proper type for a function calling schema.
Args:
param_annotation (Any): Parameter annotation.
Returns:
list[str]: List of parameter types that describe provided annotation.
"""
if get_origin(param_annotation) in (Union, types.UnionType):
return get_args(param_annotation)
return [param_annotation]
def generate_property_schema(self, properties, name, field):
if not field.json_schema_extra or field.json_schema_extra.get("is_accessible_to_agent", True):
description = field.description or "No description."
description += f" Defaults to: {field.default}." if field.default and not field.is_required() else ""
params = self.filter_format_type(field.annotation)
properties[name] = {"type": [], "description": description}
for param in params:
if param is type(None):
properties[name]["type"].append("null")
elif param_type := TYPE_MAPPING.get(param):
properties[name]["type"].append(param_type)
elif issubclass(param, Enum):
element_type = TYPE_MAPPING.get(
self.filter_format_type(type(list(param.__members__.values())[0].value))[0]
)
properties[name]["type"].append(element_type)
properties[name]["enum"] = [field.value for field in param.__members__.values()]
elif getattr(param, "__origin__", None) is list:
properties[name]["type"].append("array")
properties[name]["items"] = {"type": TYPE_MAPPING.get(param.__args__[0])}
def generate_function_calling_schemas(self):
"""Generate schemas for function calling."""
self._tools.append(final_answer_function_schema)
for tool in self.tools:
properties = {}
input_params = tool.input_schema.model_fields.items()
if list(input_params) and not isinstance(self.llm, Gemini):
for name, field in tool.input_schema.model_fields.items():
self.generate_property_schema(properties, name, field)
schema = {
"type": "function",
"function": {
"name": self.sanitize_tool_name(tool.name),
"description": tool.description[:1024],
"parameters": {
"type": "object",
"properties": {
"thought": {
"type": "string",
"description": "Your reasoning about using this tool.",
},
"action_input": {
"type": "object",
"description": "Input for the selected tool",
"properties": properties,
"required": list(properties.keys()),
"additionalProperties": False,
},
},
"additionalProperties": False,
"required": ["thought", "action_input"],
},
"strict": True,
},
}
self._tools.append(schema)
else:
schema = {
"type": "function",
"function": {
"name": self.sanitize_tool_name(tool.name),
"description": tool.description[:1024],
"parameters": {
"type": "object",
"properties": {
"thought": {
"type": "string",
"description": "Your reasoning about using this tool.",
},
"action_input": {
"type": "string",
"description": "Input for the selected tool in JSON string format.",
},
},
"additionalProperties": False,
"required": ["thought", "action_input"],
},
"strict": True,
},
}
self._tools.append(schema)
def _init_prompt_blocks(self):
"""Initialize the prompt blocks required for the ReAct strategy."""
super()._init_prompt_blocks()
if self.parallel_tool_calls_enabled:
instructions_default = REACT_BLOCK_INSTRUCTIONS_MULTI
instructions_xml = REACT_BLOCK_XML_INSTRUCTIONS_MULTI
else:
instructions_default = REACT_BLOCK_INSTRUCTIONS_SINGLE
instructions_xml = REACT_BLOCK_XML_INSTRUCTIONS_SINGLE
prompt_blocks = {
"tools": "" if not self.tools else REACT_BLOCK_TOOLS,
"instructions": REACT_BLOCK_INSTRUCTIONS_NO_TOOLS if not self.tools else instructions_default,
"output_format": REACT_BLOCK_OUTPUT_FORMAT,
}
match self.inference_mode:
case InferenceMode.FUNCTION_CALLING:
self.generate_function_calling_schemas()
prompt_blocks["instructions"] = REACT_BLOCK_INSTRUCTIONS_FUNCTION_CALLING
if self.tools:
prompt_blocks["tools"] = REACT_BLOCK_TOOLS_NO_FORMATS
case InferenceMode.STRUCTURED_OUTPUT:
self.generate_structured_output_schemas()
prompt_blocks["instructions"] = REACT_BLOCK_INSTRUCTIONS_STRUCTURED_OUTPUT
case InferenceMode.XML:
prompt_blocks["instructions"] = (
REACT_BLOCK_XML_INSTRUCTIONS_NO_TOOLS if not self.tools else instructions_xml
)
self._prompt_blocks.update(prompt_blocks)
def _execute_tools(self, tools_data: list[dict], config: RunnableConfig, **kwargs) -> str:
"""
Execute one or more tools and gather their results.
Args:
tools_data (list): List of dictionaries containing name and input for each tool
config (RunnableConfig): Configuration for the runnable
**kwargs: Additional arguments for tool execution
Returns:
str: Combined observation string with all tool results
"""
all_results = []
for tool_data in tools_data:
try:
tool_name = tool_data["name"]
tool_input = tool_data["input"]
tool = self.tool_by_names.get(self.sanitize_tool_name(tool_name))
if not tool:
error_message = f"Unknown tool: {tool_name}. Please use only available tools."
all_results.append({"tool_name": tool_name, "success": False, "result": error_message})
continue
tool_result = self._run_tool(tool, tool_input, config, **kwargs)
all_results.append(
{"tool_name": tool_name, "success": True, "tool_input": tool_input, "result": tool_result}
)
if self.streaming.enabled and self.streaming.mode == StreamingMode.ALL:
self.stream_content(
content={"name": tool.name, "input": tool_input, "result": tool_result},
source=tool.name,
step="tool",
config=config,
by_tokens=False,
**kwargs,
)
except Exception as e:
error_message = f"Error executing tool {tool_data['name']}: {str(e)}"
logger.error(error_message)
all_results.append(
{
"tool_name": tool_data["name"],
"success": False,
"tool_input": tool_input,
"result": error_message,
}
)
observation_parts = []
for result in all_results:
tool_name = result["tool_name"]
result_content = result["result"]
success_status = "SUCCESS" if result["success"] else "ERROR"
observation_parts.append(f"--- {tool_name} has resulted in {success_status} ---\n{result_content}")
combined_observation = "\n\n".join(observation_parts)
return combined_observation
|