
    ^hu                     :   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	  S SK
r
S SKrS SKJr  S SKJr  S SKJr  S SK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J r J!r!J"r"J#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.J/r/  \(       a	  S SK0J1r2  \2r3O\r3 " S S\5      r4g)    N)TYPE_CHECKINGAnyDictListOptionalUnioncast)RESPONSE_FORMAT_TOOL_NAME)map_finish_reason)anthropic_messages_pt)
BaseConfigBaseLLMException)AllAnthropicToolsValuesAnthropicComputerToolAnthropicHostedToolsAnthropicInputSchemaAnthropicMessagesToolAnthropicMessagesToolChoiceAnthropicSystemMessageContent)AllMessageValuesChatCompletionCachedContentChatCompletionSystemMessageChatCompletionToolCallChunk#ChatCompletionToolCallFunctionChunkChatCompletionToolParam)Message)PromptTokensDetailsWrapper)ModelResponseUsageadd_dummy_toolhas_tool_call_blocks   )AnthropicErrorprocess_anthropic_headers)Loggingc                     ^  \ rS rSr% SrSr\\   \S'   Sr	\\
   \S'   Sr\\   \S'   Sr\\   \S'   Sr\\   \S	'   Sr\\   \S
'   Sr\\   \S'          S@S\\   S\\
   S\\   S\\   S	\\   S
\\   S\\   SS4S jjr\U 4S j5       rS\4S jrS\4S jr     SAS\S\\   S\S\S\S\S\4S jjrS\\   S\\   S\\   4S jrS\S\4S jrS\S\\   4S jrS \\ \\\   4      S\\\      4S! jr!S"\S\\   S\4S# jr"S$\S"\S\S%\S\4
S& jr# SBS'\\   S\$4S( jjr%S)\\&   S\4S* jr'S\\\      S\4S+ jr(S)\\&   S\4S, jr)S)\\&   S\\*   4S- jr+S\S)\\&   S"\S.\S/\S\4S0 jr,S1\\   S2\\-   S\\.   4S3 jr/  SCS\S4\0Rb                  S5\2S6\3S7\4S)\\&   S"\4S.\S8\5S\\   S1\\   S\24S9 jjr6\7S2\\-   S\\.   4S: j5       r8S;\S<\S/\ \4\0Rr                  4   S\:4S= jr; SBS/\S\S)\\&   S"\S\\   S\44S> jjr<S?r=U =r>$ )DAnthropicConfig+   z
Reference: https://docs.anthropic.com/claude/reference/messages_post

to pass metadata to anthropic, it's {"user_id": "any-relevant-information"}
   
max_tokensNstop_sequencestemperaturetop_ptop_kmetadatasystemreturnc                     [        5       nUR                  5        H(  u  pU	S:w  d  M  U
c  M  [        U R                  X5        M*     g )Nself)localsitemssetattr	__class__)r3   r*   r+   r,   r-   r.   r/   r0   locals_keyvalues              d/home/james-whalen/.local/lib/python3.13/site-packages/litellm/llms/anthropic/chat/transformation.py__init__AnthropicConfig.__init__<   s7     (!--/JCf}!23 *    c                     > [         TU ]  5       $ N)super
get_config)clsr7   s    r;   rB   AnthropicConfig.get_configM   s    w!##r>   modelc                 
    / SQ$ )N)streamstopr,   r-   r*   max_completion_tokenstoolstool_choiceextra_headersparallel_tool_callsresponse_formatuser )r3   rE   s     r;   get_supported_openai_params+AnthropicConfig.get_supported_openai_paramsQ   s    
 	
r>   c                     SSS.$ )N
2023-06-01prompt-caching-2024-07-31)anthropic-versionanthropic-betarP   )r3   s    r;   get_cache_control_headers)AnthropicConfig.get_cache_control_headersa   s    !-9
 	
