1010
1111# TODO: temporarily disabling warnings to mute a pydantic warning from liteLLM
1212import warnings
13+ from functools import partial
1314from os import getenv
1415
1516warnings .filterwarnings ("ignore" , "Valid config keys have changed in V2" )
@@ -338,10 +339,16 @@ def identity(result):
338339 return identity
339340
340341
341- def set_error_to_scope_for_retry (scope : ScopeType , error , block_id : str ) -> ScopeType :
342+ def set_error_to_scope_for_retry (
343+ scope : ScopeType , error , block_id : Optional [str ] = ""
344+ ) -> ScopeType :
342345 repeating_same_error = False
343- if scope ["pdl_context" ] and isinstance (scope ["pdl_context" ], list ):
344- last_error = scope ["pdl_context" ][- 1 ]["content" ]
346+ pdl_context : Optional [LazyMessages ] = scope .get ("pdl_context" )
347+ if pdl_context is None :
348+ return scope
349+ if pdl_context and isinstance (pdl_context , list ):
350+ last_msg = pdl_context [- 1 ]
351+ last_error = last_msg ["content" ]
345352 if last_error .endswith (error ):
346353 repeating_same_error = True
347354 if repeating_same_error :
@@ -353,7 +360,7 @@ def set_error_to_scope_for_retry(scope: ScopeType, error, block_id: str) -> Scop
353360 }
354361 scope = scope | {
355362 "pdl_context" : lazy_messages_concat (
356- scope [ " pdl_context" ] ,
363+ pdl_context ,
357364 PdlList ([err_msg ]),
358365 )
359366 }
@@ -380,9 +387,15 @@ def process_advanced_block(
380387 state = state .with_yield_background (
381388 state .yield_background and context_in_contribute (block )
382389 )
390+
391+ # Bind result variables here with empty values
392+ result : PdlLazy [Any ] = PdlConst (None )
393+ background : LazyMessages = PdlList ([{}])
394+ new_scope : ScopeType = PdlDict ({})
395+ trace : AdvancedBlockType = EmptyBlock ()
396+
383397 max_retry = block .retry if block .retry else 0
384398 trial_total = max_retry + 1
385- background : LazyMessages = PdlList ([])
386399 for trial_idx in range (trial_total ):
387400 try :
388401 result , background , new_scope , trace = process_block_body (
@@ -394,32 +407,35 @@ def process_advanced_block(
394407 )
395408 trace = trace .model_copy (update = {"pdl__result" : result })
396409 if block .parser is not None :
397- parser = block .parser
398- result = lazy_apply (lambda r : parse_result (parser , r ), result )
410+ # Use partial to create a function with fixed arguments
411+ parser_func = partial (parse_result , block .parser )
412+ result = lazy_apply (parser_func , result )
399413 if init_state .yield_result and ContributeTarget .RESULT :
400414 yield_result (result , block .kind )
401415 if block .spec is not None and not isinstance (block , FunctionBlock ):
402- result = lazy_apply (
403- lambda r : result_with_type_checking (
404- r , block .spec , "Type errors during spec checking:" , loc , trace
405- ),
406- result ,
416+ # Use partial to create a function with fixed arguments
417+ checker = partial (
418+ result_with_type_checking ,
419+ spec = block .spec ,
420+ msg = "Type errors during spec checking:" ,
421+ loc = loc ,
422+ trace = trace ,
407423 )
424+ result = lazy_apply (checker , result )
408425 if block .fallback is not None :
409426 result .result ()
410427 break
411428 except Exception as exc :
429+ err_msg = exc .args [0 ]
412430 do_retry = (
413431 block .retry
414432 and trial_idx + 1 < trial_total
415- and "Keyboard Interrupt" not in exc . message
433+ and "Keyboard Interrupt" not in err_msg
416434 )
417435 if block .fallback is None and not do_retry :
418436 raise exc from exc
419- elif do_retry :
420- error = (
421- f"An error occurred in a PDL block. Error details: { exc .message } "
422- )
437+ if do_retry :
438+ error = f"An error occurred in a PDL block. Error details: { err_msg } "
423439 print (f"\n \033 [0;31m[Retry { trial_idx + 1 } /{ max_retry } ] { error } \033 [0m\n " )
424440 scope = set_error_to_scope_for_retry (scope , error , block .pdl__id )
425441 continue
@@ -437,12 +453,15 @@ def process_advanced_block(
437453 )
438454 if block .spec is not None and not isinstance (block , FunctionBlock ):
439455 loc = append (loc , "fallback" )
440- result = lazy_apply (
441- lambda r : result_with_type_checking (
442- r , block .spec , "Type errors during spec checking:" , loc , trace
443- ),
444- result ,
456+ # Use partial to create a function with fixed arguments
457+ checker = partial (
458+ result_with_type_checking ,
459+ spec = block .spec ,
460+ msg = "Type errors during spec checking:" ,
461+ loc = loc ,
462+ trace = trace ,
445463 )
464+ result = lazy_apply (checker , result )
446465 if block .def_ is not None :
447466 var = block .def_
448467 new_scope = new_scope | PdlDict ({var : result })
0 commit comments