
    Cj#                         S SK r S SKrS SKJrJrJrJrJrJrJ	r	J
r
JrJr  S SKJr  S SKrS SKJr  S SKJr  S SKJrJrJrJr  S SKJr  S SKJrJrJrJr   " S	 S
5      r  " S S5      r!g)    N)
AnyDictIterableListOptionalTypeUnioncastget_type_hintsoverload)	BaseModel)verbose_logger)BaseResponsesAPIConfig)ResponseAPIUsage!ResponsesAPIOptionalRequestParamsResponsesAPIResponseResponseText)DecodedResponseId)CompletionTokensDetailsWrapperPromptTokensDetailsWrapperSpecialEnumsUsagec                       \ rS rSrSr\S\\\      S\	S\\
   S\\   S\4
S j5       r\ SCS\S
\S\S\\\      S\	4
S jj5       r\S\	\\4   S\4S j5       r\\ SCS\S\\   S\\	\\4      S\4S jj5       5       r\\ SCS\	\\4   S\\   S\\	\\4      S\	\\4   4S jj5       5       r\ SCS\\\	\\4   4   S\\   S\\	\\4      S\\\	\\4   4   4S jj5       r\S\S\S\4S j5       r\S\S\\	\\4      4S j5       r\S\S\S\4S j5       r\S\S\\\   \4   4S j5       r\S\S \	\\4   4   S\\   S\S \	\\4   4   4S! j5       r\S"\S\4S# j5       r\S\\   S\\   S$\S\4S% j5       r\S$\S\4S& j5       r\S$\\   S\\   4S' j5       r\S(\S\4S) j5       r \S\\   S\\   S*\S\4S+ j5       r!\S*\S\4S, j5       r"\S*\S\4S- j5       r#\S.\S\\   S\\   SS	4S/ j5       r$\S0\S\\   S\\   SS	4S1 j5       r%\S2\S\\   S\\   SS	4S3 j5       r&\S.\S4\'\   SS	4S5 j5       r(\S0\S4\'\   SS	4S6 j5       r)\S2\S4\'\   SS	4S7 j5       r*\S\S\+\   4S8 j5       r,\ SCS\\\	\\4   4   S\\   S\\	\\4      S\\\	\\4   4   4S9 jj5       r-\ SCS:\\\.S;   \/4      S<\S=   S\S=   4S> jj5       r0\S?\\	\\4      S@\\1\      S\\\   \\	\\	\\4   4      \\	\\4      \\	\\4      4   4SA j5       r2SBr3g	)DResponsesAPIRequestUtils$   z2Helper utils for constructing ResponseAPI requestssupported_paramsnon_default_paramsdrop_paramscustom_llm_providermodelc           	          U c  g 0 nUR                  5        H  nX`;  d  M
  X   XV'   M     U(       a:  [        R                  SL d  Ub  USL a  g [        R                  " SU SU SU S3S9eg )NTi  z does not support parameters: z, for model=zf. To drop these, set `litellm.drop_params=True` or for proxy:

