o
    !i
                     @  s  d Z ddlmZ ddlmZ ddlmZmZ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mZmZ dd	lmZ dd
lmZmZmZmZmZmZmZ ddl m!Z!m"Z"m#Z# eeeedf Z$ee%e&eeedf Z'G dd deZ(e%e&ee)e&e*f e(e)e&ef f f Z+	 eG dd deZ,G dd deZ-G dd de#Z.ee(e.e)e&e*f e)e&ef f Z/G dd deZ0e0Z1G dd de0Z2G dd de#Z3G dd de-Z4G dd deZ5G dd  d eZ6e6Z7G d!d" d"e&eZ8G d#d$ d$eZ9e!d% Z:G d&d' d'e#d(d)Z;G d*d+ d+e9Z<G d,d- d-eZ=d.d/ Z>G d0d1 d1eZ?G d2d3 d3e?Z@G d4d5 d5e&eZAG d6d7 d7e#d(d)ZBG d8d9 d9e?ZCG d:d; d;eZDG d<d= d=eDZEG d>d? d?eDZFG d@dA dAeZGG dBdC dCeZHG dDdE dEe#d(d)ZIG dFdG dGe#d(d)ZJG dHdI dIeHZKG dJdK dKeHZLG dLdM dMeZMG dNdO dOeMZNeG dPdQ dQeZOG dRdS dSe#d(d)ZPG dTdU dUe#d(d)ZQG dVdW dWeZRG dXdY dYeRZSG dZd[ d[e#d(d)ZTG d\d] d]eZUe4V  G d^d_ d_eZWG d`da daeZXG dbdc dce#d(d)ZYG ddde dee#d(d)ZZG dfdg dgeZ[G dhdi dieZ\G djdk dkeZ]G dldm dmeZ^G dndo doeZ_G dpdq dqeZ`G drds dse&eZaG dtdu due#d(d)ZbG dvdw dwe#d(d)ZcG dxdy dye#d(d)ZdG dzd{ d{e#d(d)ZeG d|d} d}e#ZfG d~d de#d(d)ZgG dd de#ZhG dd de4ZiG dd de#ZjG dd de#ZkG dd deZlG dd deZmG dd deZnG dd deZodddddZpG dd dZqG dd dZrG dd deZsG dd deZtG dd deZuG dd deZvG dd deuZwG dd deZxdS )zSchemas for the LangSmith API.    )annotations)Iterator)datetime	timedeltatimezone)Decimal)Enum)Path)	AnnotatedAny
NamedTupleOptionalProtocolUnionruntime_checkable)UUID)	BaseModel
ConfigDictFieldPrivateAttr
StrictBoolStrictFloat	StrictInt)LiteralNotRequired	TypedDictNc                   @  "   e Zd ZU dZded< ded< dS )
Attachmenta  Annotated type that will be stored as an attachment if used.

    Examples:
        ```python
        from langsmith import traceable
        from langsmith.schemas import Attachment


        @traceable
        def my_function(bar: int, my_val: Attachment):
            # my_val will be stored as an attachment
            # bar will be stored as inputs
            return bar
        ```
    str	mime_typeUnion[bytes, Path]dataN__name__
__module____qualname____doc____annotations__ r(   r(   V/var/www/html/crm_dreinet/venv_linux/lib/python3.10/site-packages/langsmith/schemas.pyr   $   s   
 r   c                   @  s2   e Zd ZdZddddZddddZdddZdS )BinaryIOLikez$Protocol for binary IO-like objects.sizeintreturnbytesc                 C     dS )zRead function.Nr(   )selfr,   r(   r(   r)   readD      zBinaryIOLike.readr   offsetwhencec                 C  r0   )zSeek function.Nr(   )r1   r4   r5   r(   r(   r)   seekH   r3   zBinaryIOLike.seekc                 C  r0   )zGet value function.Nr(   r1   r(   r(   r)   getvalueL   r3   zBinaryIOLike.getvalueN)r+   )r,   r-   r.   r/   )r   )r4   r-   r5   r-   r.   r-   )r.   r/   )r#   r$   r%   r&   r2   r6   r8   r(   r(   r(   r)   r*   @   s
    r*   c                   @  s\   e Zd ZU dZded< eddZded< eddZded< eddZded	< e	d
d
dZ
dS )ExampleBasezExample base model.r   
dataset_idNdefaultOptional[dict[str, Any]]inputsoutputsmetadataT)frozenarbitrary_types_allowed)r#   r$   r%   r&   r'   r   r>   r?   r@   r   model_configr(   r(   r(   r)   r9   Q   s   
 r9   c                   @  s   e Zd ZU ded< ded< dS )_AttachmentDictr   r   r    r!   N)r#   r$   r%   r'   r(   r(   r(   r)   rD   \   s   
 rD   c                      s   e Zd ZU dZdZded< edd dZded	< edd
Zded< edd
Z	ded< edd
Z
ded< dZded< dZded< dZded< dZded< dZded<  fddZ  ZS )ExampleCreatez Example upload with attachments.NOptional[UUID]idc                   C     t tjS Nr   nowr   utcr(   r(   r(   r)   <lambda>j       zExampleCreate.<lambda>default_factoryr   
created_atr;   r=   r>   r?   r@   Optional[Union[str, list[str]]]splitz$Optional[dict[str, _AttachmentLike]]attachmentsFbooluse_source_run_ioOptional[list[str]]use_source_run_attachmentssource_run_idc                      t  jdi | dS zInitialize from dict.Nr(   super__init__r1   r!   	__class__r(   r)   r^   t      zExampleCreate.__init__)r#   r$   r%   r&   rG   r'   r   rQ   r>   r?   r@   rS   rT   rV   rX   rY   r^   __classcell__r(   r(   r`   r)   rE   f   s   
 rE   c                   @  s   e Zd ZU dZded< dS )ExampleUpsertWithAttachmentsz Example create with attachments.r   r:   Nr"   r(   r(   r(   r)   rd   |   s   
 rd   c                   @  *   e Zd ZU dZded< ded< ded< dS )	AttachmentInfozInfo for an attachment.r   presigned_urlr*   readerOptional[str]r   Nr"   r(   r(   r(   r)   rf      
   
 rf   c                      s   e Zd ZU dZded< edd dZded< eed	d
