12
12
import os
13
13
import sys
14
14
import time
15
+ import json
15
16
from pathlib import Path
16
17
from typing import (
17
18
IO ,
@@ -844,6 +845,7 @@ class AgentsOperations(AgentsOperationsGenerated):
844
845
def __init__ (self , * args , ** kwargs ) -> None :
845
846
super ().__init__ (* args , ** kwargs )
846
847
self ._function_tool = _models .FunctionTool (set ())
848
+ self ._function_tool_max_retry = 10
847
849
848
850
# pylint: disable=arguments-differ
849
851
@overload
@@ -1803,6 +1805,7 @@ def create_and_process_run(
1803
1805
)
1804
1806
1805
1807
# Monitor and process the run status
1808
+ current_retry = 0
1806
1809
while run .status in [
1807
1810
RunStatus .QUEUED ,
1808
1811
RunStatus .IN_PROGRESS ,
@@ -1826,14 +1829,47 @@ def create_and_process_run(
1826
1829
toolset .add (self ._function_tool )
1827
1830
tool_outputs = toolset .execute_tool_calls (tool_calls )
1828
1831
1832
+ if self .has_errors_in_toolcalls_output (tool_outputs ):
1833
+ if current_retry >= self ._function_tool_max_retry :
1834
+ logging .warning (
1835
+ f"Tool outputs contain errors - reaching max retry { self ._function_tool_max_retry } "
1836
+ )
1837
+ self .cancel_run (thread_id = thread_id , run_id = run .id )
1838
+ break
1839
+ else :
1840
+ logging .warning (f"Tool outputs contain errors - retrying" )
1841
+ current_retry += 1
1842
+
1829
1843
logging .info ("Tool outputs: %s" , tool_outputs )
1830
1844
if tool_outputs :
1831
- self .submit_tool_outputs_to_run (thread_id = thread_id , run_id = run .id , tool_outputs = tool_outputs )
1845
+ run2 = self .submit_tool_outputs_to_run (
1846
+ thread_id = thread_id , run_id = run .id , tool_outputs = tool_outputs
1847
+ )
1848
+ logging .info ("Tool outputs submitted to run: %s" , run2 .id )
1832
1849
1833
1850
logging .info ("Current run status: %s" , run .status )
1834
1851
1835
1852
return run
1836
1853
1854
+ def has_errors_in_toolcalls_output (self , tool_outputs : List [Dict ]) -> bool :
1855
+ """
1856
+ Check if any tool output contains an error.
1857
+
1858
+ :param List[Dict] tool_outputs: A list of tool outputs to check.
1859
+ :return: True if any output contains an error, False otherwise.
1860
+ :rtype: bool
1861
+ """
1862
+ for tool_output in tool_outputs :
1863
+ output = tool_output .get ("output" )
1864
+ if isinstance (output , str ):
1865
+ try :
1866
+ output_json = json .loads (output )
1867
+ if "error" in output_json :
1868
+ return True
1869
+ except json .JSONDecodeError :
1870
+ continue
1871
+ return False
1872
+
1837
1873
@overload
1838
1874
def create_stream (
1839
1875
self ,
@@ -3309,27 +3345,39 @@ def delete_agent(self, agent_id: str, **kwargs: Any) -> _models.AgentDeletionSta
3309
3345
return super ().delete_agent (agent_id , ** kwargs )
3310
3346
3311
3347
@overload
3312
- def enable_auto_function_calls (self , * , functions : Set [Callable [..., Any ]]) -> None :
3348
+ def enable_auto_function_calls (self , * , functions : Set [Callable [..., Any ]], max_retry : int = 10 ) -> None :
3313
3349
"""Enables tool calls to be executed automatically during create_and_process_run or streaming.
3314
3350
If this is not set, functions must be called manually.
3351
+ If automatic function calls fail, the agents will receive error messages allowing it to retry with another
3352
+ function call or figure out the answer with its knowledge.
3315
3353
:keyword functions: A set of callable functions to be used as tools.
3316
3354
:type functions: Set[Callable[..., Any]]
3355
+ :keyword max_retry: Maximum number of errors allowed and retry per run or stream. Default value is 10.
3356
+ :type max_retry: int
3317
3357
"""
3318
3358
3319
3359
@overload
3320
- def enable_auto_function_calls (self , * , function_tool : _models .FunctionTool ) -> None :
3360
+ def enable_auto_function_calls (self , * , function_tool : _models .FunctionTool , max_retry : int = 10 ) -> None :
3321
3361
"""Enables tool calls to be executed automatically during create_and_process_run or streaming.
3322
3362
If this is not set, functions must be called manually.
3363
+ If automatic function calls fail, the agents will receive error messages allowing it to retry with another
3364
+ function call or figure out the answer with its knowledge.
3323
3365
:keyword function_tool: A FunctionTool object representing the tool to be used.
3324
3366
:type function_tool: Optional[_models.FunctionTool]
3367
+ :keyword max_retry: Maximum number of errors allowed and retry per run or stream. Default value is 10.
3368
+ :type max_retry: int
3325
3369
"""
3326
3370
3327
3371
@overload
3328
- def enable_auto_function_calls (self , * , toolset : _models .ToolSet ) -> None :
3372
+ def enable_auto_function_calls (self , * , toolset : _models .ToolSet , max_retry : int = 10 ) -> None :
3329
3373
"""Enables tool calls to be executed automatically during create_and_process_run or streaming.
3330
3374
If this is not set, functions must be called manually.
3375
+ If automatic function calls fail, the agents will receive error messages allowing it to retry with another
3376
+ function call or figure out the answer with its knowledge.
3331
3377
:keyword toolset: A ToolSet object representing the set of tools to be used.
3332
3378
:type toolset: Optional[_models.ToolSet]
3379
+ :keyword max_retry: Maximum number of errors allowed and retry per run or stream. Default value is 10.
3380
+ :type max_retry: int
3333
3381
"""
3334
3382
3335
3383
@distributed_trace
@@ -3339,15 +3387,20 @@ def enable_auto_function_calls(
3339
3387
functions : Optional [Set [Callable [..., Any ]]] = None ,
3340
3388
function_tool : Optional [_models .FunctionTool ] = None ,
3341
3389
toolset : Optional [_models .ToolSet ] = None ,
3390
+ max_retry : int = 10 ,
3342
3391
) -> None :
3343
3392
"""Enables tool calls to be executed automatically during create_and_process_run or streaming.
3344
3393
If this is not set, functions must be called manually.
3394
+ If automatic function calls fail, the agents will receive error messages allowing it to retry with another
3395
+ function call or figure out the answer with its knowledge.
3345
3396
:keyword functions: A set of callable functions to be used as tools.
3346
3397
:type functions: Set[Callable[..., Any]]
3347
3398
:keyword function_tool: A FunctionTool object representing the tool to be used.
3348
3399
:type function_tool: Optional[_models.FunctionTool]
3349
3400
:keyword toolset: A ToolSet object representing the set of tools to be used.
3350
3401
:type toolset: Optional[_models.ToolSet]
3402
+ :keyword max_retry: Maximum number of errors allowed and retry per run or stream. Default value is 10.
3403
+ :type max_retry: int
3351
3404
"""
3352
3405
if functions :
3353
3406
self ._function_tool = _models .FunctionTool (functions )
@@ -3357,6 +3410,8 @@ def enable_auto_function_calls(
3357
3410
tool = toolset .get_tool (_models .FunctionTool )
3358
3411
self ._function_tool = tool
3359
3412
3413
+ self ._function_tool_max_retry = max_retry
3414
+
3360
3415
3361
3416
__all__ : List [str ] = [
3362
3417
"AgentsOperations" ,
0 commit comments