`litellm_settings:
 drop_params: true`
)status_codemessage)keyslitellmr   UnsupportedParamsError)r   r   r   r   r    unsupported_paramsks          i/home/rurouni/.local/share/pipx/venvs/strix-agent/lib/python3.13/site-packages/litellm/responses/utils.py_check_valid_arg)ResponsesAPIRequestUtils._check_valid_arg'   s     ##((*A((:(="% + ""d*'K4,?44 #233QRdQeeqrwqx  yc  d      Nresponses_api_provider_configresponse_api_optional_paramsallowed_openai_paramsreturnc                 (   SSK Jn  UR                  U 5      n[        [        U5      n[
        R                  XS=(       d    / -   U[        R                  UR                  U S9  UR                  UU [        R                  S9nU" UUU=(       d    / S9nU$ )a  
Get optional parameters for the responses API.

Args:
    params: Dictionary of all parameters
    model: The model name
    responses_api_provider_config: The provider configuration for responses API

Returns:
    A dictionary of supported parameters for the responses API
r   )_apply_openai_param_overrides)r   r   r   r   r    )r.   r    r   )optional_paramsr   r/   )litellm.utilsr2   get_supported_openai_paramsr
   r   r   r*   r%   r   r   map_openai_params)r    r-   r.   r/   r2   r   r   mapped_paramss           r)   !get_optional_params_responses_api:ResponsesAPIRequestUtils.get_optional_params_responses_api@   s    $ 	@ 9TT
 "$(DE 11-1L"M1++ = Q Q 	2 	
 6GG)E++ H 
 6)1"7"=2
 r,   paramsc                    SSK Jn  [        [        5      R	                  5       nU R                  SS5      nU R                  S0 5      nU R                  SS5      nUR                  U UUUU Vs0 s H  ofS_M     snS/S9nS	U;   a  [        R                  US	   5      nXS	'   S
U;   a+  SSK J	n	  U	" US
   5      n
U
b  XS
'   OUR                  S
S5        [        [        U5      $ s  snf )z
Filter parameters to only include those defined in ResponsesAPIOptionalRequestParams.

Args:
    params: Dictionary of parameters to filter

Returns:
    ResponsesAPIOptionalRequestParams instance with only the valid parameters
r   )PreProcessNonDefaultParamsr   Nkwargsadditional_drop_paramsinput)passed_paramsspecial_paramsr   r>   default_param_values#additional_endpoint_specific_paramsprevious_response_idmetadata)add_openai_metadata)r4   r<   r   r   r$   pop#base_pre_process_non_default_paramsr   <decode_previous_response_id_to_original_previous_response_idrF   r
   )r:   r<   
valid_keysr   rA   r>   r(   r   decoded_previous_response_idrF   converted_metadatas              r)   )get_requested_response_api_optional_paramBResponsesAPIRequestUtils.get_requested_response_api_optional_paramt   s    	=#$EFKKM
$jj)>EHb1!',Dd!K&JJ$-$7'=7A%Bz!gz%B5<I K  	 "%77+C  ,A  ,A"#9:,( :V56++9!45G
5S!T!-1C:."&&z4857IJJ+ &Cs   -C#responses_api_responselitellm_metadatac                     g N rO   r   rP   s      r)   /_update_responses_api_response_id_with_model_idHResponsesAPIRequestUtils._update_responses_api_response_id_with_model_id        	r,   c                     g rR   rS   rT   s      r)   rU   rV      rW   r,   c                    U=(       d    0 nUR                  S0 5      =(       d    0 nUR                  S5      n[        U [        5      (       a  U R                  S5      nO[        U SS5      nUc  U $ [        R                  UUUS9n[        U [        5      (       a  X`S'   OX`l        UR                  S5      (       a  [        R                  U US9n [        R                  U UUS9n U $ )zUpdate the responses_api_response_id with model_id and custom_llm_provider.

Handles both ``ResponsesAPIResponse`` objects and plain dictionaries returned
by some streaming providers.

