
    AHj7&             >          d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	m
Z
mZmZmZmZmZ d dlZd dlmZ d dlZd dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lm Z m!Z! d dl"m#Z# d dl$m%Z% d dl&m'Z' d dl(m)Z) d dl*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3 erd dl*m4Z4 ne5Z4d dl6m7Z7 d dl8m9Z9 d dl: d dl;m<Z< d dl=m>Z>m?Z? erd dl@mAZB neZBddlCmDZD  e%       ZE e'       ZFdee   deGfdZH	 d\de5fdZI	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d]dee5e/f   de5d ee
e.      d!ee5   d"eeJ   d#ee,   d$eee5ef      d%eeG   d&ee5   d'ee-   d(eeG   d)eeG   d*eeG   d+eeK   d,ed-   d.ee2   dee	e3      d/eeK   d0eed1      d2ee5   d3eee5ef      d4eee5ef      d5eee5ef      d6eeeKej                  f      d7ee5   dee1eDf   f4d8ZMe?	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d^dee5e/f   de5d ee
e.      d!ee5   d"eeJ   d#ee,   d$eee5ef      d%eeG   d&ee5   d'ee-   d(eeG   d)eeG   d*eeG   d+eeK   d,ed-   d9eeed:   eNf      d.ee2   dee	e3      d/eeK   d0eed1      d2ee5   d;ee5   d<ee5   d3eee5ef      d4eee5ef      d5eee5ef      d6eeeKej                  f      d7ee5   dee1eDf   f:d=       ZOdee5e/f   de5d7ee5   d>ee   d?ee5ef   d@ee5ef   dePee5e/f   e5ee5   f   fdAZQde5d7ee5   dBe<d@ee5ef   dePe5ee5   f   f
dCZRdee5e/f   dee	e3      d?ee5ef   d@ee5ef   dePee5e/f   ee	e3      f   f
dDZSe?	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d_dee5e/f   de5d ee
e.      d!ee5   d"eeJ   d#ee,   d$eee5ef      d%eeG   d&ee5   d'ee-   d(eeG   d)eeG   d*eeG   d+eeK   d,ed-   d9eeed:   eNf      d.ee2   dee	e3      d/eeK   d0eed1      d2ee5   d;ee5   d<ee5   d3eee5ef      d4eee5ef      d5eee5ef      d6eeeKej                  f      dEee
e5      d7ee5   f:dF       ZTe?	 	 	 	 	 d`dGe5d3eee5ef      d4eee5ef      d5eee5ef      d6eeeKej                  f      d7ee5   deUfdH       ZVe?	 	 	 	 	 d`dGe5d3eee5ef      d4eee5ef      d5eee5ef      d6eeeKej                  f      d7ee5   deeUeeeeUf   f   fdI       ZWe?	 	 	 	 	 d`dGe5d3eee5ef      d4eee5ef      d5eee5ef      d6eeeKej                  f      d7ee5   de1fdJ       ZXe?	 	 	 	 	 d`dGe5d3eee5ef      d4eee5ef      d5eee5ef      d6eeeKej                  f      d7ee5   dee1eeee1f   f   fdK       ZYe?	 	 	 	 	 	 	 	 dadGe5dLee5   dMee5   d ee
e5      dNeJdOedP   d3eee5ef      d6eeeKej                  f      d7ee5   defdQ       ZZe?	 	 	 	 	 	 	 	 dadGe5dLee5   dMee5   d ee
e5      dNeJdOedP   d3eee5ef      d6eeeKej                  f      d7ee5   deeeeeef   f   fdR       Z[e?	 	 	 	 	 d`dGe5d3eee5ef      d4eee5ef      d5eee5ef      d6eeeKej                  f      d7ee5   de1fdS       Z\e?	 	 	 	 	 d`dGe5d3eee5ef      d4eee5ef      d5eee5ef      d6eeeKej                  f      d7ee5   dee1eeee1f   f   fdT       Z]e?	 	 	 	 	 	 	 dbdee5e/f   de5d!ee5   d&ee5   d3eee5ef      d4eee5ef      d5eee5ef      d6eeeKej                  f      d7ee5   de1fdU       Z^e?	 	 	 	 	 	 	 dbdee5e/f   de5d!ee5   d&ee5   d3eee5ef      d4eee5ef      d5eee5ef      d6eeeKej                  f      d7ee5   dee1eeee1f   f   fdV       Z_d?eNdeNfdWZ`e?	 	 	 dcde5dXedYee5   dZee5   d6eeK   f
d[       Zay)d    N)partial)TYPE_CHECKINGAny	CoroutineDictIterableListLiteralOptionalTypeUnioncast)	BaseModel)verbose_logger)%LiteLLMResponsesTransformationHandler)request_timeout)run_async_function)Logging)*update_responses_input_with_model_file_ids*update_responses_tools_with_model_file_ids)BaseResponsesAPIConfig)BaseLLMHTTPHandler)&LiteLLMCompletionTransformationHandler)ResponsesAPIRequestUtils)	AllMessageValuesPromptObject	ReasoningResponseIncludableResponseInputParam!ResponsesAPIOptionalRequestParamsResponsesAPIResponse
ToolChoice	ToolParam)ResponseText)get_litellm_params)get_secret_str)*)GenericLiteLLMParams)ProviderConfigManagerclient)Tool   )!BaseResponsesAPIStreamingIteratortoolsreturnc                 ,    | syt        d | D              S )z;Return True if any tool in the list has type 'file_search'.Fc              3   h   K   | ]*  }t        |t              xr |j                  d       dk(   , yw)typefile_searchN)
isinstancedictget).0ts     N/root/tools/cai/cai_env/lib/python3.12/site-packages/litellm/responses/main.py	<genexpr>z(_has_file_search_tool.<locals>.<genexpr>P   s,     U!z!T"EquuV}'EEUs   02)anyr.   s    r9   _has_file_search_toolr=   L   s    UuUUU    mock_responsec                     t        d,i i ddddddddd	d d
d dd dd dddddddd| g dgdgdddd dd d dddddddd diid!d"g dd#d$d%d&id'd(d&id)d*d i d+S )-Nid5resp_67ccd2bed1ec8190b14f964abc0542670bb6a6b452d3795bobjectresponse
created_atigstatus	completederrorincomplete_detailsinstructionsmax_output_tokensmodelzgpt-4.1-2025-04-14outputmessage4msg_67ccd2bf17f0819081ff3bb2cf6508e60bb6a6b452d3795b	assistantoutput_text)r2   textannotations)r2   rA   rF   rolecontentparallel_tool_callsTprevious_response_id	reasoning)effortsummarystoretemperatureg      ?rR   formatr2   tool_choiceautodisabled$   cached_tokensr   W   reasoning_tokens{   )input_tokensinput_tokens_detailsoutput_tokensoutput_tokens_detailstotal_tokens)r.   top_p
truncationusageusermetadata )r!   r?   s    r9   mock_responses_api_responserr   S   sU      .,
I,
j,
 *,
 k	,

 T,
 !$,
 D,
  ,
 ),
 %P)' %2$1+- ,
2 "43,
4 #D5,
6 DT:7,
8 T9,
: 3;,
< X/0=,
> 6?,
@ $ ")8!(<!#*<a)@ # W,
. .r>   inputrL   includerJ   rK   promptro   rV   rW   rX   r[   
backgroundstreamr\   rR   r$   r^   rk   rl   )r_   r`   rn   extra_headersextra_query
extra_bodytimeoutcustom_llm_providerc                   K   ddl m} |j                  |      \  }}|j                  d      xs! |j                  di       j                  d      }d}d}|j                  d      } | r,t	        | t
              rt        j                  | |      \  }}}!}!|j                  |||j                  d      ||	       d{   \  }"}#|j                  |"      }$|$s|r|$|z   nd}%i d
