
    D_ir5                         S r SSKJrJrJrJr  SSKJrJrJ	r	J
r
JrJrJr  SSKJrJr  SSKJr  SSKJrJr  SSKJrJr  \(       a  SSKJrJr  \" S	\\5      r\" S
5      r " S S\SS9r " S S\\\4   5      rg)z-Runnable that retries a Runnable if it fails.    )TYPE_CHECKINGAnyTypeVarcast)AsyncRetryingRetryCallState
RetryErrorRetryingretry_if_exception_typestop_after_attemptwait_exponential_jitter)	TypedDictoverride)RunnableBindingBase)RunnableConfigpatch_config)InputOutput)AsyncCallbackManagerForChainRunCallbackManagerForChainRunTUc                   H    \ rS rSr% Sr\\S'    \\S'    \\S'    \\S'   Srg)	ExponentialJitterParams#   z2Parameters for `tenacity.wait_exponential_jitter`.initialmaxexp_basejitter N)__name__
__module____qualname____firstlineno____doc__float__annotations____static_attributes__r        X/home/james-whalen/.local/lib/python3.13/site-packages/langchain_core/runnables/retry.pyr   r   #   s%    <N	JO'MHr)   r   F)totalc                     ^  \ rS rSr% Sr\4r\\\	   S4   \
S'    Sr\\
S'    Sr\S-  \
S'    S	r\\
S
'    \S\\\4   4S j5       rS\S\4S jrS\S\4S jr\S\SSS\S\4S j5       rS\\   S\S   S\S\\   4S jrS\ SSS\S\S\!4
U 4S jjr"\# S'S\ S\S-  S\S\!4S jj5       r$S\ SSS\S\S\!4
U 4S jjr%\# S'S\ S\S-  S\S\!4S jj5       r&S\\    S\S   S\\   S\S\\!\-     4
U 4S jjr'\# S'S S!.S\\    S\\\   -  S-  S"\S\S\\!   4
S# jjj5       r(S\\    S\S   S\\   S\S\\!\-     4
U 4S$ jjr)\# S'S S!.S\\    S\\\   -  S-  S"\S\S\\!   4
S% jjj5       r*S&r+U =r,$ )(RunnableRetry0   a  Retry a Runnable if it fails.

RunnableRetry can be used to add retry logic to any object
that subclasses the base Runnable.

Such retries are especially useful for network calls that may fail
due to transient errors.

The RunnableRetry is implemented as a RunnableBinding. The easiest
way to use it is through the `.with_retry()` method on all Runnables.

Example:
Here's an example that uses a RunnableLambda to raise an exception

    ```python
    import time


    def foo(input) -> None:
        '''Fake function that raises an exception.'''
        raise ValueError(f"Invoking foo failed. At time {time.time()}")


    runnable = RunnableLambda(foo)

    runnable_with_retries = runnable.with_retry(
        retry_if_exception_type=(ValueError,),  # Retry only on ValueError
        wait_exponential_jitter=True,  # Add jitter to the exponential backoff
        stop_after_attempt=2,  # Try twice
        exponential_jitter_params={"initial": 2},  # if desired, customize backoff
    )

    # The method invocation above is equivalent to the longer form below:

    runnable_with_retries = RunnableRetry(
        bound=runnable,
        retry_exception_types=(ValueError,),
        max_attempt_number=2,
        wait_exponential_jitter=True,
        exponential_jitter_params={"initial": 2},
    )
    ```

This logic can be used to retry any Runnable, including a chain of Runnables,
but in general it's best practice to keep the scope of the retry as small as
possible. For example, if you have a chain of Runnables, you should only retry
the Runnable that is likely to fail, not the entire chain.

Example:
    ```python
    from langchain_core.chat_models import ChatOpenAI
    from langchain_core.prompts import PromptTemplate

    template = PromptTemplate.from_template("tell me a joke about {topic}.")
    model = ChatOpenAI(temperature=0.5)

    # Good
    chain = template | model.with_retry()

    # Bad
    chain = template | model
    retryable_chain = chain.with_retry()
    ```
.retry_exception_typesTr   Nexponential_jitter_params   max_attempt_numberreturnc                    0 nU R                   (       a  [        U R                   5      US'   U R                  (       a!  [        S0 U R                  =(       d    0 D6US'   U R                  (       a  [        U R                  5      US'   U$ )Nstopwaitretryr    )r2   r   r   r0   r/   r   selfkwargss     r*   _kwargs_retryingRunnableRetry._kwargs_retrying   sr    !#""/0G0GHF6N''4 117RF6N %%5d6P6PQF7Or)   r:   c                 0    [        S0 U R                  DUD6$ Nr    )r
   r;   r8   s     r*   _sync_retryingRunnableRetry._sync_retrying   s    :$//:6::r)   c                 0    [        S0 U R                  DUD6$ r>   )r   r;   r8   s     r*   _async_retryingRunnableRetry._async_retrying   s    ?t44???r)   configrun_managerr   retry_statec                 d    UR                   nUS:  a  SU 3OS n[        XR                  U5      S9$ )N   zretry:attempt:)	callbacks)attempt_numberr   	get_child)rD   rE   rF   attempttags        r*   _patch_configRunnableRetry._patch_config   s9     ,,,3aKwi(TF.C.CC.HIIr)   c           	      l    [        XSS9 VVs/ s H  u  pEU R                  XEU5      PM     snn$ s  snnf )NF)strict)ziprN   )r9   rD   rE   rF   crms         r*   _patch_config_list RunnableRetry._patch_config_list   s?     V?
? qk2?
 	
 
s   0input_r   c           
        > U R                  SS9 H  nU   [        TU ]  " UU R                  X2UR                  5      40 UD6nS S S 5        UR                  R
                  (       d  MW  UR                  R
                  R                  (       a  M~  UR                  R                  W5        M     W$ ! , (       d  f       Ns= fNT)reraise)r?   superinvokerN   rF   outcomefailed
set_resultr9   rW   rE   rD   r:   rL   result	__class__s          r*   _invokeRunnableRetry._invoke   s     **4*8G&&vG<O<OP   ""***73F3F3N3N3U3U3U##..v6 9  s   -B00
B>	inputc                 >    U R                   " U R                  X40 UD6$ N)_call_with_configrc   r9   re   rD   r:   s       r*   r\   RunnableRetry.invoke   s      %%dllELVLLr)   r   c           
        >#    U R                  SS9  S h  vN nU   [        TU ]  " UU R                  X2UR                  5      40 UD6I S h  vN nS S S 5        UR                  R
                  (       d  Me  UR                  R
                  R                  (       a  M  UR                  R                  W5        M   N No! , (       d  f       Ns= f
 W$ 7frY   )rB   r[   ainvokerN   rF   r]   r^   r_   r`   s          r*   _ainvokeRunnableRetry._ainvoke   s      "11$1? 	7'$w&&vG<O<OP      ""***73F3F3N3N3U3U3U##..v6	7  @ sT   CCB<CC0C B>C !C8#CC<C>C  
C	

Cc                 Z   #    U R                   " U R                  X40 UD6I S h  vN $  N7frg   )_acall_with_configrm   ri   s       r*   rl   RunnableRetry.ainvoke   s)      ,,T]]ETVTTTTs   "+)+inputsc           
      j  > 0 n/ nUn U R                  5        GHb  nU   [        [        U5      5       V	s/ s H  oU;  d  M
  U	PM     n
n	U
(       d   S S S 5          GO#U
 V	s/ s H  oU	   PM	     nn	U
 V	s/ s H  oU	   PM	     nn	U
 V	s/ s H  oU	   PM	     nn	[        TU ]  " UU R                  XUR                  5      4SS0UD6nS n[        U5       H/  u  nn[        U[        5      (       a  U(       d  UnM&  X   nUUU'   M1     U(       a  Ue S S S 5        UR                  R                  (       d  GM  UR                  R                  R                  (       a  GMG  UR                  R                  U5        GMe      / n[        [        U5      5       H?  nUU;   a  UR                  UU   5        M  UR                  UR!                  S5      5        MA     U$ s  sn	f s  sn	f s  sn	f s  sn	f ! , (       d  f       N= f! [         a'  nXvL a  [        SU/[        U5      -  5      n S nANS nAff = fNreturn_exceptionsTzlist[Output]r   )r?   rangelenr[   batchrU   rF   	enumerate
isinstance	Exceptionr]   r^   r_   r	   r   appendpopr9   rr   rE   rD   r:   results_mapnot_setra   rL   iremaining_indicespending_inputspending_configspending_run_managersfirst_exceptionoffsetrorig_idxeoutputsidxrb   s                        r*   _batchRunnableRetry._batch   s,    *, "*	A..0 $)V#5)#5a+9M#5 & ) - W :K%K9JAQi9JN%K:K&L:KQay:KO&LDU+VDUqNDU(+V"W]&//+7CVCV
 +/ !F '+O%.v%6	%a33#223$#4#<01H- &7 '-- '? D ''///#//77>>>''226:M 1V -/V%Ck!{3/0vzz!}-	 &
 [)
 &L&L+V WL  	A nqcCK.?@	As   H G0	GG	
G0	H G0"G!0G06G&G0
G+A9G0!H 8#H   H G00
G>	:H 
H2H--H2F)ru   ru   c                B    U R                   " U R                  X4SU0UD6$ Nru   )_batch_with_configr   r9   rr   rD   ru   r:   s        r*   rx   RunnableRetry.batch"  s0     &&KK
;L
PV
 	
r)   c           
        >#    0 n/ nUn U R                  5         S h  vN nU   [        [        U5      5       V	s/ s H  oU;  d  M
  U	PM     n
n	U
(       d   S S S 5          GO(U
 V	s/ s H  oU	   PM	     nn	U
 V	s/ s H  oU	   PM	     nn	U
 V	s/ s H  oU	   PM	     nn	[        TU ]  " UU R                  XUR                  5      4SS0UD6I S h  vN nS n[        U5       H/  u  nn[        U[        5      (       a  U(       d  UnM&  X   nUUU'   M1     U(       a  Ue S S S 5        UR                  R                  (       d  GM,  UR                  R                  R                  (       a  GMT  UR                  R                  U5        GMr  / n[        [        U5      5       H?  nUU;   a  UR                  UU   5        M  UR                  UR!                  S5      5        MA     U$  GNs  sn	f s  sn	f s  sn	f s  sn	f  GN-! , (       d  f       N= f
 N! [         a'  nXvL a  [        SU/[        U5      -  5      n S nANS nAff = f7frt   )rB   rv   rw   r[   abatchrU   rF   ry   rz   r{   r]   r^   r_   r	   r   r|   r}   r~   s                        r*   _abatchRunnableRetry._abatch/  sA     *, ")	A!%!5!5!7 %;g $)V#5)#5a+9M#5 & ) - W :K%K9JAQi9JN%K:K&L:KQay:KO&LDU+VDUqNDU(+V#(7>&//+7CVCV$
 +/$ !$ F '+O%.v%6	%a33#223$#4#<01H- &7 '-- '= B ''///#//77>>>''226:
 -/V%Ck!{3/0vzz!}-	 &
 a%;)
 &L&L+V W "8L  	A nqcCK.?@	As   I	H HG(HH H	G+
G+
H	H #I	%H)G07H=G5HG:3HG?AH!!H #H /H AI	(H+H
H	H I	
II<I	II	c                ^   #    U R                   " U R                  X4SU0UD6I S h  vN $  N7fr   )_abatch_with_configr   r   s        r*   r   RunnableRetry.abatchm  s=      --LL&
<M
QW
 
 	
 
s   $-+-r    rg   )-r!   r"   r#   r$   r%   r{   r/   tupletypeBaseExceptionr'   r   boolr0   r   r2   intpropertydictstrr   r;   r
   r?   r   rB   staticmethodr   r   rN   listrU   r   r   rc   r   r\   rm   rl   r   rx   r   r   r(   __classcell__)rb   s   @r*   r-   r-   0   s   ?B ?H\5m!4c!9:I %)T(;@D6=D  ?$sCx.   ;s ;x ;@ @ @ JJJ $J 
	J J	
^$	
 #Y	
 $		

 
n		
 2 	
  
$ <@MM$2T$9MLOM	M M
 7 	
  
$ <@UU$2T$9ULOU	U U
=U= 67= ^$	=
 = 
fy 	!=~  @D


 #(

U

 n!55<


  

 

 
f

 

<U< ;<< ^$	<
 < 
fy 	!<|  @D


 #(

U

 n!55<


  

 

 
f

 

r)   r-   N) r%   typingr   r   r   r   tenacityr   r   r	   r
   r   r   r   typing_extensionsr   r   langchain_core.runnables.baser   langchain_core.runnables.configr   r   langchain_core.runnables.utilsr   r    langchain_core.callbacks.managerr   r   r   r   r   r-   r    r)   r*   <module>r      sz    3    2 = H 8
 	/1PQACL
Iiu 
IH
'v6 H
r)   