Zded< edd
Z	ded< dZ
ded< edd
Zded< 	 edd
Zded< edd
Zded< 		d d! fddZed"ddZdd Z  ZS )#ExamplezExample model.r   rG   c                   C  s   t jdtjdS )Nr   )tz)r   fromtimestampr   rL   r(   r(   r(   r)   rM      s    zExample.<lambda>rO   r   rQ   z$00000000-0000-0000-0000-000000000000r;   r:   NOptional[datetime]modified_atrF   rY   z#Optional[dict[str, AttachmentInfo]]rT   ri   	_host_url
_tenant_idkwargsr   r.   Nonec                   s"   t  jdi | || _|| _dS )Initialize a Dataset object.Nr(   )r]   r^   rp   rq   )r1   rp   rq   rr   r`   r(   r)   r^      s   
zExample.__init__c                 C  sL   | j r$d| j d| j }| jr| j  dt| j | S | j  | S dS )URL of this run within the app.
/datasets/z/e//o/N)rp   r:   rG   rq   r   )r1   pathr(   r(   r)   url   s   zExample.urlc                 C  $   | j  d| j d| j d| j dS )5Return a string representation of the RunBase object.(id=z, dataset_id=z, link=''))ra   rG   r:   ry   r7   r(   r(   r)   __repr__      $zExample.__repr__)NN)rp   ri   rq   rF   rr   r   r.   rs   r.   ri   )r#   r$   r%   r&   r'   r   rQ   r   r:   ro   rY   rT   r   rp   rq   r^   propertyry   r~   rc   r(   r(   r`   r)   rk      s&   
 	rk   c                   @  s:   e Zd ZU dZeeddZded< eeddZ	ded< d	S )
AttachmentsOperationsz%Operations to perform on attachments.z,Mapping of old attachment names to new names)rP   descriptionzdict[str, str]renamez List of attachment names to keep	list[str]retainN)
r#   r$   r%   r&   r   dictr   r'   listr   r(   r(   r(   r)   r      s   
 r   c                      s   e Zd ZU dZded< dZded< eddZded	< eddZded
< eddZ	ded< dZ
ded< dZded< dZded< eddZ fddZ  ZS )ExampleUpdatez Example update with attachments.r   rG   NrF   r:   r;   r=   r>   r?   r@   rR   rS   zOptional[Attachments]rT   zOptional[AttachmentsOperations]attachments_operationsTrA   c                   rZ   r[   r\   r_   r`   r(   r)   r^      rb   zExampleUpdate.__init__)r#   r$   r%   r&   r'   r:   r   r>   r?   r@   rS   rT   r   r   rC   r^   rc   r(   r(   r`   r)   r      s   
 
r   c                   @  s   e Zd ZdZdZdZdZdS )DataTypezEnum for dataset data types.kvllmchatN)r#   r$   r%   r&   r   r   r   r(   r(   r(   r)   r      s
    r   c                   @  s<   e Zd ZU dZded< dZded< dZded< ed	d
ZdS )DatasetBasezDataset base model.r   nameNri   r   zOptional[DataType]	data_typeTr   )	r#   r$   r%   r&   r'   r   r   r   rC   r(   r(   r(   r)   r      s   
 r   )remove_system_messagesconvert_to_openai_messageconvert_to_openai_toolremove_extra_fieldsextract_tools_from_runc                   @  r   )DatasetTransformationz#Schema for dataset transformations.r   rx   z%Union[DatasetTransformationType, str]transformation_typeNr"   r(   r(   r(   r)   r      s   
 r   F)totalc                      s   e Zd ZU dZded< ded< eddZded	< dZd