r>   api_keyanthropic_versioncomputer_tool_usedprompt_caching_setpdf_usedis_vertex_requestc                    / nU(       a  UR                  S5        U(       a  UR                  S5        U(       a  UR                  S5        U=(       d    SUSSS.nUSL a   U$ [        U5      S:  a  S	R                  U5      US
'   U$ )NrU   zcomputer-use-2024-10-22zpdfs-2024-09-25rT   zapplication/json)rV   z	x-api-keyacceptzcontent-typeTr   ,rW   )appendlenjoin)	r3   rZ   r[   r\   r]   r^   r_   betasheaderss	            r;   get_anthropic_headers%AnthropicConfig.get_anthropic_headersg   s     LL45LL23LL*+!2!Bl (.	
 $  Z!^(+G$%r>   rK   parallel_tool_usec                    S nUS:X  a
  [        SS9nOVUS:X  a
  [        SS9nOF[        U[        5      (       a1  UR                  S0 5      R                  S5      n[        SS9nUb  XCS'   Ub  Ub  U(       + US'   U$ [        SU(       + S	9nU$ )
Nautotyperequiredanyfunctionnametooldisable_parallel_tool_use)rn   rt   )r   
isinstancedictget)r3   rK   rj   _tool_choice
_tool_names        r;   _map_tool_choice AnthropicConfig._map_tool_choice   s     ?C& 6L J&6EBLT**$R8<<VDJ6FCL%'1V$( '@Q<Q89 	  ;2C.C  r>   rs   c                    S nUS   S:X  d	  US   S:X  aR  US   R                  SS0 S.5      n[        S0 UD6n[        US   S   US9nUS   R                  S	5      nUb  XeS	'   UnGOUS   R                  S
5      (       a  SUS   ;  a  [	        S5      eUS   S   R                  S5      nUS   S   R                  S5      nUb  Uc  [	        S5      e[        US   US   R                  SS5      UUS9n	US   S   R                  S5      n
U
b  XS'   U	nOaUS   R                  S5      (       d  US   R                  S5      (       a/  US   R                  S5      nUc  [	        S5      e[        US   US9nUc  [	        SUS    35      eUR                  SS 5      nUR                  S0 5      R                  SS 5      nUb  XS'   U$ Ub#  [        U[        5      (       a  [        S0 UD6US'   U$ )Nrn   rq   custom
parametersobject)rn   
propertiesrr   rr   input_schemadescription	computer_z&Missing required parameter: parametersdisplay_width_pxdisplay_height_pxzAMissing required parameter: display_width_px or display_height_pxcomputer)rn   rr   r   r   display_numberbash_text_editor_z Missing required parameter: name)rn   rr   zUnsupported tool type: cache_controlrP   )
rw   r   r   
startswith
ValueErrorr   r   ru   rv   r   )r3   rs   returned_tool_input_schemar   _tool_description_display_width_px_display_height_px_computer_tool_display_numberfunction_name_cache_control_cache_control_functions                 r;   _map_tool_helper AnthropicConfig._map_tool_helper   s    <@<:%f)A"&z"2"6"6$"$#M 2F1V1VL)*%f-)E
  
+//>L''3m$!M&\$$[114
#33 !IJJ/3J/?/M/Q/Q"0 15Z0@0N0R0R#1 !(,>,F W  3&\*%))&*=!2"4	N #:.|<@@AQRO*3B/0*M&\$$W--f1H1H2
 2
 !,008M$ !CDD0&\"M  6tF|nEFF /48"&((:r":">">PT"U%-;/*  %0Z#T6
 6
 .I .).M/* r>   rJ   c                     / nU H>  nSU;   a  UR                  U5        M  U R                  U5      nUR                  U5        M@     U$ )Nr   )rc   r   )r3   rJ   anthropic_toolsrs   new_tools        r;   
_map_toolsAnthropicConfig._map_tools   sM    D%&&t,006&&x0  r>   rH   c                 .   S n[        U[        5      (       a   US:X  a  [        R                  SL a  U$ U/nU$ [        U[        5      (       aH  / nU H/  nUS:X  a  [        R                  SL a  M  UR                  U5        M1     [        U5      S:  a  UnU$ )N
Tr   )ru   strlitellmdrop_paramslistrc   rd   )r3   rH   new_stopnew_vvs        r;   _map_stop_sequences#AnthropicConfig._map_stop_sequences   s     )-dC  %%-vH  d##EI))T1Q  5zA~ r>   optional_paramsc                 6    SU;  a  X!S'   U$ / US   QUQUS'   U$ )NrJ   rP   )r3   r   rJ   s      r;   _add_tools_to_optional_params-AnthropicConfig._add_tools_to_optional_params  sE     /)',G$ 	( )((OG$ r>   non_default_paramsr   c                    UR                  5        GHe  u  pVUS:X  a  XbS'   US:X  a  XbS'   US:X  a   U R                  U5      nU R                  X'S9nUS:X  d  US:X  a5  U R                  UR	                  S5      UR	                  S5      S9nUb  XS'   US:X  a	  US	L a  XbS'   US
:X  aB  [        U[        5      (       d  [        U[        5      (       a  U R                  U5      n	U	b  XS'   US:X  a  XbS'   US:X  a  XbS'   US:X  ac  [        U[        5      (       aN  S n
SU;   a  US   n
OSU;   a  US   S   n
 [        SS.nU R                  U
S9nU R                  X+/S9nXS'   S	US'   US:X  d  GM^  SU0US'   GMh     U$ )Nr*   rI   rJ   )r   rJ   rK   rM   )rK   rj   rG   TrH   r+   r,   r-   rN   response_schemajson_schemaschemars   )rr   rn   )r   	json_moderO   user_idr/   )r5   r   r   rz   rw   ru   r   r   r   rv   r
   *_create_json_tool_call_for_response_format)r3   r   r   rE   r   paramr:   
tool_valuerx   _valuer   r   s               r;   map_openai_params!AnthropicConfig.map_openai_params!  s    /446LE$05-//05-!__U3
"&"D"D$3 #E # %2G)G))$6$:$:=$I*<*@*@AV*W *    +5AM2 Ud],1)Juc$:$:jPT>U>U11%8%8>$45%16.+0())j.E.E.2$-"'(9":K"e+"'"6x"@K )B6RGG + H  #'"D"D$37 #E # 2>./3,/8%.@
+o 7r r>   r   c                 ^    [        SS9nUc  SUS'   0 US'   OSU0US'   [        [        US9nU$ )a  
Handles creating a tool call for getting responses in JSON format.

Args:
    json_schema (Optional[dict]): The JSON schema the response should be in

Returns:
    AnthropicMessagesTool: The tool call to send to Anthropic API to get responses in JSON format
r   rm   TadditionalPropertiesr   valuesr   )r   r   r
   )r3   r   r   r   s       r;   r   :AnthropicConfig._create_json_tool_call_for_response_formatc  sV     /C/
  59M01*,M,'+3[*AM,'%*
 r>   messagesc                     U HW  nUR                  SS5      b    gUR                  S5      nUc  M.  [        U[        5      (       d  ME  U H  nSU;   d  M      g   MY     g)z}
Return if {"cache_control": ..} in message content block

Used to check if anthropic prompt caching headers need to be set.
r   NTcontentF)rw   ru   r   )r3   r   message_message_contentr   s        r;   is_cache_control_set$AnthropicConfig.is_cache_control_set  s^      G{{?D1=&{{95+
;KT0R0R/G&'1#  0   r>   c                 d    Uc  gU H&  nSU;   d  M  US   R                  S5      (       d  M&    g   g)NFrn   r   T)r   )r3   rJ   rs   s      r;   is_computer_tool_used%AnthropicConfig.is_computer_tool_used  s9     =D~$v,"9"9+"F"F  r>   c                     U HM  nSU;   d  M  US   c  M  [        US   [        5      (       d  M-  US    H  nSU;   d  M  US   S:w  d  M      g   MO     g)z-
Set to true if media passed into messages.

r   rn   textTF)ru   r   )r3   r   r   r   s       r;   is_pdf_usedAnthropicConfig.is_pdf_used  s]    
  GW$I&2wy1488&y1G(WV_-F#  2   r>   c                 f   / n/ n[        U5       H  u  pEUS   S:X  d  M  Sn[        S0 UD6n[        US   [        5      (       a/  [	        SUS   S9nSU;   a  US   US'   UR                  U5        SnOm[        US   [        5      (       aU  US    HJ  n	[	        U	R                  S	5      U	R                  S5      S9nSU	;   a  U	S   US'   UR                  U5        ML     SnU(       d  M  UR                  U5        M     [        U5      S
:  a#  [        U5       H  nUR                  U5        M     U$ )z
Translate system message to anthropic format.

Removes system message from the original list and returns a new list of anthropic system message content.
roler0   Fr   r   )rn   r   r   Trn   r   rP   )	enumerater   ru   r   r   rc   r   rw   rd   reversedpop)
r3   r   system_prompt_indicesanthropic_system_message_listidxr   valid_contentsystem_message_block anthropic_system_message_content_contents
             r;   translate_system_message(AnthropicConfig.translate_system_message  se    !#MO%%h/LCv(*&+'B'MW'M$29=sCC7T#1)<84 '*>>0A 9I 2888 %)M	 2D99$+I$69%-\\&%9%-\\&%9 9 +h6 ( 9 =_M 6<<< %7 %)M =)005I 0J $%) 56S! 7 -,r>   litellm_paramsrg   c           	          SU;  aZ  UbW  [        U5      (       aG  [        R                  (       a  U R                  [	        SS95      US'   O[        R
                  " SSSS9eU R                  US9n[        U5      S	:  a  XcS
'    [        UUSS9n[        R                  R                  5       n	U	R                  5        H  u  pX;  d  M  XU
'   M     UR!                  SS5      nU(       a%  [#        U[$        5      (       a  SU;   a
  SUS   0US'   UUS.UEnU$ ! [         a(  n[        SSR                  [        U5      U5      S9eSnAff = f)z)
Translate messages to anthropic format.
rJ   N	anthropic)custom_llm_providerzAnthropic doesn't support tool calling without `tools=` param specified. Pass `tools=` param OR set `litellm.modify_params = True` // `litellm_settings::modify_params: True` to add dummy tool to the request. )r   rE   llm_providerr   r   r0   )rE   r   r   i  z{}
Received Messages={})status_coder   r/   r   )rE   r   )r!   r   modify_paramsr   r    UnsupportedParamsErrorr   rd   r   	Exceptionr#   formatr   r'   rB   r5   rw   ru   rv   )r3   rE   r   r   r   rg   r   anthropic_messageseconfigkr   _litellm_metadatadatas                 r;   transform_request!AnthropicConfig.transform_request  s|   	 ?*$$X..$$+/??"{C,( 44 n!,  )-(E(Ex(E(X%,-1(EH%
	!6!(" ((335LLNDA(%&"	 # +..z4@,d33..+46G	6R*SOJ' *
 
 ;  	 299#a&(K 	s   D 
E##EEr   
tool_callsc                     S nUSL a]  [        U5      S:X  aN  S nSUS   S   ;   a*  US   S   S   [        :X  a  US   S   R                  S5      nUb  [        R	                  US9nU$ )NT   rr   r   rq   	arguments)r   )rd   r
   rw   r'   !_convert_tool_response_to_message)r3   r   r   _messagejson_mode_content_strs        r;   !_transform_response_for_json_mode1AnthropicConfig._transform_response_for_json_mode+  s    
 .2ZA!537!*Q-
33qM*-f59RR(21j(A(E(Ek(R%$0*LL) M  r>   raw_responsemodel_responselogging_objrequest_dataencodingc                    0 n[        [        UR                  5      5      US'   UR                  UU
UR                  SU0S9   UR                  5       nSU;   a.  [        USS 5      n[        [        US   5      UR                  US9eSn/ n[        US	   5       Hd  u  nnUS
   S:X  a
  UUS   -  nM  US
   S:X  d  M$  UR                  [        US   S[        US   [
        R                   " US   5      S9US95        Mf     ["        R$                  " UU=(       d    S S9nU R'                  UUS9nUb  SUS'   UnUUR(                  S   l        US	   UR,                  S'   [/        US   5      UR(                  S   l        US   S   nUS   S   nUS   nSnSn[3        [4        R4                  " 5       5      Ul        Xl        SU;   a
  US   nUU-  nSU;   a
  US   nUU-  n[;        US9nUU-   n[=        UUUUUUS9n[?        USU5        Xl        U$ ! [         aJ  n[        USS 5      n[        SR                  [        U5      UR                  5      UR                  US9eS nAff = f) Nadditional_headerscomplete_input_dict)inputrZ   original_responseadditional_argsrg   z7Unable to get json response - {}, Original Response: {})r   r   rg   errorr   r   rn   r   tool_useidrq   rr   r  )rr   r   )r  rn   rq   index)r   r   )r   r   rH   stop_reasonr   r  usageinput_tokensoutput_tokenscache_creation_input_tokenscache_read_input_tokens)cached_tokens)prompt_tokenscompletion_tokenstotal_tokensprompt_tokens_detailsr  r  ) r$   rv   rg   	post_callr   jsonr   getattrr#   r   r   r   r   rc   r   r   dumpsr   r   r   choicesr   _hidden_paramsr   finish_reasoninttimecreatedrE   r   r   r6   )r3   rE   r   r   r   r   r   r   r   r   rZ   r   r  completion_responser   response_headerstext_contentr   r   r   r   json_mode_messager  r  _usager  r  r  r  r	  s                                 r;   transform_response"AnthropicConfig.transform_response?  s     "/H%%&0
+, 	*//2LA	 	 	

	"."3"3"5 ))&|YE /89(44(  L<>J )*=i*H IW6?f, GFO3LV_
2%%3&t}!+%H%,V_*.**WW5E*F& #&
 !J" %$,H !% F F#% !G ! !,5;#M2,08N""1%-ATBN))*=> 7H#M27N""1%3
 ,G4^D/8I$W-+,#'(!$TYY[!1$(F2*01N*O'88M$.&,-F&G#44M :1!
 %'88'/%"7(C$;
 	/(6%w  	&|YE QXXFL-- )44( 	s   H 
I&AI!!I&c                    U S   S   R                  S5      n Ub  [        R                  " U5      n[        U[        5      (       a>  UR                  S5      =nb*  [
        R                  " [        R                  " U5      S9nU$ [
        R                  " [        R                  " U5      S9nU$  g! [        R                   a    [
        R                  " US9s $ f = f)z
In JSON mode, Anthropic API returns JSON schema as a tool call, we need to convert it to a message to follow the OpenAI format

r   rq   r   Nr   )r   )	rw   r  loadsru   rv   r   r   r  JSONDecodeError)r   r   argsr   r   s        r;   r   1AnthropicConfig._convert_tool_response_to_message  s     0:!}Z/H/L/L0
	B$0zz"78tT**#'88H#55B&tzz&7IJH#O  'tzz$7GHH#O 1   ## 	B??+@AA	Bs   A+B1 )B1 1(CCerror_messager   c                 H    [        UU[        [        R                  U5      S9$ )N)r   r   rg   )r#   r	   httpxHeaders)r3   r)  r   rg   s       r;   get_error_classAnthropicConfig.get_error_class  s%     #!0
 	
r>   c                     Uc  [         R                  " SSUS9eUR                  S5      nU R                  US9nU R	                  US9nU R                  US9n	U R                  UUU	USS9n
0 UEU
EnU$ )	NzMissing Anthropic API Key - A call is being made to anthropic but no key is set either in the environment variables or via params. Please set `ANTHROPIC_API_KEY` in your environment varsr   )r   r   rE   rJ   r   )rJ   F)r\   r]   r^   rZ   r_   )r   AuthenticationErrorrw   r   r   r   rh   )r3   rg   rE   r   r   rZ   rJ   r]   r\   r^   anthropic_headerss              r;   validate_environment$AnthropicConfig.validate_environment  s     ?-- U(   ##G,!666I!77e7D##X#6 6611# 7 
 3W2 12r>   rP   )r)   NNNNNN)NFFFFr@   )NN)?__name__