|d|d|d|d|d|d|	d|
d|d|d|d|d|d|d|d|d|||||d|}&|r]|r[ddl
m}' ddlm}( d|'j                         j                  dd  }) |(|||)|"        d{   }* |j                   d4| ||%||*|&||#d!|S t#        |      xr |j%                  |"      }+|j'                  |&|+#      },t)        d4| ||%|d$|, d{   }-t+        j,                  d%|-       |+rt	        |-t.              r|j1                  |-&      }.|.rq|j                  di       j                  d      }|j                  d      } t        j                  | |      \  }}}/}0|j3                  |#|.||||/|0|j                  d'      |j                  d      (	       d{   }1|1r|j5                  |-|1| )      }2|j7                  |&|xs d*+      }3g }4|r|j9                  |.|1,      }4 |j:                  d4|2||%|-j<                  d-|3 d{   }5|r.|4r,t?        |5d.      st?        |5d/      rdd0lm }6  |6|#|5|4|1      }5|5S t	        |5t.              r4|j                  ||||2       d{   \  }7}!|jC                  |5|7|13      }5|5S |-S 7 7 7 7 7 7 ,w)5a}  
    Async version of responses API with MCP integration.

    When MCP tools with server_url="litellm_proxy" are provided, this function will:
    1. Get available tools from the MCP server manager
    2. Insert the tools into the messages/input
    3. Call the standard responses API
    4. If require_approval="never" and tool calls are returned, automatically execute them
    r   LiteLLM_Proxy_MCP_Handleruser_api_key_authlitellm_metadataNsecret_fields)r   r.   litellm_trace_id)r   mcp_tools_with_litellm_proxyr   mcp_auth_headermcp_server_auth_headersrt   rJ   rK   ru   ro   rV   rX   r[   rv   rw   r\   rR   r^   rk   rl   rn   rx   )ry   rz   r{   r|   )uuid)create_mcp_list_tools_eventsmcp_   )r   r   base_item_idpre_processed_mcp_tools)rs   rL   	all_toolsr   mcp_discovery_eventscall_paramsrW   tool_server_map)r   )r   should_auto_execute)rs   rL   r.   rW   zInitial response %s)rD   litellm_call_id)	r   
