+
    i7                         R t ^ RIt^ RIHtHt ^ RIHtHt ^ RIHt ^RI	H
t
 ^RI	Ht ^RI	Ht ^RI	Ht ]P                   ! R	4      t. ROt ! R R4      tR R ltR R lt ! R R
4      tR# )z)[Experimental] Text Only Local Tokenizer.N)AnyIterable)OptionalUnion)sentencepiece_model_pb2)_common)_local_tokenizer_loader)_transformers)typeszgoogle_genai.local_tokenizerLocalTokenizer_TextsAccumulatorc                     a  ] tR t^'t o RtV 3R lR ltV 3R lR ltV 3R lR ltV 3R lR	 ltV 3R
 lR lt	V 3R lR lt
V 3R lR ltV 3R lR ltV 3R lR ltV 3R lR ltV 3R lR ltV 3R lR ltV 3R lR ltRtV tR# )r   a  Accumulates countable texts from `Content` and `Tool` objects.

This class is responsible for traversing complex `Content` and `Tool`
objects and extracting all the text content that should be included when
calculating token counts.

A key feature of this class is its ability to detect unsupported fields in
`Content` objects. If a user provides a `Content` object with fields that
this local tokenizer doesn't recognize (e.g., new fields added in a future
API update), this class will log a warning.

The detection mechanism for `Content` objects works by recursively building
a "counted" version of the input object. This "counted" object only
contains the data that was successfully processed and added to the text
list for tokenization. After traversing the input, the original `Content`
object is compared to the "counted" object. If they don't match, it
signifies the presence of unsupported fields, and a warning is logged.
c                   < V ^8  d   QhRR/# )   returnN )format__classdict__s   "o/Users/igloo/.openclaw/workspace/scratch/fb_ad_env/lib/python3.14/site-packages/google/genai/local_tokenizer.py__annotate___TextsAccumulator.__annotate__;   s              c                    . V n         R # N_textsselfs   &r   __init___TextsAccumulator.__init__;   s	    DKr   c                0   < V ^8  d   QhRS[ S[,          /# )r   r   )r   str)r   r   s   "r   r   r   >   s      # r   c                    V P                   # r   r   r   s   &r   	get_texts_TextsAccumulator.get_texts>   s    ;;r   c                H   < V ^8  d   QhRS[ S[P                  ,          RR/# )r   contentsr   N)r   r
   Content)r   r   s   "r   r   r   A   s$        8EMM#:  t  r   c                :    V F  pV P                  V4       K  	  R # r   )add_content)r   r&   contents   && r   add_contents_TextsAccumulator.add_contentsA   s    
w r   c                8   < V ^8  d   QhRS[ P                  RR/# )r   r*   r   N)r
   r'   )r   r   s   "r   r   r   E   s       4 r   c                   \         P                  ! . VP                  R 7      pVP                  '       EdT   VP                   EFB  pVP                  f   Q h\         P                  ! 4       pVP
                  f   VP                  e   \        R4      hVP                  e   VP                  Vn        VP                  e-   V P                  VP                  4       VP                  Vn	        VP                  e-   V P                  VP                  4       VP                  Vn        VP                  e7   VP                  Vn        V P                  P                  VP                  4       VP                  P                  V4       EKE  	  VP!                  RR7      VP!                  RR7      8w  d   \"        P%                  RV RV R24       R# R# ))partsroleNz6LocalTokenizers do not support non-text content types.T)exclude_nonezHContent contains unsupported types for token counting. Supported fields z. Got .)r
   r'   r0   r/   Part	file_datainline_data
ValueErrorvideo_metadatafunction_calladd_function_callfunction_responseadd_function_responsetextr   append
model_dumploggerwarning)r   r*   counted_contentpartcounted_parts   &&   r   r)   _TextsAccumulator.add_contentE   s   mm"7<<@O}}}--$$$000zz|>>%)9)9)EF  *(,(;(;,
%)

 
 !3!3
4'+'9'9,
$!!-

$
$T%;%;
<+/+A+A,
(99 "ii,

++

TYY
'$$\2%  ( t,0J0J 1K 1  nn$%VG9A7r   c                8   < V ^8  d   QhRS[ P                  RR/# )r   r8   r   N)r
   FunctionCall)r   r   s   "r   r   r   d   s!     0 0U-?-? 0D 0r   c                    VP                   '       d&   V P                  P                  VP                   4       \        P                  ! VP                   R7      pVP
                  '       d$   V P                  VP
                  4      pW2n        R# R# )z}Processes a function call and adds relevant text to the accumulator.