ed< dZd
ed< dZ	ded< dZ
ded< dZded< dZded< dZded< eddZded< eddZded< eddZded< 			d!d" fddZed#dd Z  ZS )$DatasetzDataset ORM model.r   rG   r   rQ   Nr;   rn   ro   Optional[int]example_countsession_countlast_session_start_timer=   inputs_schemaoutputs_schemaz%Optional[list[DatasetTransformation]]transformationsr@   ri   rp   rF   rq   _public_pathrr   r   r.   rs   c                   sT   d|v r| d|d< d|v r| d|d< t jdi | || _|| _|| _dS )rt   inputs_schema_definitionr   outputs_schema_definitionr   Nr(   )popr]   r^   rp   rq   r   )r1   rp   rq   r   rr   r`   r(   r)   r^     s   
zDataset.__init__c                 C  sV   | j r)| jr| j  | j S | jr | j  dt| j d| j S | j  d| j S dS )ru   rw   rv   N)rp   r   rq   r   rG   r7   r(   r(   r)   ry     s   zDataset.url)NNN)
rp   ri   rq   rF   r   ri   rr   r   r.   rs   r   )r#   r$   r%   r&   r'   r   ro   r   r   r   r   r   r   r@   r   rp   rq   r   r^   r   ry   rc   r(   r(   r`   r)   r      s*   
 r   c                   @  s&   e Zd ZU dZdZded< ded< dS )DatasetVersionz%Class representing a dataset version.NrW   tagsr   as_of)r#   r$   r%   r&   r   r'   r(   r(   r(   r)   r   '  s   
 r   c                   C  s   di iS )Nr@   r(   r(   r(   r(   r)   _default_extra.  s   r   c                   @  s"  e Zd ZU dZded< 	 ded< 	 ded< 	 ded< 	 d	Zd
ed< 	 eedZded< 	 d	Z	ded< 	 d	Z
ded< 	 d	Zded< 	 eedZded< 	 d	Zded< 	 d	Zded< 	 d	Zded< 	 d	Zded< 	 eedZded< 	 ed+d d!Zed,d"d#Zed-d%d&Zd'd( Zed)d*Zd	S ).RunBasea3  Base Run schema.

    A Run is a span representing a single unit of work or operation within your LLM app.
    This could be a single call to an LLM or chain, to a prompt formatting call,
    to a runnable lambda invocation. If you are familiar with OpenTelemetry,
    you can think of a run as a span.
    r   rG   r   r   r   
start_timerun_typeNrn   end_timerO   Optional[dict]extrari   error
serializedOptional[list[dict]]eventsr   r>   r?   rF   reference_example_idparent_run_idrW   r   z-Union[Attachments, dict[str, AttachmentInfo]]rT   r.   dict[str, Any]c                 C  s   | j du ri | _ | j di S zRetrieve the metadata (if any).Nr@   )r   
setdefaultr7   r(   r(   r)   r@   o  s   
zRunBase.metadatac                 C  s   | j dS )z"Retrieve the revision ID (if any).revision_id)r@   getr7   r(   r(   r)   r   v  s   zRunBase.revision_idOptional[float]c                 C  s   | j du rdS | j | j  S )zLatency in seconds.N)r   r   total_secondsr7   r(   r(   r)   latency{  s   
zRunBase.latencyc                 C  rz   )r{   r|   z, name='z', run_type='r}   )ra   rG   r   r   r7   r(   r(   r)   r~     r   zRunBase.__repr__T)rB   r.   r   )r.   rF   )r.   r   )r#   r$   r%   r&   r'   r   r   r   r   r   r   r   r   r>   r?   r   r   r   rT   r   r@   r   r   r~   r   rC   r(   r(   r(   r)   r   2  sT   
 r   c                      s  e Zd ZU dZdZded< 	 dZded< 	 dZded< 	 dZd	ed
< 	 dZ	ded< 	 dZ
ded< 	 dZded< 	 dZded< 	 dZded< 	 dZded< 	 dZded< 	 dZded< 	 dZded< 	 dZded< 	 dZded< 	 dZded< 	 dZded< 	 dZded< 	 dZded< 	 d ed!< 	 ed"d#Zd$ed%< 	 dZd&ed'< 	 edd#Zded(< dEdF fd-d.ZedGd/d0ZedHd2d3Z edHd4d5Z!edId7d8Z"edId9d:Z#edJd<d=Z$edJd>d?Z%edKdAdBZ&edKdCdDZ'  Z(S )LRunz$Run schema when loading from the DB.NrF   
session_idOptional[list[UUID]]child_run_idszOptional[list[Run]]
child_runsr=   feedback_statsri   app_pathmanifest_idstatusr   prompt_tokenscompletion_tokenstotal_tokenszOptional[dict[str, int]]prompt_token_detailscompletion_token_detailsrn   first_token_timeOptional[Decimal]
total_costprompt_costcompletion_costzOptional[dict[str, Decimal]]prompt_cost_detailscompletion_cost_detailsparent_run_idsr   trace_id r;   r   dotted_orderOptional[bool]
in_datasetrp   rr   r   r.   rs   c                   s   | dsd| di|}|ddpi }t jdi |d|i || _| jjdu r5| jjtj	d| _| j
durI| j
jdu rI| j
jtj	d| _
| j s^| js`| j  | j | _dS dS dS )Initialize a Run object.r   rG   r>   Ntzinfor(   )r   r   r]   r^   rp   r   r   replacer   rL   r   r   stripr   	isoformatrG   )r1   rp   rr   r>   r`   r(   r)   r^     s   
zRun.__init__c                 C  s    | j r| jr| j  | j S dS )ru   N)rp   r   r7   r(   r(   r)   ry     s   zRun.url
int | Nonec                 C     | j S )zAlias for prompt_tokens.)r   r7   r(   r(   r)   input_tokens     zRun.input_tokensc                 C  r   )zAlias for completion_tokens.)r   r7   r(   r(   r)   output_tokens  r   zRun.output_tokensDecimal | Nonec                 C  r   )zAlias for prompt_cost.)r   r7   r(   r(   r)   
input_cost  r   zRun.input_costc                 C  r   )zAlias for completion_cost.)r   r7   r(   r(   r)   output_cost  r   zRun.output_costdict[str, int] | Nonec                 C  r   )zAlias for prompt_token_details.)r   r7   r(   r(   r)   input_token_details  r   zRun.input_token_detailsc                 C  r   )zAlias for output_token_details.)r   r7   r(   r(   r)   output_token_details  r   zRun.output_token_detailsdict[str, Decimal] | Nonec                 C  r   )zAlias for prompt_cost_details.)r   r7   r(   r(   r)   input_cost_details  r   zRun.input_cost_detailsc                 C  r   )z"Alias for completion_cost_details.)r   r7   r(   r(   r)   output_cost_details	  r   zRun.output_cost_detailsrI   rp   ri   rr   r   r.   rs   r   )r.   r   )r.   r   )r.   r   )r.   r   ))r#   r$   r%   r&   r   r'   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rp   r^   r   ry   r   r   r   r   r   r   r   r   rc   r(   r(   r`   r)   r     s   
 r   c                   @  s,   e Zd ZdZdZdZdZdZdZdZ	dZ