tool_callsr   r   r   oauth2_headersraw_headersr   r   )rD   tool_resultsoriginal_inputF)r   original_stream_setting)r   r   )follow_up_inputrL   r   response_id	__aiter____iter__)MCPEnhancedStreamingIterator)r   base_iterator
mcp_eventsr   )r   r   r   r   )rD   mcp_tools_fetchedr   rp   )"/litellm.responses.mcp.litellm_proxy_mcp_handlerr   _parse_mcp_toolsr6   r4   r5   r    extract_mcp_headers_from_request+_process_mcp_tools_without_openai_transform_transform_mcp_tools_to_openailitellm._uuidr   ,litellm.responses.mcp.mcp_streaming_iteratorr   uuid4hex_create_mcp_streaming_responsebool_should_auto_execute_tools_prepare_initial_call_params
aresponsesr   debugr!   !_extract_tool_calls_from_response_execute_tool_calls_create_follow_up_input_prepare_follow_up_call_params_create_tool_execution_events_make_follow_up_callrA   hasattrr   $_add_mcp_output_elements_to_response)8rs   rL   rt   rJ   rK   ru   ro   rV   rW   rX   r[   rv   rw   r\   rR   r^   r.   rk   rl   rn   rx   ry   rz   r{   r|   kwargsr   r   other_toolsr   r   r   r   _original_mcp_toolsr   openai_toolsr   r   r   r   r   r   r   initial_call_paramsrD   r   r   raw_headers_from_requestr   r   follow_up_call_paramstool_execution_eventsfinal_responser   mcp_tools_for_outputs8                                                           r9   aresponses_api_with_mcpr      sq    P 	"2259$
 

#67 6::B<	c
 
 &*OCGJJ/MM48 %EE'u
	
# (SS+%A$67' 7 T  	 -KKL
 0<{{*QUI7 	. 	&	
 	H 	2 	Y 	 	j 	& 	{ 	 	{ 	 	j  	!" 	#$ # 2+, -K4 .&	
 djjl..r234%A)E/%$6	&
  
 H(GG 

)E!5#!5+

 

 
	
 $ 
#
>
>%A ?   4PP5H Q    1	
  H .9 z&  /PP Q 

  &

+=r B F F#!
 #JJ7M )II+'( ";!N!N /%"3 /(?-4 &

+< =!',>!? "O 
" 
L ";"S"S%LQV #T # .LL$/SX M  & )+%1OO'1 P  * (V'@'U'U ($3' (	(
 ,( " -<">:> &B(7&4#8*;	&N4 &%%  0DE
 8cc*;5Q(70G	 d  , 2VV%3.B)5 W  # &%OW	b 
DL
B"Dsq   B:N<M9=B NM<A$NM?CNNA,N<N=ANN N<N?NNNNtext_formatr   service_tiersafety_identifierc                 P  K   t               }	 t        j                         }d|d<   t        j                  ||      }|||d<   |1t        j                  ||j                  dd            \  }}}}||d<   |j                  d	d      } t        t        t           |j                  d
d            }!t        t        t           |j                  dd            }"|}#t        | t              r| j                  |!|      rt        | t              rd| dg}$n#| D %cg c]  }%t        |%t              rd|%v r|% }$}%| j                  ||$||!|"|j                  dd      |j                  dd             d{   \  }}&}'t        t         t        t"        f   |&      } ||#k7  rt        j                  |      \  }}}}|j%                  d
d       |'|d<   t'        t(        fi d| d|d|d|d|d|d|d|d|d|	d|
d |d!|d"|d|d#|d$|d%|d&|d|d'|d(|d)|d*|d|d+|d,||}(t+        j,                         })t'        |)j.                  |(      }*|j1                  d|*       d{   }+t        j2                  |+      r|+ d{   },n|+},t        |,t4              r7t        j6                  |,|j                  d-i       |.      },||,j8                  d<   |,t;        d/|,       |,S c c}%w 7 7 7 j# t<        $ r}-t        j>                  |||-||0      d}-~-ww xY ww)1zS
    Async: Handles responses API requests by reusing the synchronous function
    Tr   r   rR   NrR   base_urlrL   api_baser|   litellm_logging_obj	prompt_idprompt_variablesr   non_default_paramsrn   rT   rU   rT   prompt_labelprompt_versionrL   messagesr   r   r   r   r   rL   _async_prompt_merged_paramsrs   rL   rt   rJ   rK   ru   ro   rV   rW   rX   r[   rv   rw   r\   r^   r.   rk   rl   rx   ry   rz   r{   r   r   r   responses_api_responser   r|   z8Got an unexpected None response from the Responses API: rL   r|   original_exceptioncompletion_kwargsextra_kwargs) localsasyncioget_event_loopr   !convert_text_format_to_text_paramlitellmget_llm_providerr6   r   r   strr5   r4   LiteLLMLoggingObj"should_run_prompt_management_hooks async_get_chat_completion_promptr   r   popr   	responsescontextvarscopy_contextrunrun_in_executoriscoroutiner!   /_update_responses_api_response_id_with_model_id_hidden_params
ValueError	Exceptionexception_type).rs   rL   rt   rJ   rK   ru   ro   rV   rW   rX   r[   rv   rw   r\   rR   r   r^   r.   rk   rl   rn   r   r   rx   ry   rz   r{   r|   r   
local_varsloopr   r   r   r   original_modelclient_inputitemmerged_inputmerged_optional_paramsfuncctxfunc_with_contextinit_responserD   es.                                                 r9   r   r     s'    L JD
%%'#| (II#$
 !%Jv &+2+C+CjnnZ&F,(A"Aq 1DJ,- %jj)>E#

;(EF	

;Mt0TU!2
!DDF E 
 %%#68 !& !$-&D.     *JJ%#)#!1#ZZ=%zz*:DA K  	& s$667FE&/6/G/Ge/T,&1JJ{D)4JF01

 
 	

 &
 0
 
 
 !4
 "6
  
 
 "
 
 $
  !
" $#
$ %
& '
( ")
* +
, (-
. $/
0 "1
2 3
4 !45
6 &7
8 0;
@ &&(#CGGT2"2249JKK}-**H$H h 45/__'/!',>!C$7H >QH##$9:J8*U  c b L +*  
$$ 3 (
 	

sm   L&D
K; K/5=K; 2K43C<K; /K70K; K9AK; .L&/K; 7K; 9K; ;	L#LL##L&r   r   r   c                 &   |j                  dd       }|"|j                         D ]
  \  }}|||<    | ||fS t        t        t           |j                  dd             }	t        t        t           |j                  dd             }
|}t        | t              rd| dg}n#| D cg c]  }t        |t              rd|v r| }}t        |t              r|j                  |	|      r|j                  ||||	|
|j                  dd       |j                  d	d       
      \  }}}t        t        t        t        f   |      } | |d<   ||d<   ||k7  r t        j                  |      \  }}}}||d<   |j                         D ]
  \  }}|||<    | ||fS c c}w )Nr   r   r   rn   r   rT   r   r   r   r   rs   rL   r   r|   )r   itemsr   r   r   r6   r5   r4   r   r   get_chat_completion_promptr   r   r   r   )rs   rL   r|   r   r   r   async_mergedkeyvaluer   r   r   r   r   r   r   r   s                    r9   *_apply_prompt_management_to_responses_callr  M  s    ::;TBL&,,. 	$JC#JsO	$e000Xc]FJJ{D$ABIHTNFJJ7I4,PQN%9?E0R/S 
