
    h#                     @    d dl mZmZmZmZmZ d dlmZ  G d d      Zy)    )AnyDictListOptionalType)verbose_proxy_loggerc                      e Zd ZdZg dZg dZddgZedee	e
ef      fd       Zede	e
ef   d	e
d
e	e
ef   ddfd       Zede	e
ef   dee
   de
ddfd       Zede	e
ef   ded	e
dee
   de
de	e
ef   fd       Zede	e
ef   de	e
ef   fd       Zede	e
ef   de	e
ef   fd       Zede	e
ef   de	e
ef   fd       Zede	e
ef   de	e
ef   fd       Zy)CustomOpenAPISpecz
    Handler for customizing OpenAPI specifications with Pydantic models
    for documentation purposes without runtime validation.
    )z/v1/chat/completionsz/chat/completionsz!/engines/{model}/chat/completionsz,/openai/deployments/{model}/chat/completions)z/v1/embeddingsz/embeddingsz/engines/{model}/embeddingsz&/openai/deployments/{model}/embeddingsz/v1/responsesz
/responsesreturnc                     	 | j                         S # t        $ r$ 	 | j                         cY S # t        $ r Y Y yw xY ww xY w)z
        Get JSON schema from a Pydantic model, handling both v1 and v2 APIs.
        
        Args:
            model_class: Pydantic model class
            
        Returns:
            JSON schema dict or None if failed
        N)model_json_schemaAttributeErrorschema)model_classs    j/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/proxy/common_utils/custom_openapi_spec.pyget_pydantic_schemaz%CustomOpenAPISpec.get_pydantic_schema   sK    		0022 	"))++! 		s     	?.?	;?;?openapi_schemaschema_name
schema_defNc                 J    d| vri | d<   d| d   vri | d   d<   || d   d   |<   y)a  
        Add a schema definition to the OpenAPI components/schemas section.
        
        Args:
            openapi_schema: The OpenAPI schema dict to modify
            schema_name: Name for the schema component
            schema_def: The schema definition
        
componentsschemasN )r   r   r   s      r   add_schema_to_componentsz*CustomOpenAPISpec.add_schema_to_components5   sJ     ~-+-N<(N<8868N<(3 @J|$Y/<    paths
schema_refc                     |D ]9  }|| j                  di       v sd| d   |   v s#dddd|iiid| d   |   d   d<   ; y	)
a*  
        Add request body schema reference to specified paths.
        
        Args:
            openapi_schema: The OpenAPI schema dict to modify
            paths: List of paths to update
            schema_ref: Reference to the schema component (e.g., "#/components/schemas/ModelName")
        r   postTzapplication/jsonr   z$ref)requiredcontentrequestBodyN)get)r   r   r   paths       r   add_request_body_to_pathsz+CustomOpenAPISpec.add_request_body_to_pathsH   so     D~))'2666^T[E\]aEb;b $*$ &