d	S )
RunTypeEnumz5(Deprecated) Enum for run types. Use string directly.toolchainr   	retriever	embeddingpromptparserN)r#   r$   r%   r&   r   r   r   r   r   r   r   r(   r(   r(   r)   r     s    r   c                   @  s   e Zd ZU dZded< ded< ded< ded	< ded
< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< d ed!< ded"< d#ed$< d%S )&RunLikeDictz&Run-like dictionary, for type-hinting.r   r   r   r   r   r   r   r>   r?   rn   r   r   ri   r   r   rF   r   r   r   r   rW   r   inputs_s3_urlsoutputs_s3_urlsrG   r   session_namer   input_attachmentsoutput_attachmentsr   r   r   AttachmentsrT   Nr"   r(   r(   r(   r)   r     s4   
 r   c                   @  s,   e Zd ZU dZdZded< 	 dZded< dS )RunWithAnnotationQueueInfoz&Run schema with annotation queue info.Nrn   last_reviewed_timeadded_at)r#   r$   r%   r&   r   r'   r   r(   r(   r(   r)   r   8  s   
 r   c                   @  sJ   e Zd ZU dZded< 	 eedZded< 	 dZded	< 	 dZ	d
ed< dS )FeedbackSourceBasezBase class for feedback sources.

    This represents whether feedback is submitted from the API, model, human labeler,
        etc.
    r   typerO   r=   r@   NzOptional[Union[UUID, str]]user_idri   	user_name)
r#   r$   r%   r&   r'   r   r   r@   r  r  r(   r(   r(   r)   r  A  s   
 r  c                   @     e Zd ZU dZdZded< dS )APIFeedbackSourcezAPI feedback source.apizLiteral['api']r  Nr#   r$   r%   r&   r  r'   r(   r(   r(   r)   r  R     
 r  c                   @  r  )ModelFeedbackSourcezModel feedback source.modelzLiteral['model']r  Nr  r(   r(   r(   r)   r
  X  r	  r
  c                   @  s   e Zd ZdZdZ	 dZdS )FeedbackSourceTypezFeedback source type.r  r  N)r#   r$   r%   r&   APIMODELr(   r(   r(   r)   r  ^  s    r  c                   @  s   e Zd ZU dZded< 	 dZded< 	 dZded< 	 ded	< 	 ded
< 	 ded< 	 dZded< 	 dZded< 	 dZ	ded< 	 dZ
ded< 	 dZded< 	 dZded< 	 dZded< 	 dZded< 	 dZded< 	 dZded< 	 eddZdS )FeedbackBasezFeedback schema.r   rG   Nrn   rQ   ro   rF   run_idr   r   key
SCORE_TYPEscore
VALUE_TYPEvalueri   commentzUnion[str, dict, None]
correctionOptional[FeedbackSourceBase]feedback_sourcer   r   comparative_experiment_idfeedback_group_idr   r   Tr   )r#   r$   r%   r&   r'   rQ   ro   r  r  r  r  r  r   r   r  r  r   r   rC   r(   r(   r(   r)   r  g  sF   
 r  c                   @  $   e Zd ZU dZded< 	 ded< dS )FeedbackCategoryz+Specific value and label pair for feedback.floatr  ri   labelNr"   r(   r(   r(   r)   r       
 r  c                   @  s8   e Zd ZU dZded< 	 ded< 	 ded< 	 ded< d	S )
FeedbackConfigz:Represents _how_ a feedback value ought to be interpreted.z0Literal['continuous', 'categorical', 'freeform']r  r   minmaxz Optional[list[FeedbackCategory]]
categoriesNr"   r(   r(   r(   r)   r!    s   
 r!  c                   @  s6   e Zd ZU dZded< 	 dZded< 	 dZded< dS )	FeedbackCreatez"Schema used for creating feedback.r  r  NzOptional[FeedbackConfig]feedback_configr   r   )r#   r$   r%   r&   r'   r&  r   r(   r(   r(   r)   r%    s   
 r%  c                   @  s:   e Zd ZU dZded< ded< 	 ded< 	 dZded	< dS )
FeedbackzSchema for getting feedback.r   rG   r   rQ   ro   Nr  r  )r#   r$   r%   r&   r'   r  r(   r(   r(   r)   r'    s   
 r'  c                      s   e Zd ZU dZded< 	 edd dZded< 	 d	Zd
ed< 	 d	Zded< 	 d	Z	ded< 	 d	Z
ded< 	 ded< 	 ded< 	 ed	dZded< d$d% fddZed&ddZed'dd Zed(d"d#Z  ZS ))TracerSessionzbTracerSession schema for the API.

    Sessions are also referred to as "Projects" in the UI.
    r   rG   c                   C  rH   rI   rJ   r(   r(   r(   r)   rM     rN   zTracerSession.<lambda>rO   r   r   Nrn   r   ri   r   r   r=   r   	tenant_idrF   reference_dataset_idr;   rp   rr   r   r.   rs   c                   s>   t  jdi | || _| jjdu r| jjtjd| _dS dS )r   Nr   r(   )r]   r^   rp   r   r   r   r   rL   )r1   rp   rr   r`   r(   r)   r^     s
   zTracerSession.__init__c                 C  s$   | j r| j  d| j d| j S dS )ru   rw   /projects/p/N)rp   r)  rG   r7   r(   r(   r)   ry     s   zTracerSession.urlr   c                 C  "   | j du s
d| j vri S | j d S r   r   r7   r(   r(   r)   r@        
zTracerSession.metadatar   c                 C  s"   | j du s
d| j vrg S | j d S )zRetrieve the tags (if any).Nr   r-  r7   r(   r(   r)   r     r.  zTracerSession.tagsrI   r   r   r   r.   r   )r#   r$   r%   r&   r'   r   r   r   r   r   r   r   rp   r^   r   ry   r@   r   rc   r(   r(   r`   r)   r(    s4   
 r(  c                   @  s   e Zd ZU dZdZded< 	 dZded< 	 dZded< 	 dZded< 	 dZ	ded	< 	 dZ
ded
< 	 dZded< 	 dZded< 	 dZded< 	 dZded< 	 dZded< 	 dZded< 	 dZded< 	 dZded< 	 dZded< 	 dZded< dS )TracerSessionResultzqA project, hydrated with additional information.

    Sessions are also referred to as "Projects" in the UI.
    Nr   	run_countOptional[timedelta]latency_p50latency_p99r   r   r   rn   last_run_start_timer=   r   session_feedback_statsOptional[list[dict[str, Any]]]
run_facetsr   r   r   r   first_token_p50first_token_p99r   
error_rate)r#   r$   r%   r&   r1  r'   r3  r4  r   r   r   r5  r   r6  r8  r   r   r   r9  r:  r;  r(   r(   r(   r)   r0    sD   
 r0  c                   @  s4   e Zd ZU dZded< 	 ded< 	 ed