$%&D. 
 
 .

@
@ A   ::!%-ND9!::&6= ; 
		
" U3 223\B#
7#
7N"+2+C+C%+P(A"Aq0CJ,-0668 	$JC#JsO	$ %,,,C
s   "Flitellm_paramsc                     t        j                  | ||j                  |j                        \  } }}}||d<   |||_        |||_        | |fS )NrL   r|   r   api_keyr|   )r   r   r   r
  )rL   r|   r  r   dynamic_api_keydynamic_api_bases         r9   %_resolve_model_provider_for_responsesr    sp     	  /((&&		 )<J$%"!0#"2%%%r>   c                    |j                  d      }t        |j                  d      t              r!|j                  di       j                  d      nd }t        t        t
        t        f   t        | ||            } | |d<   |rXt        t        t        t              t        t        t        t        t        t
        t        f         |      ||            }||d<   | |fS )Nmodel_file_id_mapping
model_inforA   )rs   model_idr  rs   )r.   r  r  r.   )r6   r4   r5   r   r   r   r   r   r   r   r#   r   r	   r   r   )rs   r.   r   r   r  model_info_ids         r9   _apply_managed_file_id_mappingr    s     #JJ'>? fjj.5 	

<$((.  c%%&2""7	
E  JwXi()68Dc3h$895A&&;
 $
7%<r>   allowed_openai_paramsc                    t               }ddlm} 	 |j                  d      } |j                  dd      }!|j	                  dd      du }"t        j                  ||	      }|||d
<   t        d6i |}#|#j                  r0t        |#j                  t              rt        |#j                        S t        |||#|      \  }}t        | ||| ||      \  } }}t        | |||      \  } }|j                  |      r_i d| d|d|d|d|d|d|d|d|d|	d|
d|d|d|d
|d|d|||||||||d |}$|"rt!        d6i |$S t#        t         fi |$S |d}%nt%        j&                  ||!      }%|j)                  |       |	5d"|v r1t+               j-                  |j	                  d"            }&|&|&}	|&|d<   t        j.                  |      }'t1        |      r|%|%j3                         sdd#lm}( ddh})i d|d|d|d|d|d|d|d|	d|
d|d|d|d
|d|d$|d%|d&||||||||||d'	|j9                         D *+ci c]  \  }*}+|*|)vs|*|+ c}+}*},|"r |(d6| ||d(|,S t#        |(f| ||d(|,S |%t;        j<                  d6|| |'||"|||d)|S t        j>                  ||%|'|*      }-| jA                  |||tC        |-      i |-|"|!d+|,       t        jD                  |       } |tG        d-      tH        j=                  || |%|-||#| |||xs tJ        |"|j                  d.      |%jM                  |||/      |j                  d0i       |j                  d1      2      }.t        |.tN              r7t        jP                  |.|j                  d0i       |3      }.||.jR                  d4<   |.S c c}+}*w # tT        $ r}/tW        jX                  |||/||5      d}/~/ww xY w)7zk
    Synchronous version of the Responses API.
    Uses the synchronous HTTP handler to make requests.
    r   r~   r   r   Nr   FTr   rR   rq   )rL   r|   r  r   )rs   rL   r|   r   r   r   )rs   r.   r   r   r<   rs   rL   rt   rJ   rK   ru   ro   rV   rW   rX   r[   rv   rw   r\   r^   r.   )rk   rl   rn   rx   ry   rz   r{   r|   rL   providerreasoning_effort)$aresponses_with_emulated_file_searchrk   rl   rn   )	r   r   r   r  rx   ry   rz   r{   r|   )rs   rL   r.   )rL   rs   responses_api_requestr|   	_is_asyncrw   rx   rz   rL   responses_api_provider_configresponse_api_optional_paramsr  )r   r   r   rL   rn   optional_paramsr  r|   2custom_llm_provider is required but passed as Noner*   )rL   rw   r|   r   shared_session)rL   rs   r  $response_api_optional_request_paramsr|   r  logging_objrx   rz   r{   r  r*   fake_streamr   r"  r   r|   r   rp   )-r   r   r   r6   r   r   r   r(   r?   r4   r   rr   r  r  r  _should_use_litellm_mcp_gatewayr   r   r)   !get_provider_responses_api_configupdater   _map_reasoning_effort)get_requested_response_api_optional_paramr=   supports_native_file_search.litellm.responses.file_search.emulated_handlerr  r  )litellm_completion_transformation_handlerresponse_api_handler!get_optional_params_responses_apiupdate_from_kwargsr5   ,_restore_encrypted_content_item_ids_in_inputr   base_llm_http_handlerr   should_fake_streamr!   r   r   r   r   r   )0rs   rL   rt   rJ   rK   ru   ro   rV   rW   rX   r[   rv   rw   r\   rR   r   r^   r.   rk   rl   rn   r   r   rx   ry   rz   r{   r  r|   r   r   r   r   r   r  r  mcp_call_kwargsr  _mappedr  r  _internal_skipkvemulated_kwargsresponses_api_request_paramsrD   r   s0                                                   r9   r   r     sT   P J~
17<Q1R)/4Et)LJJ|U3t;	 (II#$
 !%Jv .77
 ''J((#-
 /,::  &K 3)!	&
"" -W 3 3!-
)u) 6uV