'- 	Hw'-f5mD r   r   operation_namec           	      v   	 t         j                  |      }|Lt         j                  | ||       t         j                  | |d|        t	        j
                  d| d       | S t	        j
                  d|        	 | S # t        $ r/}t	        j
                  d| dt        |              Y d}~| S d}~ww xY w)a  
        Generic method to add a request schema to OpenAPI specification.
        
        Args:
            openapi_schema: The OpenAPI schema dict to modify
            model_class: The Pydantic model class to get schema from
            schema_name: Name for the schema component
            paths: List of paths to add the request body to
            operation_name: Name of the operation for logging (e.g., "chat completion", "embedding")
            
        Returns:
            Modified OpenAPI schema
        Nz#/components/schemas/zSuccessfully added z schema to OpenAPI speczCould not get schema for zFailed to add z request schema: )r
   r   r   r%   r   debug	Exceptionstr)r   r   r   r   r&   request_schemaes          r   add_request_schemaz$CustomOpenAPISpec.add_request_schema_   s    *	c.BB;ON )!::>;Xfg ";;"+K=9 %**-@Md+ef  %**-F{m+TU 	  	c &&7GGXY\]^Y_X`'abb		cs   A!B  %B   	B8	$B33B8c                     	 ddl m} t        j                  | |dt        j                  d      S # t
        $ r-}t        j                  dt        |              | cY d}~S d}~ww xY w)aI  
        Add ProxyChatCompletionRequest schema to chat completion endpoints for documentation.
        This shows the request body in Swagger without runtime validation.
        
        Args:
            openapi_schema: The OpenAPI schema dict to modify
            
        Returns:
            Modified OpenAPI schema
        r   )ProxyChatCompletionRequestr/   zchat completionr   r   r   r   r&   z-Failed to import ProxyChatCompletionRequest: N)	litellm.proxy._typesr/   r
   r-   CHAT_COMPLETION_PATHSImportErrorr   r(   r*   )r   r/   r,   s      r   "add_chat_completion_request_schemaz4CustomOpenAPISpec.add_chat_completion_request_schema   sm    	"G$77-68'==0 8    	" &&)VWZ[\W]V^'_`!!	"   -0 	A&"A!A&!A&c                     	 ddl m} t        j                  | |dt        j                  d      S # t
        $ r-}t        j                  dt        |              | cY d}~S d}~ww xY w)a9  
        Add EmbeddingRequest schema to embedding endpoints for documentation.
        This shows the request body in Swagger without runtime validation.
        
        Args:
            openapi_schema: The OpenAPI schema dict to modify
            
        Returns:
            Modified OpenAPI schema
        r   )EmbeddingRequestr7   	embeddingr0   z#Failed to import EmbeddingRequest: N)	litellm.types.embeddingr7   r
   r-   EMBEDDING_PATHSr3   r   r(   r*   )r   r7   r,   s      r   add_embedding_request_schemaz.CustomOpenAPISpec.add_embedding_request_schema   sj    	"@$77-,.'77* 8    	" &&)LSQRVH'UV!!	"r5   c                     	 ddl m} t        j                  | |dt        j                  d      S # t
        $ r-}t        j                  dt        |              | cY d}~S d}~ww xY w)aF  
        Add ResponsesAPIRequestParams schema to responses API endpoints for documentation.
        This shows the request body in Swagger without runtime validation.
        
        Args:
            openapi_schema: The OpenAPI schema dict to modify
            
        Returns:
            Modified OpenAPI schema
        r   )ResponsesAPIRequestParamsr=   zresponses APIr0   z,Failed to import ResponsesAPIRequestParams: N)	litellm.types.llms.openair=   r
   r-   RESPONSES_API_PATHSr3   r   r(   r*   )r   r=   r,   s      r    add_responses_api_request_schemaz2CustomOpenAPISpec.add_responses_api_request_schema   sm    	"K$77-57';;. 8    	" &&)UVYZ[V\U]'^_!!	"r5   c                     t         j                  |       } t         j                  |       } t         j                  |       } | S )a  
        Add LLM API request schema bodies to OpenAPI specification for documentation.
        
        Args:
            openapi_schema: The base OpenAPI schema
            
        Returns:
            OpenAPI schema with added request body schemas
        )r
   r4   r;   r@   )r   s    r   add_llm_api_request_schema_bodyz1CustomOpenAPISpec.add_llm_api_request_schema_body   s>     +MMn] +GGW +KKN[r   )__name__
__module____qualname____doc__r2   r:   r?   staticmethodr   r   r*   r   r   r   r   r%   r   r-   r4   r;   r@   rB   r   r   r   r
   r
      s   
O 	
 HT#s(^,D  * Jc3h Jc J_cdgildl_m Jrv J J$ $sCx. c `c hl  , ,S#X,, , Cy	,
 , 
c3h, ,\ "4S> "dSVX[S[n " "2 "T#s(^ "SRUX " "2 "c3h "DQTVYQYN " "2 S#X 4PSUXPX>  r   r
   N)	typingr   r   r   r   r   litellm._loggingr   r
   r   r   r   <module>rJ      s    2 2 1j jr   