ddZd	S )BaseMessageLikez7A protocol representing objects similar to BaseMessage.r   contentzdict[Any, Any]additional_kwargsr.   c                 C  r0   )z,Type of the Message, used for serialization.Nr(   r7   r(   r(   r)   r  $  s    zBaseMessageLike.typeNr.   r   )r#   r$   r%   r&   r'   r   r  r(   r(   r(   r)   r<    s   
 r<  c                   @  s.   e Zd ZU dZded< 	 ded< 	 ded< dS )DatasetShareSchemaz*Represents the schema for a dataset share.r   r:   share_tokenr   ry   Nr"   r(   r(   r(   r)   r@  )     
 r@  c                   @  sB   e Zd ZU dZded< 	 ded< 	 ded< 	 ded< 	 d	ed
< dS )AnnotationQueueRubricItemzRepresents a rubric item assigned to an annotation queue.

    Links a feedback config to a queue with optional per-queue customization.
    r   feedback_keyri   r   zOptional[dict[str, str]]value_descriptionsscore_descriptionsr   is_requiredNr"   r(   r(   r(   r)   rC  4  s   
 rC  c                   @  sl   e Zd ZU dZded< 	 ded< 	 dZded< 	 ed	d
 dZded< 	 edd
 dZded< 	 ded< dS )AnnotationQueuezRepresents an annotation queue.r   rG   r   r   Nri   r   c                   C  rH   rI   rJ   r(   r(   r(   r)   rM   O  rN   zAnnotationQueue.<lambda>rO   r   rQ   c                   C  rH   rI   rJ   r(   r(   r(   r)   rM   Q  rN   
updated_atr)  )	r#   r$   r%   r&   r'   r   r   rQ   rI  r(   r(   r(   r)   rH  F  s   
 rH  c                   @  s   e Zd ZU dZdZded< dS )AnnotationQueueWithDetailsz,Represents an annotation queue with details.Nri   rubric_instructions)r#   r$   r%   r&   rK  r'   r(   r(   r(   r)   rJ  W  s   
 rJ  c                   @  sL   e Zd ZU dZded< 	 ded< 	 ded< 	 ded< 	 ded< 	 d	ed
< dS )BatchIngestConfigz"Configuration for batch ingestion.rU   use_multipart_endpointr-   scale_up_qsize_triggerscale_up_nthreads_limitscale_down_nempty_trigger
size_limitr   size_limit_bytesNr"   r(   r(   r(   r)   rL  ^  s   
 rL  c                   @  sH   e Zd ZU dZdZded< 	 dZded< 	 dZded	< 	 dZd
ed< dS )LangSmithInfoz'Information about the LangSmith server.r   r   versionNrn   license_expiration_timezOptional[BatchIngestConfig]batch_ingest_configr=   instance_flags)	r#   r$   r%   r&   rT  r'   rU  rV  rW  r(   r(   r(   r)   rS  o  s   
 rS  c                   @  s<   e Zd ZU dZded< 	 ded< 	 ded< 	 dZded	< dS )
LangSmithSettingsz"Settings for the LangSmith tenant.r   rG   display_namer   rQ   Nri   tenant_handle)r#   r$   r%   r&   r'   rZ  r(   r(   r(   r)   rX  ~  s   
 rX  c                   @  .   e Zd ZU dZded< 	 ded< 	 ded< dS )	FeedbackIngestTokenz2Represents the schema for a feedback ingest token.r   rG   r   ry   r   
expires_atNr"   r(   r(   r(   r)   r\    rB  r\  c                   @  r[  )	RunEventzRun event schema.r   r   zUnion[datetime, str]timer=   rr   Nr"   r(   r(   r(   r)   r^    rB  r^  c                   @  .   e Zd ZU dZded< 	 ded< 	 ded< dS )TimeDeltaInputzTimedelta input schema.r-   dayshoursminutesNr"   r(   r(   r(   r)   ra    rB  ra  c                   @  r`  )DatasetDiffInfoz;Represents the difference information between two datasets.z
list[UUID]examples_modifiedexamples_addedexamples_removedNr"   r(   r(   r(   r)   re    rB  re  c                   @  s   e Zd ZU dZded< 	 dZded< 	 dZded< 	 ded< 	 d	ed
< 	 d	ed< 	 ded< 	 dZded< 	 dZded< 	 dZ	ded< 	 e
dddZdS )ComparativeExperimentzRepresents a comparative experiment.

    This information summarizes evaluation results comparing
    two or more models on a given dataset.
    r   rG   Nri   r   r   r)  r   rQ   ro   r*  r=   r   r   experiments_infor   r.   r   c                 C  r,  r   r-  r7   r(   r(   r)   r@     r.  zComparativeExperiment.metadatar   )r#   r$   r%   r&   r'   r   r   r   rj  r   r   r@   r(   r(   r(   r)   ri    s0   
 ri  c                   @  sP   e Zd ZU dZded< 	 ded< 	 ded< 	 ded< 	 ded	< 	 d