u %DD5DQ 7 	
 $%6 & H &': '(< Y  j & {   {!" #$ (!.*("':34 5O8 .AAA%&=QQQ &,0) &GG0 * 	&!!3z!A;=SS12G "#	*1
;' %NN 	% !')10LLN 0>N7 $%6 &	
 H &': '(< Y  j & {  {   j!" #$ !-%6*)>!.*("':56 %+LLNNDAqa~6M1a4N7O: ; uE=L  &4	
 "  )0<QQ 
&B$7#+%
 
 
 %FF.K-I&;	 	% 	.. !=>.'#2
 !4 	/ 	
 )UU

 &QRR(==*G1M 3)+'!.::h'5HHF@S I  $ZZ(:B?!::&67# > 
* h 45/__'/!',>!C$7H >QH##$9:w Ox  
$$ 3 (
 	

sW   BN= .BN= N= C3N= N7N7N= 0N=  N=  DN= 7N= =	O%O  O%r   c           
      0  K   t               }	 t        j                         }d|d<   t        j                  |       }	|	j                  d      xs | } |	j                  d      xs |}t        t        f| |||||d|}
t        j                         }t        |j                  |
      }|j                  d|       d{   }t        j                  |      r| d{   }|S |}|S 7 )7 # t        $ r}t        j                  d||||      d}~ww xY ww)	zz
    Async version of the DELETE Responses API

    DELETE /v1/responses/{response_id} endpoint in the responses API

    Tadelete_responsesr   r   r|   r   r|   rx   ry   rz   r{   Nr   )r   r   r   r   !_decode_responses_api_response_idr6   r   delete_responsesr   r   r   r   r   r   r   r   r   rx   ry   rz   r{   r|   r   r   r   decoded_response_idr   r   r   r   rD   r   s                   r9   r<  r<    E    & J*
%%'&*"# %FF' 	
 *--m<K##$9:Q>Q 	 	
# 3'#!	
 	
 &&(#CGGT2"2249JKK}-**H  %H L +  
$$ 3 (
 	

Y   DB/C+ =C'>C+ C)C+ "D#C+ &D'C+ )C+ +	D4DDDc                    t               }	 |j                  d      }|j                  dd      }	|j                  dd      du }
t        di |}t	        j
                  |       }|j                  d      xs | } |j                  d	      xs |}|t        d
      t        j                  d|      }|t        d|       |j                  |       |j                  |dd| id|	i|       t        j                  | |||||||xs t        |
|j                  d      |j                  d            }|S # t        $ r}t        j                   d||||      d}~ww xY w)z
    Synchronous version of the DELETE Responses API

    DELETE /v1/responses/{response_id} endpoint in the responses API

    r   r   Nr<  FTr=  r   r|   r!  r  z&DELETE responses is not supported for r   rL   r   r  r|   r*   r"  r   r|   r  r  r$  rx   rz   r{   r  r*   r"  r   rp   )r   r6   r   r(   r   r?  r   r)   r'  r(  r0  r2  delete_response_api_handlerr   r   r   r   r   rx   ry   rz   r{   r|   r   r   r   r   r  r  rB  r  rD   r   s                   r9   r@  r@  3     & JI
17<Q1R)/4Et)LJJ2E:dB	 .77 %FF' 	
 *--m<K##$9:Q>Q 	 &QRR
 "CC(
 	& )089L8MN  	&! 	..{ "? !4 	/ 
	
 )DD# 3*G)+'!.::h'!::&67 E 
  
$$ 3 (
 	

   D(D5 5	E>EEc           
        K   t               }	 t        j                         }d|d<   t        j                  |       }	|	j                  d      xs | } |	j                  d      xs |}t        t        f| |||||d|}