Args:
    function_call: The function call to process.
)nameN)rH   r   r=   r
   rF   args_dict_traverse)r   r8   counted_function_callcounted_argss   &&  r   r9   #_TextsAccumulator.add_function_calld   sl     
kk++,!..M4F4FG((););<l#/  r   c                N   < V ^8  d   QhRS[ P                  RS[ P                  /# )r   toolr   )r
   Tool)r   r   s   "r   r   r   q   s#      5:: %** r   c                    \         P                  ! . R 7      pVP                  '       dU   VP                   FD  pV P                  V4      pVP                  f   . Vn        VP                  P	                  V4       KF  	  V# ))function_declarations)r
   rP   rR   _function_declaration_traverser=   )r   rO   counted_toolfunction_declarationcounted_function_declarations   &&   r   add_tool_TextsAccumulator.add_toolq   st    ::B7L!!!"&"<"<
'+'J'J (
$ --5/1,
,**112NO #= r   c                H   < V ^8  d   QhRS[ S[P                  ,          RR/# )r   toolsr   N)r   r
   rP   )r   r   s   "r   r   r   ~   s$      Xejj1 d r   c                :    V F  pV P                  V4       K  	  R # r   )rW   )r   rZ   rO   s   && r   	add_tools_TextsAccumulator.add_tools~   s    
mmD r   c                H   < V ^8  d   QhRS[ S[P                  ,          RR/# )r   function_responsesr   N)r   r
   FunctionResponse)r   r   s   "r   r   r      s(     4 4 ()?)? @44r   c                :    V F  pV P                  V4       K  	  R # r   )r;   )r   r_   r:   s   && r   add_function_responses(_TextsAccumulator.add_function_responses   s     0
  !23 0r   c                8   < V ^8  d   QhRS[ P                  RR/# )r   r:   r   N)r
   r`   )r   r   s   "r   r   r      s#     	< 	<$55	<	<r   c                *   \         P                  ! 4       pVP                  '       d7   V P                  P	                  VP                  4       VP                  Vn        VP
                  '       d$   V P                  VP
                  4      pW2n        R # R # r   )r
   r`   rH   r   r=   responserJ   )r   r:   counted_function_responsecounted_responses   &&  r   r;   '_TextsAccumulator.add_function_response   sv     !& 6 6 8
kk*//0'8'='=$!!!,,->-G-GH+;( "r   c                N   < V ^8  d   QhRS[ P                  RS[ P                  /# )r   rU   r   )r
   FunctionDeclaration)r   r   s   "r   r   r      s)     ( ("'";";(  (r   c                   \         P                  ! 4       pVP                  '       d7   V P                  P	                  VP                  4       VP                  Vn        VP
                  '       d7   V P                  P	                  VP
                  4       VP
                  Vn        VP                  '       d"   V P                  VP                  4      pW2n        VP                  '       d"   V P                  VP                  4      pWBn        V# r   )	r
   rk   rH   r   r=   description
parameters
add_schemarf   )r   rU   rV   counted_parametersrh   s   &&   r   rS   0_TextsAccumulator._function_declaration_traverse   s     $)#<#<#>    
kk-223*>*C*C"''''
kk-99:

*
* #. &&&??+?+J+JK0B-$$$)=)F)FG.>+''r   c                N   < V ^8  d   QhRS[ P                  RS[ P                  /# )r   schemar   )r
   Schema)r   r   s   "r   r   r      s#     + +u|| + +r   c                &   \         P                  ! 4       pVP                  '       d   VP                  Vn        VP                  '       d   VP                  Vn        VP                  e   VP                  Vn        VP
                  '       d7   V P                  P                  VP
                  4       VP
                  Vn        VP                  '       d7   V P                  P                  VP                  4       VP                  Vn        VP                  '       d7   V P                  P                  VP                  4       VP                  Vn	        VP                  '       d7   V P                  P                  VP                  4       VP                  Vn        VP                  '       d   VP                  Vn        VP                  '       d"   V P                  VP                  4      pW2n        VP                  '       d\   / pVP                  P                  4        F5  w  rVV P                  P                  V4       V P                  V4      pWtV&   K7  	  WBn        VP                   '       d"   V P#                  VP                   4      pWn        V# )zProcesses a schema and adds relevant text to the accumulator.

Args:
    schema: The schema to process.

Returns:
    The new schema object with only countable fields.
)r
   rt   typetitledefaultr   r   r=   rm   enumextendrequiredproperty_orderingitemsro   
