
    Z ja8                    x    S SK Jr  S SKrS SKrS SKJr  \" 5       (       a  S SKrOSrSS jrSS jr    S	S jr	g)
    )annotationsN)is_jmespath_availablec                  ^ / mU4S jn[         R                  " SX[         R                  S9n [         R                  " SSU 5      n [        T5       H/  u  p#U R	                  SU S3[
        R                  " U5      5      n M1     U $ )z[Convert Gemma4 tool call format (unquoted keys, ``<|"|>`` string delimiters) to valid JSON.c                h   > TR                  U R                  S5      5        S[        T5      S-
   S3$ )N    )appendgrouplen)mstringss    v/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/utils/chat_parsing_utils.py_capture&_gemma4_json_to_json.<locals>._capture!   s0    qwwqz"c'lQ&'t,,    z<\|"\|>(.*?)<\|"\|>flagsz(?<=[{,])(\w+):z"\1":r   )resubDOTALL	enumeratereplacejsondumps)textr   isr   s       @r   _gemma4_json_to_jsonr      ss    G-
 66((		JD66$h5D '"||d1#TNDJJqM: # Kr   c                x   U R                  5       (       a8  U R                  5       R                  5        VVs0 s H  u  pUc  M
  X_M     snn$ [        U R                  5       5      n[	        U5      S:  a  [        SU S35      e[	        U5      S:X  a  [        SU R                  S5       35      eUS   $ s  snnf )Nr   z+Regex has multiple unnamed groups!
Groups: 
r   zRegex has no capture groups:

)	groupdictitemslistgroupsr   
ValueErrorr
   )
node_matchkeyvalr$   s       r   _parse_re_matchr)   1   s    )3)=)=)?)E)E)G[)GXS3)G[[ j'')*v;?KF8SUVWW[A?
@P@PQR@S?TUVVay \s   	B6B6c           
        SU;   a  US   $ U c  gUR                  S5      nSU;   =(       d    SU;   =(       d    SU;   =(       d    SU;   nU(       a&  [        U [        5      (       d  [        SU  S	U 35      eUR                  S/ 5      nU H.  n[        R
                  " US
   US   U [        R                  S9n M0     UR                  S5      nUR                  S5      nUR                  S5      nUb6  [        R                  " X`[        R                  S9n	U	(       d  g[        U	5      n Ub[  US:w  a  [        SU SU 35      e[        R                  " Xp[        R                  S9 V	s/ s H  n	[        U	5      PM     n n	U (       d  gUb  US:w  a  [        SU SU 35      e0 n
[        R                  " X[        R                  S9 HJ  n	[        U	5      n[        U[        5      (       a  SU;  d  SU;  a  [        SU S	U 35      eUS   XS   '   ML     U
n U (       d  gSU;   a  US   nUS:X  a3  [        U [        5      (       d  [        SU  S	U 35      e[        U 5      n SnUS:X  a  [        U [        5      (       d  [        SU  S	U 35      eUR                  S0 5      nUR                  S5      nUR                  SS5      n [        R                  " U 5      nUb,  [         c  [#        S 5      e[         R                  " US   U5      nUn O[        S!U S"U 35      eUS:X  Ga  0 n[        U [        5      (       aP  S#U;  a  [        S$U  S	U 35      eUS#   R%                  5        H!  u  nn['        XS#   U   5      nUc  M  UUU'   M#     O[        U [        5      (       a  UR                  S#0 5      R%                  5        H@  u  nnSU;   a
  US   UU'   M  UU ;   a  ['        U U   U5      UU'   M0  S%U;   d  M8  US%   UU'   MB     UR                  S&S'5      nUSLaZ  [        U[        5      (       a  UO0 nU R%                  5        H-  u  nnUUR                  S#0 5      ;  d  M  ['        UU5      UU'   M/     O[        S(U  35      eUR                  S)/ 5      nU Vs/ s H  nUU;  d  M  UPM     nnU(       aB  [        U [        5      (       a  [)        U SS* 5      O
[)        U 5      n[        S+U S,U S-U 35      eU$ US:X  Ga&  U (       d  / $ / nS.U;   aL  [        U [*        5      (       d  [        S/U  35      eU  H!  nUR-                  ['        UUS.   5      5        M#     U$ S0U;   a  [        U [*        5      (       d$  [/        US0   5      S:X  a  U /n O[        S/U  35      e[/        U 5      [/        US0   5      :w  a,  [        S1[/        U 5       S2[/        US0   5       S3U  S	U 35      e[1        XS0   5       H!  u  nnUR-                  ['        UU5      5        M#     U$ [        S4U 35      eUS5;   Ga  US6:X  a\  [        U [2        5      (       a  U $ [        U [        5      (       d$  [        S7[5        U 5      R6                   S8U  35      e [3        U 5      $ US::X  ak  [        U [2        [8        45      (       a  [9        U 5      $ [        U [        5      (       d$  [        S;[5        U 5      R6                   S8U  35      e [9        U 5      $ US=:X  a  [        U [:        5      (       a  U $ [        U [        5      (       d$  [        S>[5        U 5      R6                   S8U  35      eU R=                  5       S?;   a  g'U R=                  5       S@;   a  g[        SAU  35      e[        U [        5      (       d$  [        SB[5        U 5      R6                   S8U  35      eU $ Ub  USC:X  a  U $ [        SDU SEU  35      es  sn	f ! [        R                   a%  nU(       a  U n SnAGN5[        SU  SU 35      eSnAff = fs  snf ! [         a    [        S9U < 35      ef = f! [         a    [        S<U < 35      ef = f)Fa  
This function takes content and a JSON schema which includes
regex extractors, and recursively parses the content. The output
should be a data structure matching the schema.

Args:
    node_content: The content corresponding to this node. Usually a string, but can be something else
                  if the parent node has multiple capture groups or named groups. In that case,
                  we generally pass the capture groups straight through to the children of this node
                  and don't do any parsing at this level.
    node_schema: The schema node controlling the parsing.

Returns:
    The parsed data structure for the current node.
constNtypezx-regexzx-regex-iteratorzx-regex-key-valuezx-regex-substitutionszXSchema node got a non-string input, but has a regex for parsing or substitution.
Input: z	
Schema: r   r   r   arrayzSchema node with type z& cannot use x-regex-iterator.
Schema: objectz' cannot use x-regex-key-value.
Schema: r'   valuezTRegex for x-regex-key-value must have named groups 'key' and 'value'.
Match groups: zx-parserzgemma4-tool-callz;Node has Gemma4 tool call parser but got non-string input: r   z/Node has JSON parser but got non-string input: zx-parser-args	transformallow_non_jsonFzNode has JSON parser but could not parse its contents as JSON. You can use the `allow_non_json` parser arg for nodes which may contain JSON or string content.

Content: z	

Error: zChat response schema includes a jmespath transformation, but jmespath is not installed. You can install it with `pip install jmespath`.zUnknown parser z for schema node: 
propertieszVObject node received string content but has no regex or parser to handle it.
Content: defaultadditionalPropertiesTz=Expected a dict or str for schema node with type object, got requiredi  zRequired fields z) are missing from parsed output.
Parsed: z
Input: r"   z>Expected a list or regex for schema node with type array, got prefixItemszArray node has z items, but schema only has z prefixItems defined.
Content: z?Array node has no items or prefixItems schema defined.
Schema: )stringintegernumberbooleanr8   z@Expected a string or int for schema node with type integer, got z: zVSchema node has type 'integer', but the parsed string content is not a valid integer: r9   zBExpected a string or number for schema node with type number, got zTSchema node has type 'number', but the parsed string content is not a valid number: r:   zAExpected a string or bool for schema node with type boolean, got )true1)false0zInvalid boolean value: z8Expected a string for schema node with type string, got anyzUnsupported schema type z for node: )get
isinstancestr	TypeErrorr   r   r   searchr)   finditerdictr%   r   r   loadsJSONDecodeErrorjmespathImportErrorr"   recursive_parsereprr#   r	   r   zipintr,   __name__floatboollower)node_contentnode_schema	node_type	has_regex	node_subsnode_sub
node_regexnode_regex_iteratornode_regex_to_dictr&   output_contentmatch_groupsparserparser_argsr0   r1   parsed_jsoneparsed_schemar'   
child_nodechild_node_contentadditional_schemar/   r5   missinginput_previewitemitem_schemas                                r   rK   rK   ?   s	   * +7##  'I[  	2,	2+-	2 #k1	  L#66"^ $"m%
 	
  7<Ivvhqk8A;BIIV +J%//*<=$)<=YYzryyI
&z2&4YK?fgrfstuu !kk*=SUS\S\]
]
 J'] 	 
 % 4YK?ghsgtuvv++&8biiXJ*:6LlD11U,5NRYamRm %%1N 3*m- 
 3?w2GN./ Y & [ Z(''lC00QR^Q__ijuivw  0=LFVlC00El^S]^i]jk  &///2>K#4I(__-=uEN"jj6 $#% b  'ook+.FT&Lvh6HVWW HlC(( ;.   ,~ .*m- 
 $/|#<#B#B#DZ%4\|C\]`Ca%b"%1);M#& $E d++#.??<#D#J#J#LZj()3G)<M#&L()8c9JJ)WM#&*,)3I)>M#& $M !,0F M -9CDUW[9\9\$5bd!"."4"4"6JC+//,"CC-<UDU-Vc* #7 [\h[ijkk??:r2"*G(3c.F3(G8B<QT8U8UDds!34[_`l[mM"7) ,(/ *'* 
 	g	Ik!lD11"`am`n opp$$$_T;w;O%PQ %  k)lD11{=12a7$0>L#&deqdr$stt< CM(B$CC %c,&7%88T;}567 8  ,~ .*m-  &)=7Q%R!k$$_T;%GH &S  _`k_lmnn	@	@	!,,,##lC00VW[\hWiWrWrVssu  wC  vD  E <((
 (",e55\**lC00XY]^jYkYtYtXuuw  yE  xF  G \**
 )#,--##lC00WX\]iXjXsXsWttv  xD  wE  F  !!#}4##%7 #:<.!IJJ lC00NtT`OaOjOjNkkmnzm{|   		i5029+[WXXq
Z '' !".K$ F  GS  FT  T_  `a  _b  c 	f H`   lmyl|}    jkwjz{ sH   (_'_, 
`((`(
`- 7
a
 ,`% 	` `  `%-a
a$)r   rB   returnrB   )r&   zre.Matchrj   z
dict | str)rS   zstr | list | dictrT   rF   )

__future__r   r   r   transformers.utilsr   rI   r   r)   rK    r   r   <module>rn      sK    #  	 4 H(rY#rYrYr   