t        j                         }t        |j                  |
      }|j                  d|       d{   }t        j                  |      r| d{   }n|}t        |t              r(t        j                  ||j                  di       |	      }|S 7 `7 C# t         $ r}t#        j$                  d||||
      d}~ww xY ww)ax  
    Async: Fetch a response by its ID.

    GET /v1/responses/{response_id} endpoint in the responses API

    Args:
        response_id: The ID of the response to fetch.
        custom_llm_provider: Optional provider name. If not specified, will be decoded from response_id.

    Returns:
        The response object with complete information about the stored response.
    Taget_responsesr=  r   r|   r>  Nr   r   r   )r   r   r   r   r?  r6   r   get_responsesr   r   r   r   r   r4   r!   r   r   r   r   rA  s                   r9   rM  rM    sp    2 J2
%%'#'  %FF' 	
 *--m<K##$9:Q>Q 	 	
# 3'#!	
 	
 &&(#CGGT2"2249JKK}-**H$H h 45/__'/!',>!C$7H
  L +  
$$ 3 (
 	

sN   EB/D" =D>D" D A D" ED"  D" "	E
+EE

Ec                 0   t               }	 |j                  d      }|j                  dd      }	|j                  dd      du }
t        di |}t	        j
                  |       }|j                  d      xs | } |j                  d	      xs |}|t        d
      t        j                  d|      }|t        d|       |j                  |       |j                  |dd| id|	i|       t        j                  | |||||||xs t        |
|j                  d      |j                  d            }t        |t              r(t	        j                   ||j                  di       |      }|S # t"        $ r}t%        j&                  d||||      d}~ww xY w)aq  
    Fetch a response by its ID.

    GET /v1/responses/{response_id} endpoint in the responses API

    Args:
        response_id: The ID of the response to fetch.
        custom_llm_provider: Optional provider name. If not specified, will be decoded from response_id.

    Returns:
        The response object with complete information about the stored response.
    r   r   NrM  FTr=  r   r|   r!  r  z#GET responses is not supported for rF  r*   r"  rG  r   r   r   rp   )r   r6   r   r(   r   r?  r   r)   r'  r(  r0  r2  rN  r   r4   r!   r   r   r   r   rI  s                   r9   rN  rN    s   2 JQ
17<Q1R)/4Et)LJJ/74?	 .77 %FF' 	
 *--m<K##$9:Q>Q 	 &QRR
 "CC(
 	& )056I5JK  	&! 	..{ "? !4 	/ 
	
 )66# 3*G)+'!.::h'!::&67 7 
 h 45/__'/!',>!C$7H  
$$ 3 (
 	

s   E E- -	F6FFafterbeforelimitorder)ascdescc	                 6  K   t               }
	 t        j                         }d|	d<   t        j                  |       }|j                  d      xs | } |j                  d      xs |}t        t        f| ||||||||d	|	}t        j                         }t        |j                  |      }|j                  d|       d{   }t        j                  |      r| d{   }|S |}|S 7 )7 # t        $ r}t        j                  d|||
|	      d}~ww xY ww)	z&Async: List input items for a responseTalist_input_itemsr=  r   r|   )	r   rP  rQ  rt   rR  rS  rx   r{   r|   Nr   )r   r   r   r   r?  r6   r   list_input_itemsr   r   r   r   r   r   r   r   )r   rP  rQ  rt   rR  rS  rx   r{   r|   r   r   r   rB  r   r   r   r   rD   r   s                      r9   rW  rW  P  sN     J,
%%'&*"# %FF' 	
 *--m<K##$9:Q>Q 	 
#' 3
 
 &&(#CGGT2"2249JKK}-**H  %H L +  
$$ 3 (
 	

sY   DB2C.  C*C. C, C. %D&C. )D*C. ,C. .	D7DDDc	                    t               }
	 |	j                  d      }|	j                  dd      }|	j                  dd      du }t        di |	}t	        j
                  |       }|j                  d      xs | } |j                  d	      xs |}|t        d
      t        j                  d|      }|t        d|       |
j                  |	       |j                  |
dd| id|i|       t        j                  | |||||||||||xs t        ||	j                  d      |	j                  d            }|S # t        $ r}t        j                   d|||
|	      d}~ww xY w)zList input items for a responser   r   NrW  FTr=  r   r|   r!  r  z&list_input_items is not supported for rF  r*   r"  )r   r|   r  r  r$  rP  rQ  rt   rR  rS  rx   r{   r  r*   r"  r   rp   )r   r6   r   r(   r   r?  r   r)   r'  r(  r0  r2  list_responses_input_itemsr   r   r   r   )r   rP  rQ  rt   rR  rS  rx   r{   r|   r   r   r   r   r  r  rB  r  rD   r   s                      r9   rX  rX    s    JD
17<Q1R)/4Et)LJJ2E:dB	-77 %FF' 	
 *--m<K##$9:Q>Q 	 &QRR "CC(
 	& )089L8MN  	&!..*K8-? 3 	/ 	
 )CC# 3*G)+'.::h'!::&67 D 
$  
$$ 3 (
 	

s   D,D9 9	E!EE!c           
      0  K   t               }	 t        j                         }d|d<   t        j                  |       }	|	j                  d      xs | } |	j                  d      xs |}t        t        f| |||||d|}
t        j                         }t        |j                  |
      }|j                  d|       d{   }t        j                  |      r| d{   }|S |}|S 7 )7 # t        $ r}t        j                  d||||      d}~ww xY ww)	z
    Async version of the POST Cancel Responses API

    POST /v1/responses/{response_id}/cancel endpoint in the responses API

    Tacancel_responsesr=  r   r|   r>  Nr   )r   r   r   r   r?  r6   r   cancel_responsesr   r   r   r   r   r   r   r   rA  s                   r9   r\  r\    rC  rD  c                    t               }	 |j                  d      }|j                  dd      }	|j                  dd      du }
t        di |}t	        j
                  |       }|j                  d      xs | } |j                  d	      xs |}|t        d
      t        j                  d|      }|t        d|       |j                  |       |j                  |dd| id|	i|       t        j                  | |||||||xs t        |
|j                  d      |j                  d            }|S # t        $ r}t        j                   d||||      d}~ww xY w)z
    Synchronous version of the POST Responses API

    POST /v1/responses/{response_id}/cancel endpoint in the responses API

    r   r   Nr\  FTr=  r   r|   r!  r  z&CANCEL responses is not supported for rF  r*   r"  rG  r   rp   )r   r6   r   r(   r   r?  r   r)   r'  r(  r0  r2  cancel_response_api_handlerr   r   r   r   rI  s                   r9   r]  r]  %  rJ  rK  c	                   K   t               }
	 t        j                         }d|	d<   |1t        j                  ||
j                  dd            \  }}}}||
d<   t        t        f| ||||||||d	|	}t        j                         }t        |j                  |      }|j                  d|       d{   }t        j                  |      r| d{   }n|}t        |t              r(t        j                   ||	j                  di       |	      }|S 7 `7 C# t"        $ r}t        j$                  ||||
|	
      d}~ww xY ww)z
    Async version of the POST Compact Responses API

    POST /v1/responses/compact endpoint in the responses API

    Runs a compaction pass over a conversation, returning encrypted, opaque items.
    Tacompact_responsesNr   r   r|   )	rs   rL   rJ   rW   rx   ry   rz   r{   r|   r   r   r   )r   r   r   r   r   r6   r   compact_responsesr   r   r   r   r   r4   r!   r   r   r   r   )rs   rL   rJ   rW   rx   ry   rz   r{   r|   r   r   r   r   r   r   r   r   rD   r   s                      r9   ra  ra    sn    . J3
%%''+#$ &+2+C+CjnnZ&F,(A"Aq 1DJ,-
%!5'#! 3
 
 &&(#CGGT2"2249JKK}-**H$H h 45/__'/!',>!C$7H  L +  
$$ 3 (
 	

sN   EB%D 3D4D DA D ED D 	E !D;;E  Ec	                    t               }
	 |	j                  d      }|	j                  dd      }|	j                  dd      du }t        di |	}t	        j
                  |||j                  |j                        \  }}}}||
