@@ -431,6 +431,10 @@ async def _run_tool_loop(
431431 max_tool_rounds : int = 16 ,
432432 ** kwargs
433433 ) -> BaseModel | str :
434+ # ``max_retries`` is consumed by the instructor wrapper; on bare-client
435+ # paths it leaks to the SDK and raises ``TypeError``. Pop once and
436+ # re-attach only to the instructor (response_model) round-trip.
437+ max_retries = kwargs .pop ("max_retries" , None )
434438 requested_stream = bool (kwargs .get ("stream" , False ))
435439 if requested_stream and structured_model is None :
436440 # In stream mode we can't know upfront whether a tool will be called.
@@ -442,6 +446,8 @@ async def _run_tool_loop(
442446 stream = False
443447 if structured_model is not None and not tool_instances :
444448 kwargs ["response_model" ] = structured_model
449+ if max_retries is not None :
450+ kwargs ["max_retries" ] = max_retries
445451 else :
446452 if tool_instances :
447453 stream = kwargs .pop ("stream" , False )
@@ -470,6 +476,8 @@ async def _run_tool_loop(
470476
471477 if structured_model :
472478 kwargs ["response_model" ] = structured_model
479+ if max_retries is not None :
480+ kwargs ["max_retries" ] = max_retries
473481 output = await self .run_client (model_spec , messages_curr , stream = stream , ** kwargs )
474482 return output
475483
@@ -1290,6 +1298,7 @@ async def _run_tool_loop(
12901298 messages , structured_model , tool_instances , tool_contexts , model_spec , max_tool_rounds , ** kwargs
12911299 )
12921300
1301+ max_retries = kwargs .pop ("max_retries" , None )
12931302 requested_stream = bool (kwargs .get ("stream" , False ))
12941303 if requested_stream and structured_model is None :
12951304 kwargs .pop ("response_model" , None )
@@ -1304,6 +1313,8 @@ async def _run_tool_loop(
13041313 # can ask for the structured response in a single round-trip.
13051314 if structured_model is not None and not tool_instances and not has_inbuilt :
13061315 kwargs ["response_model" ] = structured_model
1316+ if max_retries is not None :
1317+ kwargs ["max_retries" ] = max_retries
13071318 else :
13081319 kwargs .pop ("response_model" , None )
13091320
@@ -1332,6 +1343,8 @@ async def _run_tool_loop(
13321343 if structured_model :
13331344 final_kwargs = dict (kwargs )
13341345 final_kwargs ["response_model" ] = structured_model
1346+ if max_retries is not None :
1347+ final_kwargs ["max_retries" ] = max_retries
13351348 response_id = getattr (output , "id" , None )
13361349 if response_id :
13371350 final_kwargs ["previous_response_id" ] = response_id
0 commit comments