Zded< d
S )PromptCommitz$Represents a Prompt with a manifest.r   ownerrepocommit_hashr   manifestz
list[dict]examplesNri   r   )r#   r$   r%   r&   r'   r   r(   r(   r(   r)   rk    s   
 rk  c                   @  s   e Zd ZU dZded< 	 ded< 	 ded< 	 dZded	< 	 dZded
< 	 dZded< 	 dZded< 	 dZ	ded< 	 dZ
ded< 	 eedZded< 	 dZded< 	 dZded< 	 dZded< 	 dZded< dS )ListedPromptCommitz;Represents a listed prompt commit with associated metadata.r   rG   r   rl  rm  NrF   r   repo_id	parent_idri   rn  rn   rQ   rI  rO   r   example_run_idsr   r   num_downloads	num_viewsparent_commit_hashr   )r#   r$   r%   r&   r'   r   rr  rs  rn  rQ   rI  r   r   rt  ru  rv  rw  r   r(   r(   r(   r)   rq    s<   
 rq  c                   @  s  e Zd ZU dZded< 	 dZded< 	 dZded< 	 ded< 	 ded	< 	 d
ed< 	 d
ed< 	 ded< 	 ded< 	 ded< 	 dZded< 	 dZded< 	 ded< 	 ded< 	 ded< 	 ded< 	 ded< 	 dZ	ded< 	 dZ
ded< 	 ded< 	 dZded< 	 dZded< dS ) Promptz"Represents a Prompt with metadata.r   repo_handleNri   r   readmerG   r)  r   rQ   rI  rU   	is_publicis_archivedr   r   original_repo_idupstream_repo_idrl  	full_namer-   	num_likesru  rv  r   liked_by_auth_userlast_commit_hashnum_commitsoriginal_repo_full_nameupstream_repo_full_name)r#   r$   r%   r&   r'   r   rz  r}  r~  r  r  r  r  r(   r(   r(   r)   rx    s\   
 rx  c                   @  r  )ListPromptsResponsez A list of prompts with metadata.zlist[Prompt]reposr-   r   Nr"   r(   r(   r(   r)   r  J  r   r  c                   @  s&   e Zd ZdZdZ	 dZ	 dZ	 dZdS )PromptSortFieldz$Enum for sorting fields for prompts.ru  rv  rI  r  N)r#   r$   r%   r&   ru  rv  rI  r  r(   r(   r(   r)   r  S  s    r  c                   @  sL   e Zd ZU dZded< 	 ded< 	 ded< 	 ded< 	 ded< 	 ded< d	S )
InputTokenDetailsz~Breakdown of input token counts.

    Does *not* need to sum to full input token count. Does *not* need to have all keys.
    r-   audiocache_creation