model_infoidN)model_idr   response_id"encrypted_content_affinity_enabled)responser\   rT   )	get
isinstancedictgetattrr    _build_responses_api_response_idr[   ._update_encrypted_content_item_ids_in_response!_update_container_ids_in_response)rO   r   rP   rZ   r\   r]   
updated_ids          r)   rU   rV      s    ,1r%5%9%9,%K%Qr
>>$' ,d33044T:K!"8$EK ))-NN 3# O 

 ,d33+54((2% DEE(WW3% X  # %FF'=$7!1 G  	 &%r,   r\   item_idc                     SU  SU 3n[         R                  " UR                  S5      5      R                  S5      nSU 3$ )zEncode model_id into an output item ID for encrypted-content items.

Format: ``encitem_{base64("litellm:model_id:{model_id};item_id:{original_id}")}``
litellm:model_id:z	;item_id:utf-8encitem_base64	b64encodeencodedecode)r\   rh   	assembledencodeds       r)   _build_encrypted_item_id1ResponsesAPIRequestUtils._build_encrypted_item_id   sI     (z7)D	""9#3#3G#<=DDWM'##r,   
encoded_idc                    U R                  S5      (       d  g U [        S5      S n[        U5      S-  nU(       a  USSU-
  -  -  n[        R                  " UR	                  S5      5      R                  S5      nUR                  SS5      n[        U5      S:  a  gUS	   R                  S
S5      nUS   R                  SS5      nXVS.$ ! [         a     gf = f)zDecode a litellm-encoded encrypted-content item ID.

Returns a dict with ``model_id`` and ``item_id`` keys, or ``None`` if
the string is not a litellm-encoded item ID.
rl   N   =rk   ;      r   rj    zitem_id:)r\   rh   )	
startswithlenrn   	b64decoderp   rq   splitreplace	Exception)rv   cleanedmissingdecodedpartsr\   rh   s          r)   _decode_encrypted_item_id2ResponsesAPIRequestUtils._decode_encrypted_item_id   s     $$Z00	 Z!23G'lQ&G3!g+..&&w~~g'>?FFwOGMM#q)E5zA~Qx''(;R@HAh&&z26G (== 		s   BC -C 
CCencrypted_contentc                     SU 3n[         R                  " UR                  S5      5      R                  S5      nSU SU  3$ )a'  Wrap encrypted_content with model_id metadata for affinity routing.

When Codex or other clients send items with encrypted_content but no ID,
we encode the model_id directly into the encrypted_content itself.

Format: ``litellm_enc:{base64("model_id:{model_id}")};{original_encrypted_content}``
	model_id:rk   litellm_enc:rz   rm   )r   r\   rE   encoded_metadatas       r)   %_wrap_encrypted_content_with_model_id>ResponsesAPIRequestUtils._wrap_encrypted_content_with_model_id  sL     xj)!++HOOG,DELLWU./q1B0CDDr,   wrapped_contentc                    U R                  S5      (       d  SU 4$  U R                  SS5      n[        U5      S:  a  SU 4$ US   R                  SS5      nUS   n[        U5      S-  nU(       a  US	SU-
  -  -  n[        R
                  " UR                  S
5      5      R                  S
5      nUR                  SS5      nXc4$ ! [         a    SU 4s $ f = f)zUnwrap encrypted_content to extract model_id and original content.

Returns:
    Tuple of (model_id, original_encrypted_content).
    If not wrapped, returns (None, original_content).
r   Nrz   r{   r|   r   r}   rx   ry   rk   r   )	r~   r   r   r   rn   r   rp   rq   r   )r   r   metadata_b64original_contentr   decoded_metadatar\   s          r)   '_unwrap_encrypted_content_with_model_id@ResponsesAPIRequestUtils._unwrap_encrypted_content_with_model_id$  s     )).99((	)#))#q1E5zA~_,, 8++NB?L$Qx ,'!+Gq7{ 33%//0C0CG0LMTT  (//R@H-- 	)((	)s   $C BC CCr_   r   c                    U(       d  U $ Sn[        U [        5      (       a  U R                  S5      nO[        U SS5      n[        U[        5      (       d  U $ U GH?  n[        U[        5      (       a  UR                  S5      nUR                  S5      nU(       ai  [        U[
        5      (       aR  [        R                  XQ5      US'   U(       a1  [        U[
        5      (       a  [        R                  X5      US'   M  M  M  M  M  [        USS5      n[        USS5      nU(       d  M  [        U[
        5      (       d  M   [        R                  XQ5      Ul	        U(       d  GM  [        U[
        5      (       d  GM$   [        R                  X5      Ul        GMB     U $ ! [         a     NPf = f! [         a     GMd  f = f)a=  Rewrite item IDs for output items that contain ``encrypted_content``.

Encodes ``model_id`` into the item ID so that follow-up requests can be
routed back to the originating deployment without any cache lookup.

For items without an ID (e.g., from Codex), encodes model_id directly
into the encrypted_content itself.
Noutputr[   r   )ra   rb   r`   rc   liststrr   r   rt   r   AttributeErrorr[   )r_   r\   r   itemrh   r   s         r)   re   GResponsesAPIRequestUtils._update_encrypted_content_item_ids_in_responseG  s    O!%h%%\\(+FXx6F&$''OD$%%((4.$(HH-@$A!$4Es)K)K 1VV- ,- :gs#;#;%=%V%V$&T
 $<w *L$ "$d3$+D2Et$L!$$4Es)K)K1I1o1o-2. w:gs#;#;!&>&W&W ('DGA L  *   . ! !s$   F>F/
F,+F,/
F>=F>request_inputc                    [        U [        5      (       d  U $ U  H  n[        U[        5      (       d  M  UR                  S5      nU(       a9  [        U[        5      (       a$  [
        R                  U5      nU(       a  US   US'   UR                  S5      nU(       d  M  [        U[        5      (       d  M  [
        R                  U5      u  nnXd:w  d  M  XaS'   M     U $ )aG  Decode litellm-encoded item IDs in request input back to original IDs.

Called before forwarding the request to the upstream provider so the
provider receives the original item IDs and unwrapped encrypted_content.

Handles both:
1. Items with encoded IDs (encitem_...)
2. Items with wrapped encrypted_content (litellm_enc:...)
r[   rh   r   )ra   r   rb   r`   r   r   r   r   )r   r   rh   r   r   _	unwrappeds          r)   ,_restore_encrypted_content_item_ids_in_inputEResponsesAPIRequestUtils._restore_encrypted_content_item_ids_in_input  s     -..  !D$%%((4.z'3776PPG %,Y%7T
$(HH-@$A!$$4Es)K)K 1XX)! !54=01' "* r,   r]   c                     [        [        R                  R                  5      R	                  XU5      n[
        R                  " UR                  S5      5      R                  S5      nSU 3$ )z#Build the responses_api_response_idrk   resp_)	r   r   %LITELLM_MANAGED_RESPONSE_COMPLETE_STRvalueformatrn   ro   rp   rq   )r   r\   r]   assembled_idbase64_encoded_ids        r)   rd   9ResponsesAPIRequestUtils._build_responses_api_response_id  sj      >>DD

&$
< 	 "(!1!1,2E2Eg2N!O!V!V"
 ()**r,   c                     U R                  SS5      n[        R                  " UR                  S5      5      R	                  S5      nSU;  a  [        SSU S9$ UR                  S5      nSnSn[        U5      S:  aF  US   nUS	   nUS
   nUR                  SS5      nUR                  SS5      nUR                  SS5      n	OU n	[        UUU	S9$ ! [         a1  n
[        R                  " SU  SU
 35        [        SSU S9s Sn
A
$ Sn
A
ff = f)z
Decode the responses_api_response_id

Returns:
    DecodedResponseId: Structured tuple with custom_llm_provider, model_id, and response_id
r   r}   rk   rz   Nr   r\   r]      r   r{   r|   litellm:custom_llm_provider:r   zresponse_id:zError decoding response_id '': )r   rn   r   rp   rq   r   r   r   r   r   debug)r]   
cleaned_id
decoded_idr   r   r\   custom_llm_provider_partmodel_id_partresponse_partdecoded_response_ides              r)   !_decode_responses_api_response_id:ResponsesAPIRequestUtils._decode_responses_api_response_id  sE   -	$,,Wb9J))**;*;G*DELLWUJ *$((,! +  $$S)E #'H E
a+08( %a %a&>&F&F2B'# )00bA&3&;&;NB&O#&1#$$7!/ 
  	  #?}CPQs!ST$$(' 	s%   AC A6C 
D&D DDc                 h    U c  g[         R                  U 5      nUR                  S5      =(       d    S$ )z%Get the model_id from the response_idNr\   r   r   r`   )r]   r   s     r)   get_model_id_from_response_id6ResponsesAPIRequestUtils.get_model_id_from_response_id  s8     $FF{S 	 #&&z2:d:r,   rD   c                 P    [         R                  U 5      nUR                  SU 5      $ )a  
Decode the previous_response_id to the original previous_response_id

Why?
    - LiteLLM encodes the `custom_llm_provider` and `model_id` into the `previous_response_id` this helps with maintaining session consistency when load balancing multiple deployments of the same model.
    - We cannot send the litellm encoded b64 to the upstream llm api, hence we decode it to the original `previous_response_id`

Args:
    previous_response_id: The previous_response_id to decode

Returns:
    The original previous_response_id
r]   r   )rD   r   s     r)   rI   UResponsesAPIRequestUtils.decode_previous_response_id_to_original_previous_response_id  s0    $ %FF$ 	
 #&&}6JKKr,   container_idc                     U c  SOU nUc  SOUnSU SU SU 3n[         R                  " UR                  S5      5      R                  S5      nSU 3$ )zBuild a managed container ID with provider and model info encoded.

Format: cntr_{base64("litellm:custom_llm_provider:{provider};model_id:{model};container_id:{original}")}
r}   r   z
;model_id:z;container_id:rk   cntr_rm   )r   r\   r   provider_part
model_partr   r   s          r)   _build_container_id,ResponsesAPIRequestUtils._build_container_id  ss     29?R#+R
5m_JzlZhiuhvw",,\-@-@-IJQQ
 ()**r,   c                     U R                  S5      (       d  [        SSU S9$ U R                  SS5      n[        R                  " UR                  S5      5      R                  S5      nUR                  S5      (       d  [        SSU S9$ Sn[        R                  " X25      nU(       d  [        SSU S9$ UR                  S5      nUR                  S	5      nUS
;   a  SOUnUS
;   a  SOUnUR                  S5      n	[        UUU	S9$ ! [         a1  n
[        R                  " SU  SU
 35        [        SSU S9s Sn
A
$ Sn
A
ff = f)zDecode a managed container ID to extract provider, model, and original container ID.

Returns:
    DecodedResponseId with custom_llm_provider, model_id, and response_id (original container_id)
r   Nr   r}   rk   zlitellm:zH^litellm:custom_llm_provider:([^;]*);model_id:([^;]*);container_id:(.+)$r{   r|   )r}   Noner   zError decoding container_id 'r   )r~   r   r   rn   r   rp   rq   rematchgroupr   r   r   )r   r   r   patternr   raw_providerraw_model_idr   r\   original_container_idr   s              r)   _decode_container_id-ResponsesAPIRequestUtils._decode_container_id*  sc   3	**733((,! ,  &--gr:J))**;*;G*DELLWUJ ((44((,! ,  bGHHW1E((,! ,  !;;q>L ;;q>L*6,*F$L+|;tH$)KKN!$$7!1 
  	  #@cRSQT!UV$$(( 	s0    D A&D 
)D 4AD 
E&D<6E<Ec                 P    [         R                  U 5      nUR                  SU 5      $ )zDecode a managed container ID to get the original provider-issued ID.

This is used when making upstream API calls - we need to send the original
container ID that the provider issued, not our encoded version.
r]   )r   r   r`   )r   r   s     r)   decode_container_id_to_original8ResponsesAPIRequestUtils.decode_container_id_to_originalf  s%     +??M{{=,77r,   annotationsc                 ~    U (       a  [        U [        5      (       d  gU  H  n[        R                  UUU5        M     g)zNEncode ``container_id`` on each annotation (e.g. ``container_file_citation``).N)ra   r   r   #_encode_container_id_on_output_item)r   r   r\   anns       r)   $_encode_container_ids_in_annotations=ResponsesAPIRequestUtils._encode_container_ids_in_annotationsp  s7     *[$"?"?C$HH# r,   contentc                    U (       d  g[        U [        5      (       ai  U  Hb  n[        U[        5      (       a(  [        R	                  UR                  S5      UU5        M@  [        R	                  [        USS5      UU5        Md     gg)zKWalk message ``content`` parts and encode citation ``container_id`` values.Nr   )ra   r   rb   r   r   r`   rc   )r   r   r\   parts       r)   (_encode_container_ids_in_message_contentAResponsesAPIRequestUtils._encode_container_ids_in_message_content  su     gt$$dD)),QQ/+  -QQmT:+    %r,   r   c                   ^^ U c  gS[         S[        [            4UU4S jjn[        U [        5      (       a  U R	                  S5      n[        U[         5      (       a  U" U5      nUb  XPS'   U R	                  S5      n[        U[        5      (       a5  UR	                  S5      n[        U[         5      (       a  U" U5      nUb  XVS'   U R	                  S5      S:X  a&  [
        R                  U R	                  S5      TT5        g[        U SS5      n[        U[         5      (       a  U" U5      nUb   [        U SU5        [        U SS5      n	U	b  [
        R                  U	TT5        [        U SS5      S:X  a#  [
        R                  [        U SS5      TT5        gg! [         a    [        R                  " S	S
S9   N}f = f)a  Mutate one output item (dict or object): wrap raw ``container_id`` as LiteLLM-managed.

Handles top-level ``container_id`` and nested ``code_interpreter_call.container_id``
(some wire payloads nest the tool call). Used by non-streaming responses and by
streaming ``response.output_item.*`` events so UIs see managed IDs incrementally.

For ``message`` items, also encodes ``container_id`` inside
``content[].annotations`` (``container_file_citation``), which is what clients use
to fetch generated files.
Nr   r0   c                 ~   > [         R                  U 5      nUR                  S5      b  g [         R                  TTU S9$ )Nr   )r   r\   r   )r   r   r`   r   )r   r   r   r\   s     r)   _maybe_encodeSResponsesAPIRequestUtils._encode_container_id_on_output_item.<locals>._maybe_encode  sH    .CCLQG{{01=+??$7!) @  r,   code_interpreter_calltyper#   r   z3Could not set container_id on streaming output itemT)exc_info)r   r   ra   rb   r`   r   r   rc   setattrr   r   r   r   )
r   r   r\   r   cidencnestednccid_attr
nested_objs
    ``       r)   r   <ResponsesAPIRequestUtils._encode_container_id_on_output_item  s     <	 	 	 	 dD!!((>*C#s###C(?+.(XX56F&$''ZZ/b#&&'+C14~.xx9,(QQHHY''
 46h$$)CD.#6 T#:DA
!$HH# 4&)3$MMi.# 4 ! "((M!%s   2F F=<F=	collectedc                 z    U (       a  [        U [        5      (       d  g U  H  n[        R                  X!5        M     g rR   )ra   r   r   '_collect_container_ids_from_output_item)r   r   r   s      r)   '_collect_container_ids_from_annotations@ResponsesAPIRequestUtils._collect_container_ids_from_annotations  s1    
 *[$"?"?C$LL r,   c                    U (       d  g [        U [        5      (       ag  U  H`  n[        U[        5      (       a'  [        R	                  UR                  S5      U5        M?  [        R	                  [        USS 5      U5        Mb     g g )Nr   )ra   r   rb   r   r   r`   rc   )r   r   r   s      r)   +_collect_container_ids_from_message_contentDResponsesAPIRequestUtils._collect_container_ids_from_message_content  so    
 gt$$dD)),TT/!
 -TTmT:!   %r,   c                    U c  g[        U [        5      (       a  U R                  S5      n[        U[        5      (       a  U(       a  UR	                  U5        U R                  S5      n[        U[        5      (       a>  UR                  S5      n[        U[        5      (       a  U(       a  UR	                  U5        U R                  S5      S:X  a%  [
        R                  U R                  S5      U5        g[        U SS5      n[        U[        5      (       a  U(       a  UR	                  U5        [        U SS5      nUb  [
        R                  Xa5        [        U SS5      S:X  a"  [
        R                  [        U SS5      U5        gg)zDCollect managed or raw ``container_id`` values from one output item.Nr   r   r   r#   r   )	ra   rb   r`   r   addr   r   rc   r   )r   r   r   r   r   r   r   s          r)   r   @ResponsesAPIRequestUtils._collect_container_ids_from_output_item  s>    <dD!!((>*C#s##c"XX56F&$''ZZ/b#&&2MM"%xx9,(TTHHY' 46h$$MM(#T#:DA
!$LL 4&)3$PPi. 4r,   c                     U c  / $ [        U [        5      (       a  U R                  S/ 5      nO[        U S/ 5      =(       d    / n[	        5       nU(       a  U H  n[
        R                  X25        M     [        U5      $ )zBReturn unique container IDs referenced in a Responses API payload.r   )ra   rb   r`   rc   setr   r   r   )r_   r   r   r   s       r)   -collect_container_ids_from_responses_responseFResponsesAPIRequestUtils.collect_container_ids_from_responses_response.  sp     Ih%%\\(B/FXx4:F!e	(PP  Ir,   c                 ,   U=(       d    0 nUR                  S0 5      =(       d    0 nUR                  S5      n[        U [        5      (       a  U R                  S/ 5      nO[        U S/ 5      nU(       d  U $ U H  n[        R                  UUUS9  M     U $ )zEncode container IDs in the response output with provider/model info.

This walks through all output items and encodes any container_id fields
so that follow-up container API calls can auto-route to the correct provider.
rZ   r[   r   )r   r   r\   )r`   ra   rb   rc   r   r   )rO   r   rP   rZ   r\   r   r   s          r)   rf   :ResponsesAPIRequestUtils._update_container_ids_in_responseA  s     ,1r%5%9%9,%K%Qr
>>$' ,d33+//"=F3XrBF))D$HH$7! I   &%r,   text_formatr   textr   c                 t    U b4  Uc1  SSK Jn  U" U 5      nUb   SUS   US   S   US   S   US   S   S	.0nU$ U$ )
a<  
Convert text_format parameter to text parameter for the responses API.

Args:
    text_format: Pydantic model class or dict to convert to response format
    text: Existing text parameter (if provided, text_format is ignored)

Returns:
    ResponseText object with the converted format, or None if conversion fails
r   )type_to_response_format_paramr   r   json_schemanameschemastrict)r   r  r  r	  ) litellm.llms.base_llm.base_utilsr  )r  r  r  response_formats       r)   !convert_text_format_to_text_param:ResponsesAPIRequestUtils.convert_text_format_to_text_paramb  so     "t|V <KHO*  / 7 / >v F"1-"@"J"1-"@"J	 r,   secret_fieldstoolsc                    SSK Jn  SSKJn  SnU (       a&  [	        U [
        5      (       a  U R                  S5      nSnSnSnU(       a;  U" U5      nUR                  U5      nUR                  U5      nUR                  U5      nU(       a  U H  n	[	        U	[
        5      (       d  M  U	R                  S5      S:X  d  M1  U	R                  S0 5      n
U
(       d  ML  [	        U
[
        5      (       d  Mc  U" U
5      nUR                  U5      nU(       a;  Uc  0 nUR                  5        H"  u  nnX;  a  0 Xm'   Xm   R                  U5        M$     Uc  0 nUR                  U
5        M     UUUU4$ )	z
Extract MCP auth headers from the request to pass to MCP server.
Headers from tools.headers in request body should be passed to MCP server.
r   )Headers)MCPRequestHandlerNraw_headersr   mcpheaders)starlette.datastructuresr  Alitellm.proxy._experimental.mcp_server.auth.user_api_key_auth_mcpr  ra   rb   r`   !_get_mcp_auth_header_from_headers)_get_mcp_server_auth_headers_from_headers _get_oauth2_headers_from_headersitemsupdate)r  r  r  r  raw_headers_from_requestmcp_auth_headermcp_server_auth_headersoauth2_headersheaders_objtooltool_headersheaders_obj_from_tooltool_mcp_server_auth_headersserver_aliasheaders_dicts                  r)    extract_mcp_headers_from_request9ResponsesAPIRequestUtils.extract_mcp_headers_from_request  s    	5	

 >B Zt<<'4'8'8'G$ *.GK37#!":;K/QQO "KKKX $ /OON dD))dhhv.>%.G#'88Ir#:L#|
<(F(F 180E--WW 5 5
 86>:< 7
 ">!C!C!E! , ,#/#NLN$;$I 7 E L L$0!" "F 4;794077E9 > #$	
 	
r,   rS   rR   )4__name__
__module____qualname____firstlineno____doc__staticmethodr   r   r   r   boolr*   r   r   r8   r   rM   r   r   rU   r	   rt   r   r   tupler   re   r   rd   r   r   r   rI   r   r   r   r   r   r   r   r   r   r   r   r   rf   r   rb   r  r   r(  __static_attributes__rS   r,   r)   r   r   $   s^   <"49-  d^ &c]	
  0 
 6:	11'=1 'H1  (S	2	1
 
1 1f .KS#X.K	*.K .Kb  6: 4%c] #4S>2 
	    6: $S#X%c] #4S>2 
c3h	    6:4& %&:DcN&J K4&%c]4& #4S>24& 
#T#s(^3	4	4& 4&l $3 $ $ $ $ c htCH~6N  2 EE*-E	E E  ) )	x}c!	" )  )D >.S#X>?>3-> 
%tCH~5	6> >@ "C "C " "H +%c]+3-+ + 
	+ + 66	6 6p ;8C= ;Xc] ; ; L!L	L L. +%c]+3-+ + 
	+ +$ 93 93D 9 9v 8c 8c 8 8 %c] 3- 
	  %c] 3- 
	 . JJ%c]J 3-J 
	J JX 		s8	 
	 	 s8 
 & &&s8& 
& &P  S	  $  6:& %&:DcN&J K&%c]& #4S>2& 
#T#s(^3	4	& &@  *.eD$5t$;<=~& 
.	! B M
S#X/M
&M
 
c4S>)*+c3h c3h 	"
M
 M
r,   r   c                   f    \ rS rSr\S\\\4   S\4S j5       r	\S\
\\\4      S\4S j5       rSrg)	ResponseAPILoggingUtilsi  usager0   c                 J    [        U [        5      (       a  gSU ;   a  SU ;   a  gg)z1returns True if usage is from OpenAI Response APITinput_tokensoutput_tokensF)ra   r   )r5  s    r)   _is_response_api_usage.ResponseAPILoggingUtils._is_response_api_usage  s*     e-..U"%'?r,   usage_inputc           
      N   U c  [        SSSS9$ [        U [        5      (       a  [        U 5      n U R                  S5      c  SU ;   a  U S   U S'   U R                  S5      c  SU ;   a  U S   U S'   U R                  S5      nUc0  U R                  S	5      nU R                  S
5      nUb  Ub  X#-   nXS'   [	        S0 U D6nOU nUR
                  =(       d    SnUR                  =(       d    SnSnUR                  (       a  [        UR                  [        5      (       a  [        S0 UR                  D6nO`[        [        UR                  SS5      [        UR                  SS5      [        UR                  SS5      [        UR                  SS5      S9nSn[        USS5      n	U	(       a8  [        [        U	SS5      [        U	SS5      [        U	SS5      [        U	SS5      S9n[        UUXV-   UUS9n
[        US5      (       a$  UR                  b  [        U
SUR                  5        U
$ )z
Transforms ResponseAPIUsage or ImageUsage to a Usage object.

Both have the same spec with input_tokens, output_tokens, and
input_tokens_details (text_tokens, image_tokens).
Nr   )prompt_tokenscompletion_tokenstotal_tokensinput_tokens_detailsinput_token_detailsoutput_tokens_detailsoutput_token_detailsr?  r7  r8  cached_tokensaudio_tokenstext_tokensimage_tokens)rD  rE  rF  rG  reasoning_tokens)rH  rG  rF  rE  )r=  r>  r?  prompt_tokens_detailscompletion_tokens_detailscostrS   )r   ra   rb   r`   r   r7  r8  r@  r   rc   r   hasattrrK  r   )r;  r?  r7  r8  response_api_usager=  r>  rI  rJ  rB  
chat_usages              r)   +_transform_response_api_usage_to_chat_usageCResponseAPILoggingUtils._transform_response_api_usage_to_chat_usage  sr    "#  k4(({+K  67?)[86ABW6X23 78@*k97B*834 '??>:L#*~> + @+0I#/#?L2>/!1!@K!@!,/<<A!3!A!A!FQFJ22,AA4HH(B )(==)% )C")*??RV# ")*??QU" !(*??PT! ")*??QU")% OS! ' 7!
 !(F!()+=t" %%:NDQ#$9=$O$%:NDQ)% '/&:"7&?

 %v..3E3J3J3VJ(:(?(?@r,   rS   N)r*  r+  r,  r-  r/  r	   rb   r   r0  r9  r   r   rO  r2  rS   r,   r)   r4  r4    se    eD2B,B&C    ZeD*:$:;<Z	Z Zr,   r4  )"rn   r   typingr   r   r   r   r   r   r	   r
   r   r   pydanticr   r%   litellm._loggingr   .litellm.llms.base_llm.responses.transformationr   litellm.types.llms.openair   r   r   r   litellm.types.responses.mainr   litellm.types.utilsr   r   r   r   r   r4  rS   r,   r)   <module>rX     sZ     	     + Q  ; n
 n
be er,   