d<   |||_        |||_        |t        d	      t        j                  ||
      }|t        d|       |
j                  |	       t        j                  |
      }t        j                  |||d      }|j                  |
|t!        |      i |d|i|       t        j"                  |       } t$        j'                  || ||||||||xs t(        ||	j                  d      |	j                  d            }t+        |t,              r(t        j.                  ||	j                  di       |      }|S # t0        $ r}t	        j2                  ||||
|	      d}~ww xY w)z
    Synchronous version of the POST Compact Responses API

    POST /v1/responses/compact endpoint in the responses API

    Runs a compaction pass over a conversation, returning encrypted, opaque items.
    r   r   Nra  FTr	  r|   r!  r  z'COMPACT responses is not supported for r  rF  r*   r"  )rL   rs   r  r#  r  r$  r|   rx   rz   r{   r  r*   r"  r   r   r   rp   )r   r6   r   r(   r   r   r   r
  r   r)   r'  r(  r   r*  r/  r0  r5   r1  r2  compact_response_api_handlerr   r4   r!   r   r   r   )rs   rL   rJ   rW   rx   ry   rz   r{   r|   r   r   r   r   r  r  r  r  r  r  r:  rD   r   s                         r9   rb  rb    sj   . Js
17<Q1R)/4Et)LJJ3U;tC	 .77 $$ 3#,,"**	
	
 -@
() &%4N"'&6N#&QRR
 "CC(
 	& )09:M9NO  	&! %NN 	% %FF.K-I&*	 	% 	.. !=>.!? !4 	/ 		
 )UU

 )EE*G1M)+ 3'!.::h'!::&67 F 
" h 45/__'/!',>!C$7H  
$$ 3 (
 	

s   F9G 	G.G))G.c                     i | j                  d      xs i }| j                  d      xs i j                  d      xs | j                  d      xs g }|r||d<   |S )Nr   ro   
guardrails)r6   )r   ro   rf  s      r9   _build_litellm_metadata_for_wsrg  f  si    ?$67=2?H	J		%2**<8 	::l#	 
 !+Or>   	websocketr   r
  c                   K   |j                  d      }|j                  dd      }t        di |}t        di |}	t        j                  | ||      \  } }
}}|j                  || |i |	|
       d}|
*t        j                  | t        j                  |
            }|xs" |j                  xs t        j                  xs d}|xs= |j                  xs/ t        j                  xs t        j                  xs t        d      }|j                         D ci c]  \  }}|dvr|| }}}t        j                  d| |||||||j                  d	      t!        |      |
d

| d{    yc c}}w 7 w)a  
    Private function to handle the Responses API WebSocket mode.

    For PROXY use only.

    Resolves the LLM provider from ``model``, looks up the matching
    ``BaseResponsesAPIConfig``, and hands off to
    ``BaseLLMHTTPHandler.async_responses_websocket``.
    r   rn   N)rL   r   r
  r  r  OPENAI_API_KEY>   r   user_api_key_dictrk  )
rL   rh  r$  r  r   r
  r{   rk  r   r|   rp   )r6   r(   r%   r   r   r0  r)   r'  LlmProvidersr   r
  