propertiesexample_any_traverse)	r   rs   counted_schemacounted_schema_itemsdkeyvaluecounted_valuecounted_schema_examples	   &&       r   ro   _TextsAccumulator.add_schema   s    \\^N{{{"KKn|||#\\n~~!%~~n}}}
kk'$mmn
kk++,#)#5#5n {{{
kk%"KKn
kk) &n)/)A)An&|||!__V\\:1
a))//1*#3.# 2 #$~~~#11&..A5r   c                R   < V ^8  d   QhRS[ S[S[3,          RS[ S[S[3,          /# )r   r   r   )dictr!   r   )r   r   s   "r   r   r      s+      d38n c3h r   c                    / pV P                   P                  \        VP                  4       4      4       VP	                  4        F  w  r4V P                  V4      W#&   K  	  V# )zProcesses a dict and adds relevant text to the accumulator.

Args:
    d: The dict to process.

Returns:
    The new dict object with only countable fields.
)r   rz   listkeysr}   r   )r   r   counted_dictr   vals   &&   r   rJ    _TextsAccumulator._dict_traverse   sP     LKKtAFFH~&GGI,,S1l r   c                &   < V ^8  d   QhRS[ RS[ /# )r   r   r   )r   )r   r   s   "r   r   r      s        r   c                0   \        V\        4      '       d   V P                  P                  V4       V# \        V\        4      '       d   V P                  V4      # \        V\        4      '       d!   V Uu. uF  q P                  V4      NK  	  up# V# u upi )zProcesses a value and adds relevant text to the accumulator.

Args:
    value: The value to process.

Returns:
    The new value with only countable fields.
)
isinstancer!   r   r=   r   rJ   r   r   )r   r   items   && r   r   _TextsAccumulator._any_traverse   s{     %
kkl	E4	 	   ''	E4	 	 38954  &599l :s   5Br   N)__name__
__module____qualname____firstlineno____doc__r   r#   r+   r)   r9   rW   r\   rb   r;   rS   ro   rJ   r   __static_attributes____classdictcell__r   s   @r   r   r   '   s     &        >0 0  4 4	< 	<( ((+ +Z  r   c                x    V ^8  d   QhR\         R\        P                  P                  P                  R\
        /# )r   tokenrv   r   )r!   r   
ModelProtoSentencePieceTypebytes)r   s   "r   r   r      s6     5 55-88FFKK5
5r   c                     V\         P                  P                  P                  P                  8X  d   \        V 4      P                  ^RR7      # V P                  RR4      P                  R4      # )   big)length	byteorderu   ▁ zutf-8)	r   r   r   r   BYTE_parse_hex_byteto_bytesreplaceencode)r   rv   s   &&r   _token_str_to_bytesr      s\     
$//==BBGGG5!**!u*EE==$++G44r   c                0    V ^8  d   QhR\         R\        /# )r   r   r   )r!   int)r   s   "r   r   r      s      3 3 r   c                F   \        V 4      ^8w  d   \        RV  24      hV P                  R4      '       d   V P                  R4      '       g   \        RV  24      h \	        V R,          ^4      pTR8  d   \        RT  24      hT#   \         d    \        RT  24      hi ; i)	zParses a hex byte string of the form '<0xXX>' and returns the integer value.

Raises ValueError if the input is malformed or the byte value is invalid.
zInvalid byte length: z<0x>zInvalid byte format: :      NzInvalid hex value:    zByte value out of range: )lenr6   
startswithendswithr   )r   r   s   & r   r   r      s     	Z1_
,UG4
55			%	 	 s(;(;
,UG4
554
eCj"
C 	CZ
08
99	* 
 4
*5'2
334s   B B c                      a  ] tR tRt o RtV 3R lR lt]P                  ! R4      RR/V 3R lR	 ll4       t]P                  ! R4      V 3R
 lR l4       t	Rt
V tR# )r   i  a  [Experimental] Text Only Local Tokenizer.

This class provides a local tokenizer for text only token counting.

LIMITATIONS:
- Only supports text based tokenization and no multimodal tokenization.
- Forward compatibility depends on the open-source tokenizer models for future
Gemini versions.
- For token counting of tools and response schemas, the `LocalTokenizer` only
supports `types.Tool` and `types.Schema` objects. Python functions or Pydantic
models cannot be passed directly.
c                    < V ^8  d   QhRS[ /# )r   
model_name)r!   )r   r   s   "r   r   LocalTokenizer.__annotate__$  s     E E Er   c                    \         P                  ! V4      V n        \         P                  ! V P                  4      V n        \         P
                  ! V P                  4      V n        R # r   )loaderget_tokenizer_name_tokenizer_nameload_model_proto_model_protoget_sentencepiece