__module____qualname____firstlineno____doc__r*   r   r  __annotations__r+   r   r,   r-   r.   r/   rv   r0   r   r<   classmethodrB   rQ   rX   boolrh   r   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   LitellmMessager   r+  Responser   LoggingClassr   r   r"  staticmethodr   r,  r   r-  r2  __static_attributes____classcell__)r7   s   @r;   r'   r'   +   s    	   &*NHTN)!%K#%E8C=E8C=#Hhtn# FHSM  )-%)###' $4
4
 !4 c]4 }4 }4 4.4 4 
4" $ $
 
 
4 
 ,0#(#("' $C= !	
 !    
@#C==Ed^	-	.8O+O	 Ob	 	.E)F 	U3S	>23	$s)	,
#
,01H,I
	
@ @ @ 	@
 @ 
@H '+d^ 
<T2B-C  "d#:;<	D)9$: t  3--.3-	+	,3-jHH '(H 	H
 H H 
HTD> 45 
.	!	> "&$(xx nnx &	x
 "x x '(x x x x #x D>x 
xt 45	.	! <
 
/2
=B4CV=W
	
 "&  '(	
  # 
 r>   r'   )5r  r  typingr   r   r   r   r   r   r	   r+  r   litellm.constantsr
   'litellm.litellm_core_utils.core_helpersr   3litellm.litellm_core_utils.prompt_templates.factoryr   )litellm.llms.base_llm.chat.transformationr   r   litellm.types.llms.anthropicr   r   r   r   r   r   r   litellm.types.llms.openair   r   r   r   r   r   litellm.types.utilsr   r<  r   litellm.utilsr   r   r    r!   common_utilsr#   r$   *litellm.litellm_core_utils.litellm_loggingr%   LiteLLMLoggingObjr>  r'   rP   r>   r;   <module>rN     sn      H H H   7 E U R    : : T T DW$LLRj Rr>   