cache_readcache_read_over_200kephemeral_5m_input_tokensephemeral_1h_input_tokensNr"   r(   r(   r(   r)   r  `  s   
 r  c                   @  $   e Zd ZU dZded< 	 ded< dS )OutputTokenDetailszBreakdown of output token counts.

    Does *not* need to sum to full output token count. Does *not* need to have all keys.
    r-   r  	reasoningNr"   r(   r(   r(   r)   r  {     
 r  c                   @  r`  )InputCostDetailszvBreakdown of input token costs.

    Does *not* need to sum to full input cost. Does *not* need to have all keys.
    r  r  r  r  Nr"   r(   r(   r(   r)   r    s   
 r  c                   @  r  )OutputCostDetailszxBreakdown of output token costs.

    Does *not* need to sum to full output cost. Does *not* need to have all keys.
    r  r  r  Nr"   r(   r(   r(   r)   r    r  r  c                   @  t   e Zd ZU dZded< 	 ded< 	 ded< 	 ded< 	 ded	< 	 d
ed< 	 d
ed< 	 d
ed< 	 ded< 	 ded< dS )UsageMetadatazUsage metadata for a message, such as token counts.

    This is a standard representation of token usage that is consistent across models.
    r-   r   r   r   zNotRequired[InputTokenDetails]r   zNotRequired[OutputTokenDetails]r   zNotRequired[float]r   r   r   zNotRequired[InputCostDetails]r   zNotRequired[OutputCostDetails]r   Nr"   r(   r(   r(   r)   r    s,   
 r  c                   @  r  )ExtractedUsageMetadatazUsage metadata dictionary extracted from a run.

    Should be the same as UsageMetadata, but does not require all
    keys to be present.
    r-   r   r   r   r  r   r  r   r  r   r   r   r  r   r  r   Nr"   r(   r(   r(   r)   r    s,   
 r  c                   @  r[  )	UpsertExamplesResponsezCResponse object returned from the upsert_examples_multipart method.r-   countr   example_idszNotRequired[str | None]r   Nr"   r(   r(   r(   r)   r    rB  r  c                   @  s$   e Zd ZU dZeedZded< dS )ExampleWithRunszExample with runs.rO   z	list[Run]runsN)r#   r$   r%   r&   r   r   r  r'   r(   r(   r(   r)   r    s   
 r  c                   @  s   e Zd ZU dZded< 	 ded< 	 ded< 	 ded< 	 ded< 	 ded	< 	 d
ed< 	 ded< 	 ded< 	 ded< 	 ded< 	 ded< 	 ded< 	 ded< dS )ExperimentRunStatsz!Run statistics for an experiment.r   r1  r2  r3  r4  r   r   r   rn   r5  r7  r8  r   r   r   r   r9  r:  r   r;  Nr"   r(   r(   r(   r)   r    s<   
 r  c                   @  r[  )	ExperimentResultszResults container for experiment data with stats and examples.

    Breaking change in v0.4.32:
        The 'stats' field has been split into 'feedback_stats' and 'run_stats'.
    r   r   r  	run_statszIterator[ExampleWithRuns]examples_with_runsNr"   r(   r(   r(   r)   r  #  s   
 r  c                   @  sf   e Zd ZU dZded< ded< ded< dZded	< ded
< ded< ded< edddZdddZdS )InsightsReportzHAn Insights Report created by the Insights Agent over a tracing project.
UUID | strrG   r   r   r   N
str | Noner   
project_idhost_urlr)  r.   c                 C  s.   | j  dt| j dt| j dt| j S )z1URL to view this Insights Report in LangSmith UI.rw   r+  z?tab=4&clusterJobId=)r  r   r)  r  rG   r7   r(   r(   r)   link<  s   .zInsightsReport.linkc                 C  s   d| j  d| j dS )Nz	<a href="z2", target="_blank" rel="noopener">InsightsReport('z')</a>)r  r   r7   r(   r(   r)   _repr_html_A  s   zInsightsReport._repr_html_r?  )	r#   r$   r%   r&   r'   r   r   r  r  r(   r(   r(   r)   r  1  s   
 r  c                   @  sN   e Zd ZU dZded< dZded< dZded< d	ed
< ded< dZded< dS )InsightsHighlightedTracez2A trace highlighted in an insights report summary.r  r  NUUID | str | None
cluster_idr  cluster_namer-   rankr   highlight_reasonsummary)r#   r$   r%   r&   r'   r  r  r  r(   r(   r(   r)   r  E  s   
 r  c                   @  sN   e Zd ZU dZeedZded< dZded< eedZ	ded	< dZ
d
ed< dS )InsightsSummaryReportzIHigh-level summary of an insights job: key points and highlighted traces.rO   r   
key_pointsNr  titlezlist[InsightsHighlightedTrace]highlighted_traceszdatetime | NonerQ   )r#   r$   r%   r&   r   r   r  r'   r  r  rQ   r(   r(   r(   r)   r  P  s   
 r  c                   @  s^   e Zd ZU dZded< dZded< ded< d	ed
< d	ed< dZded< ded< dZded< dS )InsightsClusterz/A single cluster of runs in an insights report.r  rG   Nr  rs  r-   levelr   r   r   r  parent_namenum_runsdict[str, Any] | Nonestats)r#   r$   r%   r&   r'   rs  r  r  r(   r(   r(   r)   r  Y  s   
 r  )r  clientr   r   
str | UUIDjob_idr  str | UUID | Noner.   list[dict[str, Any]]c                C  s   ddl m} g }d}d}|dur\||d}|durt||d< | jdd| d	| d
|d}	||	 |	 }
|
dg p>g }|| |
d}|du rPd}nt|}|sXd}|dus|S )zHFetch runs for an Insights job, optionally filtered to a single cluster.r   )utilsd   N)limitr4   r  GETz
/sessions/z
/insights/z/runs)paramsr  r4   )		langsmithr  r   request_with_retriesraise_for_status_with_textjsonr   extendr-   )r  r   r  r  ls_utilsall_runsr  next_offsetr  respbodybatchreturned_offsetr(   r(   r)   _fetch_insights_runsf  s2   



r  c                   @  sF   e Zd ZdZdZdd	d
ZdddZdddZdddZdddZ	dS )_ClusterWithTraceszYCluster wrapper that can load its traces from the API. Returned by report.clusters[name]._cluster_reportclusterr  reportInsightsReportResultr.   rs   c                 C  s   || _ || _d S rI   r  )r1   r  r  r(   r(   r)   r^     s   
z_ClusterWithTraces.__init__r   r   r   c                 C  s   t | j|S rI   )getattrr  )r1   r   r(   r(   r)   __getattr__  s   z_ClusterWithTraces.__getattr__r   c                 C  s    t tt| jtt| jB S rI   )r   setdirra   r  r7   r(   r(   r)   __dir__  s    z_ClusterWithTraces.__dir__c                 C  s   d| j jdS )Nz_ClusterWithTraces(name=))r  r   r7   r(   r(   r)   r~     s   z_ClusterWithTraces.__repr__r  c                 C  sf   t | jdd}|du rtdt | jdd}t | jdd}|du s%|du r)tdt|||| jjdS )z.Fetch run dicts for this cluster from the API._clientNzMReport not attached to a client; get reports via Client.get_insights_report()_session_id_job_idzReport missing session/job ids)r  r   r  r  )r  r  
ValueErrorr  r  rG   r1   r  r   r  r(   r(   r)   load_traces  s   z_ClusterWithTraces.load_tracesN)r  r  r  r  r.   rs   )r   r   r.   r   r/  r?  )r.   r  )
r#   r$   r%   r&   	__slots__r^   r  r  r~   r  r(   r(   r(   r)   r    s    



r  c                   @  sD   e Zd ZdZdZdd	d
ZdddZdd ZdddZdddZ	dS )_ClustersMapzWList-like map of clusters by name. Supports report.clusters['name'] and .load_traces().)	_clustersr  clusterslist[InsightsCluster]r  r  r.   rs   c                 C  s   t || _|| _d S rI   )r   r  r  )r1   r  r  r(   r(   r)   r^     s   

z_ClustersMap.__init__r  	str | intr  c                 C  s^   t |tr| j| }ndd | jD }||vr%td|dt|  || }t|| jS )Nc                 S  s   i | ]}|j |qS r(   )r   .0cr(   r(   r)   
<dictcomp>  s    z,_ClustersMap.__getitem__.<locals>.<dictcomp>zCluster z not found; available: )
isinstancer-   r  KeyErrorr   keysr  r  )r1   r  r  by_namer(   r(   r)   __getitem__  s   
z_ClustersMap.__getitem__c                   s    fdd j D S )Nc                 3  s    | ]	}t | jV  qd S rI   )r  r  r  r7   r(   r)   	<genexpr>  s    z(_ClustersMap.__iter__.<locals>.<genexpr>)r  r7   r(   r7   r)   __iter__  s   z_ClustersMap.__iter__r-   c                 C  s
   t | jS rI   lenr  r7   r(   r(   r)   __len__  s   
z_ClustersMap.__len__r   c                 C  s   dt | j dS )Nz_ClustersMap(n_clusters=r  r  r7   r(   r(   r)   r~     s   z_ClustersMap.__repr__N)r  r  r  r  r.   rs   )r  r  r.   r  )r.   r-   r?  )
r#   r$   r%   r&   r  r^   r  r  r  r~   r(   r(   r(   r)   r    s    


r  c                   @  s  e Zd ZU dZded< ded< ded< dZded	< dZded
< dZded< dZded< dZ	ded< dZ
ded< dZded< eedZded< dZded< eedZded< 	 eddZded< eddZded< eddZded< ed d!Zd/d(d)Zd0d+d,Zd0d-d.ZdS )1r  zVFull result of fetching an Insights report (job + clusters + summary + optional runs).r  rG   r   r   r   Nzdatetime | str | Noner   r   rQ   r  r@   r   shaper  r   r  	config_idrO   r  r  zInsightsSummaryReport | Noner  r  r  r;   r   r  r  r  allowr-  r  r   r  r  r.   rs   c                 C  sL   t | d| t | dt| t | dt| t | dt| j|  dS )zXAttach client and ids so clusters can load traces. Called by Client.get_insights_report.r  r  r  r  N)object__setattr__r   r  r  r  r(   r(   r)   _attach_client  s   
z#InsightsReportResult._attach_clientr   c                 C  sf   | j }t|drt| d|j z| jddW t|dr$t| d| S S t|dr2t| d| w w )z?Return the report as a JSON-serializable dict (raw JSON shape).r  r  r  mode)r  hasattrr  r  r  
model_dump)r1   clusters_valr(   r(   r)   to_json  s   

zInsightsReportResult.to_jsonc                 C  s   | j ddS )zHReturn the report as a JSON-serializable dict (e.g. for saving to file).r  r  )r  r7   r(   r(   r)   model_dump_json_dict  s   z)InsightsReportResult.model_dump_json_dict)r  r   r   r  r  r  r.   rs   r   )r#   r$   r%   r&   r'   r   r   rQ   r@   r  r   r  r   r   r  r  r  r   r  r  r  r   rC   r  r  r  r(   r(   r(   r)   r    s.   
 


r  c                   @  re   )	FeedbackFormulaWeightedVariablezBA feedback key and weight used when calculating feedback formulas.zLiteral['weighted_key']	part_typer  weightz#Annotated[str, Field(min_length=1)]r  Nr"   r(   r(   r(   r)   r    rj   r  c                   @  sP   e Zd ZU dZdZded< dZded< ded< ded	< ed
dddZded< dS )FeedbackFormulaCreatez,Schema used for creating a feedback formula.NrF   r:   r   r   rD  Literal['sum', 'avg']aggregation_type.   2   
min_length
max_length%list[FeedbackFormulaWeightedVariable]formula_parts)	r#   r$   r%   r&   r:   r'   r   r   r  r(   r(   r(   r)   r    s   
 r  c                   @  s8   e Zd ZU dZded< ded< edddd	Zd
ed< dS )FeedbackFormulaUpdatez,Schema used for updating a feedback formula.r   rD  r	  r
  .r  r  r  r  r  N)r#   r$   r%   r&   r'   r   r  r(   r(   r(   r)   r     s   
 r  c                   @  s*   e Zd ZU dZded< ded< ded< dS )FeedbackFormulaz%Schema for getting feedback formulas.r   rG   r   rQ   ro   Nr"   r(   r(   r(   r)   r  *  rj   r  c                   @  sF   e Zd ZU dZded< 	 ded< 	 ded< 	 ded	< 	 d
Zded< d
S )FeedbackConfigSchemaa  Represents a feedback configuration for a tenant's feedback key.

    Feedback configurations define how feedback with a given key should be
    interpreted, including its type (continuous, categorical, or freeform),
    scoring bounds, and valid categories.
    r   rD  r!  r&  r   r)  r   ro   Nr   is_lower_score_better)r#   r$   r%   r&   r'   r  r(   r(   r(   r)   r  2  s   
 r  )
r  r   r   r  r  r  r  r  r.   r  )yr&   
__future__r   collections.abcr   r   r   r   decimalr   enumr   pathlibr	   typingr
   r   r   r   r   r   r   uuidr   pydanticr   r   r   r   r   r   r   typing_extensionsr   r   r   r  r   r   r  r   tupler/   r   r*   r9   rD   _AttachmentLikerE   ExampleUploadWithAttachmentsrd   rf   rk   r   r   ExampleUpdateWithAttachmentsr   r   DatasetTransformationTyper   r   r   r   r   r   r   r   r   r  r  r
  r  r  r  r!  r%  r'  r(  r0  r<  r@  rC  rH  rJ  rL  rS  model_rebuildrX  r\  r^  ra  re  ri  rk  rq  rx  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r(   r(   r(   r)   <module>   s    $	$	&*
	0W 		)	
6($.1	"!	%&#8