openai_keyr&   r  r2  async_responses_websocketrg  )rL   rh  r   r
  r{   r   r   rn   r  litellm_params_dict_custom_llm_providerr  r  r  resolved_api_baseresolved_api_keyr7  r8  remaining_kwargss                      r9   _aresponses_websocketrt  r  s    $ .4ZZ8M-N::fd#D)3F3N,6v6 	  	 ***0 +  GK!'!CC --.BC 	& 	ON33Ow7G7GO4  	 	,!!	,??	, 	, *+  LLNAq== 	
1   
9
9 '&C"  **%897?0   s   DE5E-)>E5'E3(E5)a  In a peaceful grove beneath a silver moon, a unicorn named Lumina discovered a hidden pool that reflected the stars. As she dipped her horn into the water, the pool began to shimmer, revealing a pathway to a magical realm of endless night skies. Filled with wonder, Lumina whispered a wish for all who dream to find their own hidden magic, and as she glanced back, her hoofprints sparkled like stardust.)NNNNNNNNNNNNNNNNNNNNNNN)NNNNNNNNNNNNNNNNNNNNNNNNNN)NNNNNNNNNNNNNNNNNNNNNNNNNNN)NNNNN)NNN   rU  NNN)NNNNNNN)NNN)br   r   	functoolsr   typingr   r   r   r   r   r	   r
   r   r   r   r   httpxpydanticr   r   litellm._loggingr   Ilitellm.completion_extras.litellm_responses_transformation.transformationr   litellm.constantsr   #litellm.litellm_core_utils.asyncifyr   *litellm.litellm_core_utils.litellm_loggingr   r   8litellm.litellm_core_utils.prompt_templates.common_utilsr   r   .litellm.llms.base_llm.responses.transformationr   *litellm.llms.custom_httpx.llm_http_handlerr   ;litellm.responses.litellm_completion_transformation.handlerr   litellm.responses.utilsr   litellm.types.llms.openair   r   r   r   r   r    r!   r"   r#   r$   r   -litellm.litellm_core_utils.get_litellm_paramsr%   litellm.secret_managers.mainr&   litellm.types.responses.mainlitellm.types.routerr(   litellm.utilsr)   r*   	mcp.typesr+   MCPToolstreaming_iteratorr-   r2  r-  r   r=   rr   intfloatTimeoutr   r5   r   tupler  r  r  r   DeleteResponseResultr<  r@  rM  rN  rW  rX  r\  r]  ra  rb  rg  rt  rp   r>   r9   <module>r     s            + . B S R I =
 
 
 6L L 7 * 5 7)G A +, ,R,T )V# V4 V o11n 37"&'+%))-*.*.%) !%!#'%)(,+/!8< /3,0+/59)-9U(()UU d-./U 3-	U
  }U \"U tCH~&U "$U #3-U 	"U D>U U TNU %U >
"U  *%!U" HY'(#U$ E?%U& !345'U( 3-)U. DcN+/U0 $sCx.)1U2 c3h(3U4 eE5==0125U8 "#9U< !BBC=Up  37"&'+%))-*.*.%) !%!#'%)<@(,+/!8<"&'+ /3,0+/59)-?j
(()j
j
 d-./j
 3-	j

  }j
 \"j
 tCH~&j
 "$j
 #3-j
 	"j
 D>j
 j
 TNj
 %j
 >
"j
  %[ 14 789!j
" *%#j
$ HY'(%j
& E?'j
( !345)j
* 3-+j
, 3--j
.  }/j
4 DcN+5j
6 $sCx.)7j
8 c3h(9j
: eE5==012;j
> "#?j
B !BBCCj
 j
Z6-(()6-6- "#6- ""34	6-
 cN6- S#X6- 5(()3=>6-r&&!#& )& S#X	&
 3&2"(()"HY'(" cN" S#X	"
 5(()8HY4G+HHI"J  37"&'+%))-*.*.%) !%!#'%)<@(,+/!8<"&'+ /3,0+/5915)-Aj
(()j
j
 d-./j
 3-	j

  }j
 \"j
 tCH~&j
 "$j
 #3-j
 	"j
 D>j
 j
 TNj
 %j
 >
"j
  %[ 14 789!j
" *%#j
$ HY'(%j
& E?'j
( !345)j
* 3-+j
, 3--j
.  }/j
4 DcN+5j
6 $sCx.)7j
8 c3h(9j
: eE5==012;j
> $DI.?j
@ "#Aj
 j
Z	 
 /3,0+/59)-=
=
 DcN+	=

 $sCx.)=
 c3h(=
 eE5==012=
 "#=
 =
 =
@ 
 /3,0+/59)-\
\
 DcN+	\

 $sCx.)\
 c3h(\
 eE5==012\
 "#\
 35I+I!JJK\
 \
~ 
 /3,0+/59)-K
K
 DcN+	K

 $sCx.)K
 c3h(K
 eE5==012K
 "#K
 K
 K
\ 
 /3,0+/59)-j
j
 DcN+	j

 $sCx.)j
 c3h(j
 eE5==012j
 "#j
 35I+I!JJKj
 j
Z    #'$*.259)-:
:
C=:
 SM:
 d3i 	:

 :
 =!:
 DcN+:
 eE5==012:
 "#:
 
:
 :
z    #'$*.259)-R
R
C=R
 SMR
 d3i 	R

 R
 =!R
 DcN+R
 eE5==012R
 "#R
 43T>**+R
 R
j 
 /3,0+/59)-=
=
 DcN+	=

 $sCx.)=
 c3h(=
 eE5==012=
 "#=
 =
 =
@ 
 /3,0+/59)-\
\
 DcN+	\

 $sCx.)\
 c3h(\
 eE5==012\
 "#\
 35I+I!JJK\
 \
~  #'*. /3,0+/59)-J
(()J
J
 3-J
 #3-	J
 DcN+J
 $sCx.)J
 c3h(J
 eE5==012J
 "#J
 J
 J
Z  #'*. /3,0+/59)-J
(()J
J
 3-J
 #3-	J
 DcN+J
 $sCx.)J
 c3h(J
 eE5==012J
 "#J
 35I+I!JJKJ
 J
d	4 	D 	  #!#QQQ smQ c]	Q
 e_Q Qr>   