_tokenizer)r   r   s   &&r   r   LocalTokenizer.__init__$  sF    !44Z@D//0D0DED..t/C/CDDOr   zThe SDK's local tokenizer implementation is experimental and may change in the future. It only supports text based tokenization.configNc                   < V ^8  d   QhRS[ S[P                  S[P                  3,          RS[S[P
                  ,          RS[P                  /# )r   r&   r   r   )r   r
   ContentListUnionContentListUnionDictr   CountTokensConfigOrDictCountTokensResult)r   r   s   "r   r   r   -  sP     & &e,,e.H.HHI& u445	&
 &r   c                  \         P                  ! V4      p\        4       p\        P                  P                  T;'       g    / 4      pVP                  V4       VP                  '       d   VP                  VP                  4       VP                  '       dB   VP                  P                  '       d&   VP                  VP                  P                  4       VP                  '       d1   VP                  \         P                  ! VP                  .4      4       V P                  P                  \        VP!                  4       4      4      p\        P"                  ! \%        R V 4       4      R7      # )a  Counts the number of tokens in a given text.

Args:
  contents: The contents to tokenize.
  config: The configuration for counting tokens.

Returns:
  A `CountTokensResult` containing the total number of tokens.

Usage:

.. code-block:: python

  from google import genai
  tokenizer = genai.LocalTokenizer(model_name='gemini-2.0-flash-001')
  result = tokenizer.count_tokens("What is your name?")
  print(result)
  # total_tokens=5
c              3   8   "   T F  p\        V4      x  K  	  R # 5ir   )r   ).0tokenss   & r   	<genexpr>.LocalTokenizer.count_tokens.<locals>.<genexpr>R  s     ?;V;s   )total_tokens)t
t_contentsr   r
   CountTokensConfigmodel_validater+   rZ   r\   generation_configresponse_schemaro   system_instructionr   r   r   r#   r   sum)r   r&   r   processed_contentstext_accumulatortokens_lists   &&$   r   count_tokensLocalTokenizer.count_tokens)  s	   : h/(*$$33FLLbAF!!"45|||  .F$<$<$L$L$L!!&":":"J"JK   ##ALL&2K2K1L$MN//((.>.H.H.J)KLK""?;?? r   c                x   < V ^8  d   QhRS[ S[P                  S[P                  3,          RS[P                  /# )r   r&   r   )r   r
   r   r   ComputeTokensResult)r   r   s   "r   r   r   Y  s;     2> 2>e,,e.H.HHI2>   2>r   c                    \         P                  ! V4      p\        4       pV F  pVP                  V4       K  	  V P                  P                  VP                  4       4      p. pV FE  pVP                  '       g   K  VP                   F  pVP                  VP                  4       K   	  KG  	  . p\        WV4       F  w  rTP                  \        P                  ! V	P                   Uu. uF  qP                  NK  	  upV	P                   Uu. uFH  p\        VP                   V P"                  P                  VP                  ,          P$                  4      NKJ  	  upV
R7      4       K  	  \        P&                  ! VR7      # u upi u upi )a  Computes the tokens ids and string pieces in the input.

Args:
  contents: The contents to tokenize.

Returns:
  A `ComputeTokensResult` containing the token information.

Usage:

.. code-block:: python

  from google import genai
  tokenizer = genai.LocalTokenizer(model_name='gemini-2.0-flash-001')
  result = tokenizer.compute_tokens("What is your name?")
  print(result)
  # tokens_info=[TokensInfo(token_ids=[279, 329, 1313, 2508, 13], tokens=[b' What', b' is', b' your', b' name', b'?'], role='user')]
)	token_idsr   r0   )tokens_info)r   r   r   r)   r   EncodeAsImmutableProtor#   r/   r=   r0   zipr
   
TokensInfopiecesidr   piecer   rv   r   )r   r&   r   r   r*   tokens_protosroles_token_infostokens_protor0   r   s   &&          r   compute_tokensLocalTokenizer.compute_tokensU  sP   4 h/(*%""7+ &OO::""$M E%	A
,,w||
$  &
 K!-7


/;/B/BC/Be/BC
  ,22	  3e &kk4#4#4#;#;EHH#E#J#J  3	 	 8 $$== Ds   ,FAF)r   r   r   )r   r   r   r   r   r   r   experimental_warningr   r   r   r   r   s   @r   r   r     sp     E E
 B& 9=	& &	&P B2>	2>r   )r   r   r   r   )r   loggingtypingr   r   r   r   sentencepiecer    r   r   r   r	   r   r
   	getLoggerr?   __all__r   r   r   r   r   r   r   <module>r      s]     0    " 1  /   			9	:M M`5,u> u>r   