� ���g���v�UdZddlmZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddlm Z ddl mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&ddl'm(Z(m)Z)m*Z*m+Z+m,Z,ddl-m.Z/ddl-m0Z0m1Z1m2Z2dd l3m4Z5dd l6m7Z7er dd l8m9Z9dd l:m;Z;e j<e=��Z>ej?ee0j@d d���ZAej?eeBdd���ZCej?eeeBdd���ZDej?eeeBefdd���ZEej?ee#eFedfdd���ZGej?ee/jHdd���ZIeAeCeDeEeGeId�ZJdeKd<dZLd�d�ZM d�d�d�ZNejOddddddd �d�d0���ZPeMZQd�d3�ZRdddddddddd4� d�dA�ZSd�dD�ZTGdE�dFe!dG�H��ZUe"dIdJ�K��ZVe)dL��ZWe&GdM�dNeeeWeVf����ZXe%d�dO���ZYe% d�dddddddddddGdQ� d�dW���ZYd�dZ�ZYGd[�d\��ZZd�d]�Z[d�d`�Z\hda�Z]Gdb�dce!dG�H��Z^Gdd�dee!dG�H��Z_ d�d�dl�Z`d�dp�Za d�d�ds�Zb d�d�dv�Zc d�d�dx�Zdd�dy�Zed�d|�Zfd�d}�Zgd�d��Zhd�d��Ziejjd�����d�d����Zkd�d��Zld�d��Zmd�d�d��Znd�d��Zod�d��Zpe"d���ZqGd��d�eeq��ZrGd��d�ereeq��ZsGd��d�ereeq��Ztd�d��Zud�d��ZvdS)�z1Decorator for creating a run tree from functions.�)� annotationsN)� copy_context)� TYPE_CHECKING�Any�AsyncGenerator� AsyncIterator� Awaitable�Callable�Dict� Generator�Generic�Iterator�List�Literal�Mapping�Optional�Protocol�Sequence�Set�Tuple�Type� TypedDict�TypeVar�Union�cast�overload�runtime_checkable)� Annotated� ParamSpec� TypeGuard�get_args� get_origin��client)� run_trees�schemas�utils)�_aiter)� _runtime_env)� TracebackType)�Runnable�_PARENT_RUN_TREE��default� _PROJECT_NAME�_TAGS� _METADATA�local�_TRACING_ENABLED�_CLIENT��parent� project_name�tags�metadata�enabledr$z!Dict[str, contextvars.ContextVar]� _CONTEXT_KEYSzlangsmith/run_helpers.py�return�Optional[run_trees.RunTree]c�4�t���S)zGet the current run tree.)r,�get���e/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/langsmith/run_helpers.py�get_current_run_treerCPs�� � � � !� !�!rA�context�Optional[contextvars.Context]�Dict[str, Any]c�x�����t���t���t���t���t ���t ���d�S�fd�t���D��S)z Get the current tracing context.Nr5c�B��i|]\}}|��|����Sr@�r?)�.0�k�vrDs �rB� <dictcomp>z'get_tracing_context.<locals>.<dictcomp>bs)��� @� @� @�$�!�Q�A�w�{�{�1�~�~� @� @� @rA) r,r?r/r0r1r3r4r;�items�rDs`rB�get_tracing_contextrPUs������&�*�*�,�,�)�-�-�/�/��I�I�K�K�!� � ���'�+�+�-�-��k�k�m�m�  � � � A� @� @� @�-�*=�*=�*?�*?� @� @� @�@rA)r7r8r9r6r:r$r7� Optional[str]r8�Optional[List[str]]r9�Optional[Dict[str, Any]]r6�@Optional[Union[run_trees.RunTree, Mapping, str, Literal[False]]]r:�'Optional[Union[bool, Literal['local']]]r$�Optional[ls_client.Client]�kwargsr�Generator[None, None, None]c +��K�|rtjd|�d�t��t��}|dur&t d|p|�d��i��nd}|�Bt t|pg��t|jpg��z��}i|j �|pi�}|�|n|�d��}t||||||d��� dV�t|��dS#t|��wxYw) a�Set the tracing context for a block of code. Args: project_name: The name of the project to log the run to. Defaults to None. tags: The tags to add to the run. Defaults to None. metadata: The metadata to add to the run. Defaults to None. parent: The parent run to use for the context. Can be a Run/RunTree object, request headers (for distributed tracing), or the dotted order string. Defaults to None. client: The client to use for logging the run to LangSmith. Defaults to None, enabled: Whether tracing is enabled. Defaults to None, meaning it will use the current context value or environment variables. z Unrecognized keyword arguments: �.Fr6� parent_runNr:r5) �warnings�warn�DeprecationWarningrP�_get_parent_runr?�sorted�setr8r9�_set_tracing_context) r7r8r9r6r:r$rW�current_contextr[s rB�tracing_contextrdesG����4� �� � 8�v� 8� 8� 8� � � � �*�+�+�O� �� � � ��6�#E�V�Z�Z� �-E�-E�F�G�G�G� �� ���c�$�*�"�o�o��J�O�,A�r�(B�(B�B�C�C��>�j�)�>�h�n�"�>�� �,�g�g�/�2E�2E�i�2P�2P�G�� �(�� ���  � � � � �.� �����_�-�-�-�-�-���_�-�-�-�-���s �C&�&C7�func�'TypeGuard[SupportsLangsmithExtra[P, R]]c���t|��pQt|tj��ot|j��p#t |d��ot|j��S)z,Check if a function is @traceable decorated.�__call__)�_is_traceable_function� isinstance� functools�partialre�hasattrrh�res rB�is_traceable_functionro�s]�� �t�$�$� Q� �t�Y�.� /� /� U�4J�4�9�4U�4U� Q� �D�*� %� %� O�*@���*O�*O�rA� �namer9r8r$� reduce_fnr7�process_inputs�process_outputs� process_chunk�Callable[P, R]rq�Optional[Mapping[str, Any]]rr�$Optional[Callable[[Sequence], dict]]rs� Optional[Callable[[dict], dict]]rt�Optional[Callable[..., dict]]ru�Optional[Callable]�SupportsLangsmithExtra[P, R]c �f�t|��r|St||||||||| �� � |��S)z$Ensure that a function is traceable.rp)ro� traceable) rerqr9r8r$rrr7rsrtrus rB�ensure_traceabler�s^���T�"�"��� � �9� �� ���!�%�'�#� � � � � � �  rAr �boolc�|�tj|��p(t|d��otj|j��S)z;Inspect function or wrapped function to see if it is async.� __wrapped__)�inspect�iscoroutinefunctionrmr�rns rB�is_asyncr��s:�� � &�t� ,� ,� ���m�$�$�V��)D�T�EU�)V�)V�rAc��eZdZUdZded< ded< ded< ded < d ed < ded < d ed< ded< ded< ded< ded<dS)�LangSmithExtraz<Any additional info to be injected into the run dynamically.rQrq�Optional[ls_client.ID_TYPE]�reference_example_id�Optional[Dict]� run_extraz0Optional[Union[run_trees.RunTree, str, Mapping]]r6r=�run_treer7rSr9rRr8�run_idrVr$�,Optional[Callable[[run_trees.RunTree], Any]]�on_endN��__name__� __module__� __qualname__�__doc__�__annotations__r@rArBr�r��s��������F�F�����$�5�5�5�5�-�����.�<�<�<�<�D�)�)�)�)�)�����'�&�&�&�&�(�����,�'�'�'�'�"�&�&�&�&�$�8�8�8�8�D�DrAr�F)�total�RT)� covariant�Pc� �eZdZdZdd�d d �ZdS)�SupportsLangsmithExtraa`Implementations of this Protoc accept an optional langsmith_extra parameter. Args: *args: Variable length arguments. langsmith_extra (Optional[LangSmithExtra): Optional dictionary of additional parameters for Langsmith. **kwargs: Keyword arguments. Returns: R: The return type of the callable. N��langsmith_extra�args�P.argsr��Optional[LangSmithExtra]rW�P.kwargsr<r�c��dS)ajCall the instance when it is called as a function. Args: *args: Variable length argument list. langsmith_extra: Optional dictionary containing additional parameters specific to Langsmith. **kwargs: Arbitrary keyword arguments. Returns: R: The return value of the method. Nr@)�selfr�r�rWs rBrhzSupportsLangsmithExtra.__call__�s ��$ �rA)r�r�r�r�rWr�r<r�)r�r�r�r�rhr@rArBr�r��sA������ � �59� � � � � � � � rAr�c��dS�Nr@rns rBr~r~s ��$'�3rA�chain) rqr9r8r$rrr7rsrtru�_invocation_params_fn�dangerously_allow_filesystem�run_type�ls_client.RUN_TYPE_Tr�r��8Callable[[Callable[P, R]], SupportsLangsmithExtra[P, R]]c ��dSr�r@) r�rqr9r8r$rrr7rsrtrur�r�s rBr~r~s ��@C�srAr��/Union[Callable, Callable[[Callable], Callable]]c�������ttj|r#t|dt��r|dn|�dd��pd����t vr#tjd��dt �d��d���t|��d krtjd ��d |vrtjd t��|�d d���t|�d d��|�dd��|�dd��|�dd��|�dd��|�dd���|�dd��|�dd��|�dd��|�dd���� � �|�dd��}tj t|����|r=tjdt|������d�t��d ����fd� }t|��d kr(t#|d��r|s||d��S|S)!aTrace a function with langsmith. Args: run_type: The type of run (span) to create. Examples: llm, chain, tool, prompt, retriever, etc. Defaults to "chain". name: The name of the run. Defaults to the function name. metadata: The metadata to add to the run. Defaults to None. tags: The tags to add to the run. Defaults to None. client: The client to use for logging the run to LangSmith. Defaults to None, which will use the default client. reduce_fn: A function to reduce the output of the function if the function returns a generator. Defaults to None, which means the values will be logged as a list. Note: if the iterator is never exhausted (e.g. the function returns an infinite generator), this will never be called, and the run itself will be stuck in a pending state. project_name: The name of the project to log the run to. Defaults to None, which will use the default project. process_inputs: Custom serialization / processing function for inputs. Defaults to None. process_outputs: Custom serialization / processing function for outputs. Defaults to None. dangerously_allow_filesystem: Whether to allow filesystem access for attachments. Defaults to False. Traces that reference local filepaths will be uploaded to LangSmith. In general, network-hosted applications should not be using this because referenced files are usually on the user's machine, not the host machine. Returns: Union[Callable, Callable[[Callable], Callable]]: The decorated function. Note: - Requires that LANGSMITH_TRACING_V2 be set to 'true' in the environment. Examples: Basic usage: .. code-block:: python @traceable def my_function(x: float, y: float) -> float: return x + y my_function(5, 6) @traceable async def my_async_function(query_params: dict) -> dict: async with httpx.AsyncClient() as http_client: response = await http_client.get( "https://api.example.com/data", params=query_params, ) return response.json() asyncio.run(my_async_function({"param": "value"})) Streaming data with a generator: .. code-block:: python @traceable def my_generator(n: int) -> Iterable: for i in range(n): yield i for item in my_generator(5): print(item) Async streaming data: .. code-block:: python @traceable async def my_async_generator(query_params: dict) -> Iterable: async with httpx.AsyncClient() as http_client: response = await http_client.get( "https://api.example.com/data", params=query_params, ) for item in response.json(): yield item async def async_code(): async for item in my_async_generator({"param": "value"}): print(item) asyncio.run(async_code()) Specifying a run type and name: .. code-block:: python @traceable(name="CustomName", run_type="tool") def another_function(a: float, b: float) -> float: return a * b another_function(5, 6) Logging with custom metadata and tags: .. code-block:: python @traceable( metadata={"version": "1.0", "author": "John Doe"}, tags=["beta", "test"] ) def tagged_function(x): return x**2 tagged_function(5) Specifying a custom client and project name: .. code-block:: python custom_client = Client(api_key="your_api_key") @traceable(client=custom_client, project_name="My Special Project") def project_specific_function(data): return data project_specific_function({"data": "to process"}) Manually passing langsmith_extra: .. code-block:: python @traceable def manual_extra_function(x): return x**2 manual_extra_function(5, langsmith_extra={"metadata": {"version": "1.0"}}) rr�Nr�zUnrecognized run_type: z. Must be one of: z . Did you mean @traceable(name='z')?�z�The `traceable()` decorator only accepts one positional argument, which should be the run_type. All other arguments should be passed as keyword arguments.�extrazJThe `extra` keyword argument is deprecated. Please use `metadata` instead.rrrqr9r8r$r7rsrur�r�F) � extra_outerrqr9r8r$r7r�rsru�invocation_params_fnr�rt)�outputs_processorzHThe following keyword arguments are not recognized and will be ignored: rZrer c ���� � �tj���}|j�dd��du� |j�dd��du� t j���dd�d� � �� � fd ���}t j���dd�d� � �� � �fd ���}t j���dd�d� � �� fd ���}t j���dd�d� � �� ��fd���}t j���dd�d� � �� � �fd���}t j���dd�d� � �� � �fd���}tj���r|}n5tj���r|}nt���r�r|}n |}n�r|}n|}t|dd��tj|��} | j�d��s�| � gd�| j� ��D���tj dtj j d����d�| j� ��D������} | |_|S)Nr��configr�r�rr�r�rWr<c��~�K�� s|�dd��tjt� � |||����d{V��} tj��}� r |d|d<� |i|��}|r#t j||d����d{V��}n?td it|d����5|�d{V��}ddd��n #1swxYwYnQ#t$rD}t|��t j tj�||������d{V���d}~wwxYwtj�||� ���d{V��|S) z"Async version of wrapper function.r�N��container_inputr�r�rW�new_runr�rDrO��error��outputsr@) �pop� aitertools� aio_to_thread� _setup_run�asyncio_accepts_context�asyncio� create_taskrdrP� BaseException�_cleanup_traceback�shield) r�r�rW� run_container�accepts_context�fr_coro�function_result�e� _on_run_endr�re�func_accepts_config�func_accepts_parent_runs �����rB� async_wrapperz3traceable.<locals>.decorator.<locals>.async_wrapper�sM�����'� +�� � �8�T�*�*�*�",�":��� /� /��� #�#�#�������M� �",�"D�"F�"F��*�B�)6�y�)A�F�:�&��$��/��/�/��"� 8�,3�,?���y�)A�-�-�-�'�'�'�'�'�'�O�O� )���-�m�I�.F�G�G���8�8�18�-�-�-�-�-�-��8�8�8�8�8�8�8�8�8�8�8����8�8�8�8���!� � � �"�1�%�%�%��n��,�[�-�q�Q�Q�Q���������������  �����*��]�O���� � � � � � � �#� "s=�A,C�. C�7 C�C�C� C� C� D�?D�Drc ��K�� s|�dd��tjt� � |||����d{V��}g} � r |d|d<� |i|��}tj��}t j|��rd|r#tj||d����d{V��}n?td it|d����5|�d{V��}ddd��n #1swxYwYt|||dr|dj dknd||� � d ��� ��23d{V��}|WV�� 6n`#t$rS}t|��tjtj� ||t#|���� �����d{V���d}~wwxYwtj� |t#|���� ���d{V��dS)Nr�r�r�r�rDrO�llmFru�� generatorr�� is_llm_runr��resultsru�r�r�r�r@)r�r�r�r�r�r�� iscoroutiner�r�rdrP�_process_async_iteratorr�r?r�r�r��_get_function_result)r�r�rWr�r��async_gen_resultr��itemr�r�r�rer�r�rrs ������rB�async_generator_wrapperz=traceable.<locals>.decorator.<locals>.async_generator_wrapper#s>�����'� +�� � �8�T�*�*�*�",�":��� /� /��� #�#�#�������M�"$�G�, �*�B�)6�y�)A�F�:�&�$(�4��#8��#8�#8� �",�"D�"F�"F���&�'7�8�8� F�&� F�18�1D�,�m�I�6N�2�2�2�,�,�,�,�,�,�(�(� -���1�-� �2J�K�K���F�F�6F�/E�/E�/E�/E�/E�/E�,�F�F�F�F�F�F�F�F�F�F�F����F�F�F�F� #:�.�"/�)��3�#� �i�0�9�U�B�B�"�$3�#�"1�"5�"5�o�"F�"F� #� #� #� � � � � � � �$��J�J�J�J�J� #� #��!� � � �"�1�%�%�%��n��,�#�%�� 4�W�i� H� H� ����������������� �����*���,�W�i�@�@���� � � � � � � � � sK�BD4� C� D4�C�D4� C�!AD4�%D2�+D4�4 F�>AF � Fc���� s|�dd��t� �|||���}tj� ��j�dd��du} |r |d|d<|dj� g|�Ri|��}n/#t$r"}t|���||����d}~wwxYw�||���|S) �>Create a new run or create_child() if run is passed in kwargs.r�Nr�r�r�rDr�r�) r�r�r�� signature� parametersr?�runr�r�) r�r�rWr�r�r�r�r�r�rer�s ����rB�wrapperz-traceable.<locals>.decorator.<locals>.wrapperes���'� +�� � �8�T�*�*�*�&�� /� /��� ���M��!�$�'�'�2�6�6�z�4�H�H�PT�T� $� �*�B�)6�y�)A�F�:�&�">�-� �":�">�t�"U�d�"U�"U�"U�f�"U�"U���� � � � �"�1�%�%�%�� �M��3�3�3�3������ ���� �K� �� ?� ?� ?� ?�"� "s�$B� B/� B*�*B/c ?�X�K�� s|�dd��t� � |||���}tj� ��j�dd��du}g}d} |r |d|d<|dj� g|�Ri|��}t||�dk|� �d�����Ed{V��}|�|�|��n>#t$r1}t|��� ||t|� ��� ���d}~wwxYw� |t|� ��� ��|S) Nr�r�r�r�rDr�ru�r�r�rur�r�) r�r�r�r�r�r?r��_process_iterator�appendr�r�r�)r�r�rWr�r�r��function_return�generator_resultr�r�r�rer�rrr�s ������rB�generator_wrapperz7traceable.<locals>.decorator.<locals>.generator_wrapper�s������'� +�� � �8�T�*�*�*�&�� /� /��� ���M��!�$�'�'�2�6�6�z�4�H�H�PT�T� $�"$�G�#'�O� �*�B�)6�y�)A�F�:�&�#?�=��#;�#?��#V�t�#V�#V�#V�v�#V�#V� �->�$�!�'�5�0�#�"1�"5�"5�o�"F�"F� .�.�.�#�#�#�#�#�#��#�.��N�N�?�3�3�3��� � � � �"�1�%�%�%�� �!��0��)�D�D����� ����� ���� �K� �/C�G�Y�/W�/W� X� X� X� X�"� "s�$A,C� D �,D�D c���� s|�dd��t��|||���} � r |d|d<|dj�g|�Ri|��}n/#t$r"}t |���||����d}~wwxYwt |d��rt ||� ��St |d��rt||� ��S�||� ��|S) Nr�r�r�r�rDr��__iter__� __aiter__r�)r�r�r�� Exceptionr�rm� _TracedStream�_TracedAsyncStream� r�r�rW�trace_container�streamr�r�r�rer�r�rrs ������rB�stream_wrapperz4traceable.<locals>.decorator.<locals>.stream_wrapper�s7���'� +�� � �8�T�*�*�*�(�� /� /��� ���O� �*�D�)8��)C�F�:�&�7���3�7��N�t�N�N�N�v�N�N����� � � �"�1�%�%�%�� �O�1�5�5�5�5������ ���� �v�z�*�*� N�$�V�_�i�H�H�H����-�-� N�)�&�/�9�M�M�M� �K��� 8� 8� 8� 8��Ms�$A� B�A;�;Bc����K�� s|�dd��tjt��|||����d{V��} � r |d|d<�|i|���d{V��}n0#t$r#}tj�||����d{V���d}~wwxYwt |d��rt ||� ��St |d��rt||� ��Stj�||����d{V��|S) Nr�r�r�r�r�r�r�r�)r�r�r�r�r�rmr�r�r�s ������rB�async_stream_wrapperz:traceable.<locals>.decorator.<locals>.async_stream_wrapper�s|�����'� +�� � �8�T�*�*�*�$.�$<��� /� /��� %�%�%�������O� �*�D�)8��)C�F�:�&�#�t�T�4�V�4�4�4�4�4�4�4�4����� � � � �.�{�O�ST�U�U�U�U�U�U�U�U�U�U������ �����v�{�+�+� I�)�&�/�9�M�M�M����,�,� I�$�V�_�i�H�H�H��*�;��QW�X�X�X� X� X� X� X� X� X� X��Ms�A� B �(B�B �__langsmith_traceable__Tc3�NK�|] }|jtjjk�|V��!dSr���kindr�� Parameter� VAR_KEYWORD�rJ�params rB� <genexpr>z/traceable.<locals>.decorator.<locals>.<genexpr> �A������!� �:��):�)F�F�F��F�F�F�F��rAr-c3�NK�|] }|jtjjk�|V��!dSr�r�r�s rBr�z/traceable.<locals>.decorator.<locals>.<genexpr>rrA)r�)r�rr�r�rWrr<r)r�rr�r�rWrr<r)r�r�r�r?rk�wraps�isasyncgenfunction�isgeneratorfunctionr��setattr�replace�valuesr�� KEYWORD_ONLY� __signature__)re�func_sigr�r�r�r�r�r��selected_wrapper�sigr�r�r�r�rrr�s` @@����rB� decoratorztraceable.<locals>.decorator�s��������$�T�*�*��"*�"5�"9�"9�*�d�"K�"K�SW�"W��&�1�5�5�h��E�E�T�Q�� ��� � �9=�* #�* #�* #�* #�* #�* #�* #�* #�* #�* #�* #� � �* #�X ��� � �DH�? �? �? �? �? �? �? �? �? �? �? �? � � �? �B ��� � �9=� #� #� #� #� #� #� #� #� #� #� � � #�: ��� � �DH�, #�, #�, #�, #�, #�, #�, #�, #�, #�, #�, #�, #� � �, #�b ��� � �DH� � � � � � � � � � � � � � � �@ ��� � �DH� � � � � � � � � � � � � � � �@ � %�d� +� +� +�)@� � � � (�� .� .� +�0� � � �d�^�^� +�� 1�#7� � �#0� � �� +�#1� � �#*� �� �";�T�B�B�B��� 0�1�1���~�!�!�(�+�+� 1��+�+����%(�^�%:�%:�%<�%<����� �%� �'�"3�"@�$���� ���%(�^�%:�%:�%<�%<��������C�".1� � *��rA)rer )r� ls_client� RUN_TYPE_Trj�strr��_VALID_RUN_TYPESr\r]�lenr^�_ContainerInputrkrl�_handle_container_endr`�keys�callable)r�rWr�r r�r�rrr�s @@@@rBr~r~*s�������f���� ;�"�4��7�C�0�0� ;�D��G�G��*�*�Z��.�.�9�'� ��H��'�'�'�� � <�h� <� <�BR� <� <�.6� <� <� <� � � � �4�y�y�1�}�}�� � $� � � � �&���� � � � � � � � � �;��-�-�I�%��J�J�w��-�-� �Z�Z��� %� %����J��-�-� �Z�Z��� %� %��z�z�(�D�)�)��Z�Z���5�5���z�z�"2�D�9�9��j�j��$�7�7�#�Z�Z�(?��F�F�%+�Z�Z�0N�PU�%V�%V� � � �O�� � �#4�d�;�;���#��1B����K�� �� � (��f�k�k�m�m�$�$� (� (� (� � � � � i �i �i �i �i �i �i �i �i �Z  �4�y�y�A�~�~�(�4��7�+�+�~�F�~��y��a��!�!�!� �rAc �x�eZdZdZ d/dddddddddddd� d0d�Zd1d"�Zd2d*�Zd1d+�Z d3d2d,�Zd1d-�Z d3d2d.�Z dS)4�tracea� Manage a LangSmith run in context. This class can be used as both a synchronous and asynchronous context manager. Args: name (str): Name of the run. run_type (ls_client.RUN_TYPE_T, optional): Type of run (e.g., "chain", "llm", "tool"). Defaults to "chain". inputs (Optional[Dict], optional): Initial input data for the run. Defaults to None. project_name (Optional[str], optional): Project name to associate the run with. Defaults to None. parent (Optional[Union[run_trees.RunTree, str, Mapping]], optional): Parent run. Can be a RunTree, dotted order string, or tracing headers. Defaults to None. tags (Optional[List[str]], optional): List of tags for the run. Defaults to None. metadata (Optional[Mapping[str, Any]], optional): Additional metadata for the run. Defaults to None. client (Optional[ls_client.Client], optional): LangSmith client for custom settings. Defaults to None. run_id (Optional[ls_client.ID_TYPE], optional): Preset identifier for the run. Defaults to None. reference_example_id (Optional[ls_client.ID_TYPE], optional): Associates run with a dataset example. Only for root runs in evaluation. Defaults to None. exceptions_to_handle (Optional[Tuple[Type[BaseException], ...]], optional): Exception types to ignore. Defaults to None. extra (Optional[Dict], optional): Extra data to send to LangSmith. Use 'metadata' instead. Defaults to None. Examples: Synchronous usage: .. code-block:: python >>> with trace("My Operation", run_type="tool", tags=["important"]) as run: ... result = "foo" # Perform operation ... run.metadata["some-key"] = "some-value" ... run.end(outputs={"result": result}) Asynchronous usage: .. code-block:: python >>> async def main(): ... async with trace("Async Operation", run_type="tool", tags=["async"]) as run: ... result = "foo" # Await async operation ... run.metadata["some-key"] = "some-value" ... # "end" just adds the outputs and sets error to None ... # The actual patching of the run happens when the context exits ... run.end(outputs={"result": result}) >>> asyncio.run(main()) Handling specific exceptions: .. code-block:: python >>> import pytest >>> import sys >>> with trace("Test", exceptions_to_handle=(pytest.skip.Exception,)): ... if sys.platform == "win32": # Just an example ... pytest.skip("Skipping test for windows") ... result = "foo" # Perform test operation r�N) �inputsr�r7r6r8r9r$r�r��exceptions_to_handle� attachmentsrqrr�r�rr�r�r7rQr6�COptional[Union[run_trees.RunTree, str, Mapping, Literal['ignore']]]r8rRr9rwr$rVr�r�r�r�)Optional[Tuple[Type[BaseException], ...]]r�Optional[schemas.Attachments]rWrc  ���|�dd��|_|r=tjdt |������d�t ��||_||_||_ | |_ ||_ ||_ ||_ |�d��|_||_||_| |_| |_| |_| |_d|_d|_dS)z_Initialize the trace context manager. Warns if unsupported kwargs are passed. � _end_on_exitTzEThe `trace` context manager no longer supports the following kwargs: rZr�N)r�r r\r]r`rr^rqr�rrr�r7r6r?r�r8r9r$r�r�rr��old_ctx)r�rqr�rr�r7r6r8r9r$r�r�rrrWs rB�__init__ztrace.__init__Zs���0#�J�J�~�t�<�<��� � � �M�,��&�+�+�-�-�(�(�,�,�,�"� � � � �� � �� ��� �&����� �(����� �� � �:�.�.�� ��� � �� ��� ��� �$8��!�$8��!�48�� �'+�� � � rAr<�run_trees.RunTreec ��t��|_tj|j��}t���}t ���}|jp|j�d��}t|j |j |d���}tt|j pg|pgz����}i|jpi�|pi�ddi�}|jpi}||d<t!|j��} |�=|r;|�|j|j|j||j||j���|_nmt3j|jt7j|j��t7j|jd� ��|j|| pd |jpi|||jpi� � � |_|dur|j���|r�t� |��t � |��t>� |j��t@� | ��tB� |��|jS) a]Set up the tracing context and create a new run. This method initializes the tracing context, merges tags and metadata, creates a new run (either as a child of an existing run or as a new root run), and sets up the necessary context variables. Returns: run_trees.RunTree: The newly created run. r$)r6r�r$� ls_methodrr9N)rqr�r�r�rr8rT�� accept_nullr.) rq�idr�r�r�r7rr8r$r)"rPr!r'�tracing_is_enabledr0r?r1r$r_r6r�r`rar8r9r��_get_project_namer7� create_childrqr�r�rrr�r%�RunTreer� _ensure_uuidr��postr,r/r4) r�r:� outer_tags�outer_metadata�client_� parent_run_�tags_r9r�� project_name_s rB�_setupz trace._setup�sT��+�,�,�� ��*�4�<�8�8���Y�Y�[�[� �"�������+�;���!1�!1�(�!;�!;��%��+� �M�!� � � � � ��s�D�I�O�� �0@�b�A�B�B�C�C�� ��}�"�� ��#�� � �� � �� �j�&�B� �"*� �J��)�$�*;�<�<� � � "�w� "�&�3�3��Y��{���!��{�� �,�4���D�L�L�%�,��Y��)�$�+�6�6�%.�%;��-�4�&�&�&���!�*�7�i��{�(�b��� �,�2�� � � �D�L� �d�?�?� �L� � � � � � � !� �I�I�e� � � � �M�M�(� #� #� #� � � ��� .� .� .� � � �m� ,� ,� ,� �K�K�� � � ��|�rA�exc_type�Optional[Type[BaseException]]� exc_value�Optional[BaseException]� traceback�Optional[TracebackType]�Nonec��|j�dS|�\|jrt||j��rd}n"tj��}|j�d|�d|��}|j�|���|j�Stj|j��}|dur |j r|j� ��t|j��dStj dt��dS)a�Clean up the tracing context and finalize the run. This method handles exceptions, ends the run if necessary, patches the run if it's not disabled, and resets the tracing context. Args: exc_type: The type of the exception that occurred, if any. exc_value: The exception instance that occurred, if any. traceback: The traceback object associated with the exception, if any. N�: � r�Tz(Tracing context was not set up properly.)r�r� issubclassr'� _format_excr��endr!r)r �patchrbr\r]�RuntimeWarning)r�r6r8r:�tbr:s rB� _teardownztrace._teardown�s��� �<� � �F� � ��(� A�Z��$�3�.�.� A�����&�(�(�� �)�@�@�Y�@�@�B�@�@�� �L� � �2� � &� &� &� �<� #��.�t�|�<�<�G��$���4�#4��� �"�"�$�$�$� ��� .� .� .� .� .� �M�D�n� U� U� U� U� UrAc�*�|���S)zyEnter the context manager synchronously. Returns: run_trees.RunTree: The newly created run. )r5�r�s rB� __enter__ztrace.__enter__�s�� �{�{�}�}�rAc�4�|�|||��dS)aExit the context manager synchronously. Args: exc_type: The type of the exception that occurred, if any. exc_value: The exception instance that occurred, if any. traceback: The traceback object associated with the exception, if any. N)rF)r�r6r8r:s rB�__exit__ztrace.__exit__s �� ���x��I�6�6�6�6�6rAc��K�t��}tj|j|����d{V��}t t |����|S)zzEnter the context manager asynchronously. Returns: run_trees.RunTree: The newly created run. ��__ctxN)rr�r�r5rbrP)r��ctx�results rB� __aenter__ztrace.__aenter__sV���� �n�n��!�/�� �3�G�G�G�G�G�G�G�G�G���0��5�5�6�6�6�� rAc ��K�t��}|�7tjtj|j||||������d{V��n$tj|j||||����d{V��t t|����dS)aExit the context manager asynchronously. Args: exc_type: The type of the exception that occurred, if any. exc_value: The exception instance that occurred, if any. traceback: The traceback object associated with the exception, if any. NrM)rr�r�r�r�rFrbrP)r�r6r8r:rOs rB� __aexit__ztrace.__aexit__s������n�n�� � ��.��(��N�H�i��#������ � � � � � � � � �*����)�Y�c���� � � � � � � � �0��5�5�6�6�6�6�6rA�r�)rqrr�r�rr�r�r�r7rQr6rr8rRr9rwr$rVr�r�r�r�rrrrrWr)r<r#)r6r7r8r9r:r;r<r<�NNN) r�r�r�r�r"r5rFrIrKrQrSr@rArBrr$s������3�3�p*1�0,� "&� $�&*� �$(�04�-1�.2�<@�JN�59�#0,�0,�0,�0,�0,�0,�dG�G�G�G�R"V�"V�"V�"V�H����37�-1�-1� 7� 7� 7� 7� 7� � � � �37�-1�-1� 7�7�7�7�7�7�7rArc��|r|St���}t���p|r|jndpt j��Sr�)r,r?r/� session_namer'�get_tracer_project)r7�prts rBr*r*7s_������ � � � � �C� ����� &� #� -�C� � �� &� � #� %� %� rA� traceable_fnr+c�B���� ddlm�m}ddlm�m�n"#t $r}t d��|�d}~wwxYwt|��s= tj |��}n#t$rd}YnwxYwtd|�d|�����G���fd �d |��}||��S) a�Convert a function wrapped by the LangSmith @traceable decorator to a Runnable. Args: traceable_fn (Callable): The function wrapped by the @traceable decorator. Returns: Runnable: A Runnable object that maintains a consistent LangSmith tracing context. Raises: ImportError: If langchain module is not installed. ValueError: If the provided function is not wrapped by the @traceable decorator. Example: >>> @traceable ... def my_function(input_data): ... # Function implementation ... pass >>> runnable = as_runnable(my_function) r)�RunnableConfig�RunnableLambda)�Input�Outputzjas_runnable requires langchain-core to be installed. You can install it with `pip install langchain-core`.Nz<source unavailable>zRas_runnable expects a function wrapped by the LangSmith @traceable decorator. Got z defined as: c�n���eZdZdZ dd���fd � Zed���fd � ��Zed���fd� ��Z�xZS)�&as_runnable.<locals>.RunnableTraceablez�Converts a @traceable decorated function to a Runnable. This helps maintain a consistent LangSmith tracing context. Nrer �afunc�*Optional[Callable[..., Awaitable[Output]]]r<r<c �B��d}|�|��}t|��rC|�td���tt�g�f|�|����}n@t |��r1tt�g�f|�|����}|�t|jj �d|�����t��� |ttt�gt�f|����dS)Nz�Func was provided as a coroutine function, but afunc was also provided. If providing both, func should be a regular function to avoid ambiguity.zG expects a function wrapped by the LangSmith @traceable decorator. Got )� _wrap_asyncr�� TypeErrorrr ro� _wrap_sync� ValueError� __class__r��superr"rr )r�rerb�wrapped�awrappedr^r_ris ���rBr"z/as_runnable.<locals>.RunnableTraceable.__init__qs7��� <@�G��'�'��.�.�H���~�~� Q��'�#�7���� �x�����8�$�:J�:J�4�:P�:P�Q�Q���&�t�,�,� Q��x�����8�$�/�/�$�:O�:O�P�P���� ��~�.�9�9�26�9�9���� �G�G� � ����X�u�g�y��/@�&@�A�B���� � � � � rA�Callable[..., Output]�)Callable[[Input, RunnableConfig], Output]c�P���d�fd� }tt��g�f|��S) z4Wrap a synchronous function to make it asynchronous.r�dictr�r\r<rc���tj�tt|����}�di|�dd|ii��S�Nr�r�r@�r%r,�from_runnable_configrrp)rr�r�res �rB�wrap_traceablezIas_runnable.<locals>.RunnableTraceable._wrap_sync.<locals>.wrap_traceable�sI���$�,�A�A�$�t�V�BT�BT�U�U���t�M�M�f�M�M�z�8�6L�M�M�M�MrA�rrpr�r\r<r)rr )rerur^r_r\s` ���rBrgz1as_runnable.<locals>.RunnableTraceable._wrap_sync�sI����  N� N� N� N� N� N���%��!8�&�!@�A�>�R�R� RrA�>Optional[Callable[[Input, RunnableConfig], Awaitable[Output]]]c����|�dSt|��std|�����ttdt�f|���d �fd � }tt��gt�f|��S) z.Wrap an async function to make it synchronous.NzXRunnableTraceable expects a function wrapped by the LangSmith @traceable decorator. Got .rrpr�r\r<rc���K�tj�tt|����}�di|�dd|ii���d{V��Srrrs)rr�r��afunc_s �rB�awrap_traceablezKas_runnable.<locals>.RunnableTraceable._wrap_async.<locals>.awrap_traceable�s_�����$�,�A�A�$�t�V�BT�BT�U�U��#�V�U�U�f�U�U�z�8�>T�U�U�U�U�U�U�U�U�U�UrArv)rorhrr r )rbr{rzr^r_r\s @���rBrez2as_runnable.<locals>.RunnableTraceable._wrap_async�s����� �}��t�(��/�/� � �:�27�:�:�����(�3� �&�(9�#9�:�E�B�B�F� V� V� V� V� V� V���%��0�)�F�2C�C�D�o��� rAr�)rer rbrcr<r<)rermr<rn)rbrcr<rw) r�r�r�r�r"� staticmethodrgre� __classcell__)rir^r_r\s@���rB�RunnableTraceableraks��������� � �AE� � � � � � � � � �@ � S� S� S� S� S� S� S� �� S� � � � � � � � � �� � � � � rAr~) �langchain_core.runnablesr\r]�langchain_core.runnables.utilsr^r_� ImportErrorror�� getsourcer�rh)rZr]r��fn_srcr~r^r_r\s @@@rB� as_runnabler�DsZ�����*�K�K�K�K�K�K�K�K�@�@�@�@�@�@�@�@�@�� ����� D� � �� ��������� !�� .� .� � ,��&�|�4�4�F�F��� ,� ,� ,�+�F�F�F� ,����� O�*6� O� O�FL� O� O� � � � G�G�G�G�G�G�G�G�G�N�G�G�G�R � �\� *� *�*s"�� 5�0�5�A� A,�+A,>r��toolr��parser�prompt� embedding� retrieverc�Z�eZdZUdZded<ded<ded<ded<d ed <d ed <d ed<dS)�_TraceableContainer�3Typed response when initializing a run a traceable.r=r�rQr7� outer_projectrSr0rRr/r�r�zcontextvars.ContextrDNr�r@rArBr�r��sl�������=�=�(�(�(�(����� � � � �,�,�,�,�#�#�#�#�8�8�8�8� � � � � � rAr�c��eZdZUdZded<ded<ded<ded <d ed <d ed <ded<ded<ded<d ed<ded<ded<dS)rr�r�r�rQrqrSr9rRr8rVr$r{rrr7r�r�ryrsrur�zOptional[bool]r�Nr�r@rArBrr�s��������=�=���������&�&�&�&�����&�&�&�&�!�!�!�!�����"�"�"�"�4�4�4�4�%�%�%�%�:�:�:�:�0�0�0�0�0�0rAr� containerr�� Optional[Any]r�r9r<c�T�|�d��}|�dSt|t��r|}n�|��t|d��r�t |j��rvt|t ��sa |�dd���}nL#t$r;}t� dt |���d|����d |i}Yd}~n d}~wwxYwd |i}d}|r'tj ��}t|���d |��}|� ||� ��tj��dur|���|�d ��}|�Rt |��rE ||��dS#t $r(}t�d |����Yd}~dSd}~wwxYwdSdS)z End the run.r�N� model_dumpT�json)� exclude_none�modez&Failed to use model_dump to serialize z to JSON: �outputr?�r�r�r�zFailed to run on_end function: )r?rjrprmrr��typer��LOGGER�debugr'rA�reprrBr)rCr��warning) r�r�r�r��outputs_r��error_� stacktracer�s rB�_container_endr��s�� �}�}�Y�'�'�H������'�4� � �'������ �G�\� *� *� � �W�'� (� (� ��7�D�)�)� �  +��)�)�t�&�)�I�I�H�H��� +� +� +� �L�L�U��g���U�U�RS�U�U� � � �!�'�*�H�H�H�H�H�H�����  +���� �g�&�� �F� �2��&�(�(� ���K�K�1�1�Z�1�1�� �L�L���L�0�0�0� ��!�!�T�)�)������� �]�]�8� $� $�F� ��h�v�.�.�� B� �F�8� � � � � ��� B� B� B� �N�N�@�Q�@�@� A� A� A� A� A� A� A� A� A����� B�������s0�.B� C �1C�C �$ E1�1 F#�;F�F#r�rpr�c�H�|�dd��}|ri|�|�}n|}|S)Nr�rI)r�r�r�� extra_inners rB�_collect_extrar� s;���#�#�K��6�6�I��"�2��2� �2� � �!� � �rAr��Optional[dict]c �Z�|�d��}|dkrdSt|tj��r|St|t��rUtj�||�d��t |�d�������St|t��rWtj�||�d��t |�d�������}|S|�d��}|r|St��}tj ���atj� ||�d�����x}r+|r'|�|�d��s|j |j kr|S|S) Nr6�ignorer$r7)r$r7r�r#� callbacks)r?rjr%r,rp� from_headersr*r�from_dotted_orderrCr)�get_langchain_core_versionrt� dotted_order)r�r�r6�dortr��crt�rts rBr_r_s���� � �� *� *�F� �����t��&�)�+�,�,��� ��&�$��� �� �-�-� �"�&�&�x�0�0�*�?�+>�+>�~�+N�+N�O�O� .� � � � �&�#����� �2�2� �"�&�&�x�0�0�*�?�+>�+>�~�+N�+N�O�O� 3� � �� � ��"�"�:�.�.�H����� � � �C��.�0�0�<��"�7�7� �?�.�.�x�8�8�8� � � �2� �� ��&�6�:�:�k�+B�+B�&� �?�S�%5�5�5�� � �JrAr�r�c �P �|�d��pi}|�d��}|�d��}|�d��}|�d��pd} |�dd��} t���} |p t��}|�d ��p|�d ��} |�d|��pt���} t i|�d| i�|�d ����}t���}|p:|r|jnd p/|�d ��p|d pt j��}|�d ��}|�d��}|sgt j��sTt j tj d��td || d d |�d��t�����S|pttj����}t#j|��}| pt j|��}t)||��}t*���}t,���}t��}i|�d��pi�|pi�}|�t*j|��|�|pi��d|d<||d<t5|g|�Ri|��\}}|�d��}|r� d�||�����D��}|r*t9|t:��r|�|��n7#t<$r*}t>� d|�d|����Yd }~nd }~wwxYw|�d��}|rD ||��}n7#t<$r*}t>� d|�d|����Yd }~nd }~wwxYw|�d��pg|pgz} |�t,j| ��| |pgz } |�|�!|| || |||���}!nEtEj#tIj%|��||| tIj%|d���||| | || �� � }!t j��durR |!�&��n<#t<$r/}t>� d|!j'�d|����Yd }~nd }~wwxYwt|!|| |||�d��|���}"|�tj|"d ��|�tPj|"d��|"S) r�r�r9r8r$r�r�r�Frqr�Nr7r�r�z>LangSmith tracing is not enabled, returning original function.r�)r�r7r�r0r/r�rDr~r%r�c��i|] \}}|�||�� Sr�r@)rJrKrLs rBrMz_setup_run.<locals>.<dictcomp>�s&��!�!�!���A����1���rAz&Failed to infer invocation params for r>rszFailed to filter inputs for )rqr�rr8r�r�rTr&) r(rqrr�r�r7r�r8r$rr�zFailed to post run r�))r?r/r�r4r_rWr'rXr)�log_once�logging�DEBUGr�rr�uuid�uuid4r�r��_get_function_namer�r1r0r�ra�update� _get_inputs_and_attachments_saferNrjrpr�r�r�r+r%r,rr-r.r(r,)#rer�r�r�rWr�r9r8r$r�r�r�rqr1r2� project_cv�selected_projectr��id_r��name_r�r0r/rD� metadata_rrr��invocation_paramsr�rsr3r��response_containers# rBr�r�Gsr��"�%�%�m�4�4�:��K��"�"�:�.�.�H� � � �v� &� &�D� � � �� *� *�F��"�"�:�.�.�9�'�H�#2�#6�#6�&��$�$� �"�%�%�'�'�M�%�9��)9�)9�O� � � �v� &� &� E�/�*=�*=�f�*E�*E�D��!�!�(�F�3�3�D�w�{�{�}�}�G�!�.�?�.�H�g�.�.�� � �8�0D�0D���K��"�"�$�$�J�� &�(3� =�K� $� $�� &� � � �~� .� .�  &� �>� *�  &� � #� %� %��+�.�.�/E�F�F�� � � �h� '� '�C� �  �u�7�9�9�  � �� �M� L� � � �#��)�'���"�&�&�x�0�0� �N�N� � � � � � "��T�Z�\�\�"�"�C��!�$�'�'�I� � 2�E�,�T�2�2�E� ��o�>�>�K��]�]�_�_�N������J��n�n�G�� � � �z� *� *� 0�b�� � �R��I� �K�K� � �y�)�)�)� ���X�^��$�$�$�(�I�k��'�K� ��:�9�V�t�V�V�V�v�V�V��F�K�*�.�.�/E�F�F���P� P�!�!�!5�!5�f�!=�!=�!C�!C�!E�!E�!�!�!� �!� 4�Z�0A�4�%H�%H� 4�� � �!2�3�3�3���� P� P� P� �L�L�N�%�N�N�1�N�N� O� O� O� O� O� O� O� O����� P����$�(�(�)9�:�:�N��F� F�#�^�F�+�+�F�F��� F� F� F� �L�L�D��D�D��D�D� E� E� E� E� E� E� E� E����� F���� � � �� (� (� .�B�:�3C�� D�E� �K�K�� �5�!�!�!� �T�Z�R��E����*�*�������#�+� � ����#��%�c�*�*����!*�!7�$�$�"�"�"�*����#�)E� � � �� ��!�!�T�)�)� B� �L�L�N�N�N�N��� B� B� B� �L�L�@�w�z�@�@�Q�@�@� A� A� A� A� A� A� A� A����� B����,��%�#�%���"�"�8�,�,������ �K�K� �!�#5�n�#E�F�F�F� �K�K� �$�&8��&C�D�D�D� �sI�=AN� O� O�O� O,�, P �6 P�P � S5�5 T.�?%T)�)T.r�c��� |� ||��}t|||���dS#t$r5}t�dt |������Yd}~dSd}~wwxYw)zHandle the end of run.Nr�z!Unable to process trace outputs: )r�r�r�r�r�)r�r�r�r�r�s rBrr�s���F� � (�'�'��0�0�G��y�'��?�?�?�?�?�?�� �F�F�F����D�4��7�7�D�D�E�E�E�E�E�E�E�E�E�����F���s�#� A"�*A�A"c�$�t|dd��S)Nr�F)�getattrrns rBriri�s�� �4�2�E� :� :�:rAr��inspect.Signaturec��|j|i|��}|���t|j��}|�dd��|�dd��|j���D]S\}}|jtj j kr4||vr0|� ||��|�|���T|S)z:Return a dictionary of inputs from the function signature.r�N�cls) � bind_partial�apply_defaultsrp� argumentsr�r�rNr�r�r�r�r�)r�r�rW�boundr�� param_namer�s rB� _get_inputsr��s��� #�I� "�D� 3�F� 3� 3�E� �������U�_�%�%�I� �M�M�&�$���� �M�M�%�����&�1�7�7�9�9�*�*�� �E� �:��*�6� 6� 6��Y�&�&�� � ��:�!6�7�7�7�� � �j�)�)�)�� �rAc�� t|g|�Ri|��S#t$r/}t�d|�d|����||d�cYd}~Sd}~wwxYw�NzFailed to get inputs for r>)r�rW)r�r�r�r�)r�r�rWr�s rB�_get_inputs_safer��s���0��9�6�t�6�6�6�v�6�6�6�� �0�0�0�� � �A��A�A�a�A�A�B�B�B���/�/�/�/�/�/�/�/�����0���s�� A �$A�A �A r��inspect.Parameterc��|jtjkpGt|j��tko*t d�t |j��D����S)Nc3�6K�|]}|tjkV��dSr�)r&� Attachment)rJ�args rBr�z!_is_attachment.<locals>.<genexpr>�s*����P�P�c��w�)�)�P�P�P�P�P�PrA)� annotationr&r�r"r�anyr!)r�s rB�_is_attachmentr��sX�� � �w�1� 1� ��5�#�$�$� �1� Q� �P�P�X�e�>N�5O�5O�P�P�P� P� P�rA�Set[str]c�H�d�|j���D��S)Nc�6�h|]\}}t|���|��Sr@)r�)rJrqr�s rB� <setcomp>z*_attachment_args_helper.<locals>.<setcomp>s9�� � � ���u��u�@U�@U� � � � � rA)r�rN�r�s rB�_attachment_args_helperr�s2�� � � )� 4� :� :� <� <� � � �rAi�)�maxsizec� �t|��Sr�)r�r�s rB�_cached_attachment_argsr�s�� "�9� -� -�-rAc�`� t|��S#t$rt|��cYSwxYwr�)r�rfr�r�s rB�_attachment_argsr� sD��2�&�y�1�1�1�� �2�2�2�&�y�1�1�1�1�1�2���s ��-�-� Tuple[dict, schemas.Attachments]c�,� t|g|�Ri|��}t|��}|r1ii}}|���D]\}}||vr|||<�|||<�||fS|ifS#t$r1} t�d|�d| ����||d�ifcYd} ~ Sd} ~ wwxYwr�)r�r�rNr�r�r�) r�r�rW�inferred�attachment_argsrrrKrLr�s rBr�r�s���4��y�:�4�:�:�:�6�:�:��*�9�5�5�� � '�"$�b�K�F� ���(�(� "� "���1���'�'�%&�K��N�N� !�F�1�I�I��;�&� &���|��� �4�4�4����C�9�C�C��C�C�D�D�D���/�/��3�3�3�3�3�3�3�����4���s$�AA�A� B�"&B�B�Bc���|�6t���D]\}}|�d���dS|���D]'\}}t|}|�|���(dS)zSet the tracing context.N)r;rNra)rDrKrL�vars rBrbrb's|����!�'�'�)�)� � �D�A�q� �E�E�$�K�K�K�K���� � �������1��A��� ���� � � � ��rAr�� Iterator[T]r�r�r�� List[Any]�Generator[T, None, Any]c#�K� |d�t|��}|r ||��}n|}|rg|dr_|d�dtj�tjj�����d|id���|�|��|V���#t$r}|j cYd}~Sd}~wwxYw)NTrDr�� new_token�token�rq�timerW) r��next� add_event�datetime�now�timezone�utc� isoformatr�� StopIteration�value)r�r�r�r�rur�� traced_itemr�s rBr�r�2s ����� �#�I�.�2�2�4��C�C�D�� #�+�m�D�1�1� � �"� �� �m�I�6� ��i�(�2�2� +� (� 1� 5� 5�$�-�1�!�!�#�)�+�+�#*�K�"8� ����� �N�N�;� '� '� '��J�J�J�# ��$ �����w��������������s�B5B9�9 C�C� C�C�AsyncIterator[T]r��AsyncGenerator[T, None]c�fK� |r5tjtj|��|d����d{V��}nQt dit |d����5tj|���d{V��}ddd��n #1swxYwY|r ||��}n|}|rg|dr_|d�dtj�tj j ��� ��d|id���|� |��|WV���#t$rYdSwxYw) NTrDrOr�r�r�r�r@)r�r�r��py_anextrdrPr�r�r�r�r�r�r��StopAsyncIteration)r�r�r�r�r�rur�r�s rBr�r�Qs����� � �� @�$�0��'� �2�2�)�)�4������������ %�U�U�':�=��;S�'T�'T�U�U�@�@�!+�!4�Y�!?�!?�?�?�?�?�?�?�D�@�@�@�@�@�@�@�@�@�@�@����@�@�@�@�� #�+�m�D�1�1� � �"� �� �m�I�6� ��i�(�2�2� +� (� 1� 5� 5�$�-�1�!�!�#�)�+�+�#*�K�"8� ����� �N�N�;� '� '� '��J�J�J�J�3 ��4 � � � � ��� ���s7�AD"�B�6 D"�B�D"� B� BD"�" D0�/D0�Tc�H�eZdZdZ ddd �Zdd �Zd �Zd�Zd�Zd�Z ddd�Z dS)�_TracedStreamBasez%Base class for traced stream objects.Nr��$Union[Iterator[T], AsyncIterator[T]]r�r�rrr{c��||_||_d|_||_g|_|dr|djdknd|_dS)NFr�r�)� __ls_stream__�__ls_trace_container__�__ls_completed__�__ls_reduce_fn__�__ls_accumulated_output__r��__is_llm_run__)r�r�r�rrs rBr"z_TracedStreamBase.__init__s^�� $���&5��#� %��� )���24��&��y�)� �O�I� &� /�5� 8� 8�� ���rArqrc�,�t|j|��Sr�)r�r)r�rqs rB� __getattr__z_TracedStreamBase.__getattr__�s���t�)�4�0�0�0rAc��ttt|j��t|j��z����Sr�)�listra�dirrirrHs rB�__dir__z_TracedStreamBase.__dir__�s2���C��D�N�+�+�c�$�2D�.E�.E�E�F�F�G�G�GrAc��d|j�d�S)Nz Traceable(�))rrHs rB�__repr__z_TracedStreamBase.__repr__�s��3�D�.�3�3�3�3rAc�*�t|j��Sr�)rrrHs rB�__str__z_TracedStreamBase.__str__�s���4�%�&�&�&rAc�� |js|���n#t$rYnwxYw |j���dS#t$rYdSwxYwr�)r� _end_tracer�r�__del__rHs rBrz_TracedStreamBase.__del__�s��� ��(� "����!�!�!���� � � � �D� ���� � � � &� &� (� (� (� (� (��� � � � �D�D� ���s�� +�+�A � A�Ar�r9c���|jrdS |jr|�|j��}n|j}t|j||���d|_dS#d|_wxYw)Nr�T)rrrr�r)r�r��reduced_outputs rBrz_TracedStreamBase._end_trace�s��� � � � �F� )��$� @�!%�!6�!6�t�7U�!V�!V���!%�!?�� ��+�^�5� � � � �%)�D� !� !� !��D�D� !� (� (� (� (s �AA� Ar�)r�rr�r�rrr{)rqr�r�r9) r�r�r�r�r"r rrrrrr@rArBrr|s�������/�/� )-�  � � � � �"1�1�1�1�H�H�H�4�4�4�'�'�'� � � � )� )� )� )� )� )� )rArc�F��eZdZdZ dd�fd � Zdd �Zdd�Zd�Zd�Z�xZ S)r�z>A wrapper for synchronous stream objects that handles tracing.Nr�r�r�r�rrr{ruc���t���|||���||_t|j|j|j|j|���|_dS)N�r�r�rrr�)rjr"rr�rr r� __ls__gen__�r�r�r�rrruris �rBr"z_TracedStream.__init__�so��� ������?�i� � � � �$���,� � � � '��*��2�'�  � � ����rAr<r�c�r� t|j��S#t$r|����wxYwr�)r�rr�rrHs rB�__next__z_TracedStream.__next__�sF�� ���(�)�)� )��� � � � �O�O� � � � � ���s�� 6c#�K� |jEd{V��|���dS#t$r+}t|��|�|����d}~wwxYw�Nr�)rrr�r�)r�r�s rBr�z_TracedStream.__iter__�s����� ��'� '� '� '� '� '� '� '� �O�O� � � � � �� � � � � �q� !� !� !� �O�O�!�O� $� $� $� ����� ���s� '� A�&A�Ac�4�|j���Sr�)rrIrHs rBrIz_TracedStream.__enter__�s���!�+�+�-�-�-rAc�� |j�|||��|�|r|nd���S#|�|r|nd���wxYwr#)rrKr�r�r6�exc_val�exc_tbs rBrKz_TracedStream.__exit__�sc�� A��%�.�.�x��&�I�I� �O�O�X�"?�'�'�4�O� @� @� @� @��D�O�O�X�"?�'�'�4�O� @� @� @� @���s �8�A�NN)r�r�r�r�rrr{rur{�r<r�)r<r�) r�r�r�r�r"r!r�rIrKr}�ris@rBr�r��s��������H�H� )-�,0�  � � � � � � �&��������.�.�.�A�A�A�A�A�A�ArAr�c�P��eZdZdZ dd�fd � Zddd �Zdd�Zdd�Zd�Zd�Z �xZ S)r�z?A wrapper for asynchronous stream objects that handles tracing.Nr�r�r�r�rrr{ruc����t���|||���||_t|j|j|jt j��|j|���|_ dS)Nrr�) rjr"rr�rr r�r�r�_TracedAsyncStream__ls_genrs �rBr"z_TracedAsyncStream.__init__�sx��� ������?�i� � � � �$���/��(��5��*�&�>�@�@��2�'�  � � �� � � rAr�r9c���K�t��}tjtj|j||������d{V��t t|����dS)NrM)rr�r�r�r�rrbrP)r�r�rOs rB� _aend_tracez_TracedAsyncStream._aend_trace�sw�����n�n���n� � $�T�_�e�3� G� G� G� � � � � � � � � � �0��5�5�6�6�6�6�6rAr<r�c��K� tttj|j���d{V����S#t $r|����d{V���wxYwr�)rr�r�r�r.r�r0rHs rB� __anext__z_TracedAsyncStream.__anext__s|���� ����!4�T�]�!C�!C�C�C�C�C�C�C�D�D� D��!� � � ��"�"�$�$� $� $� $� $� $� $� $� � ���s �16�&Ac�K� |j23d{V��}|WV�� 6 |����d{V��dS#t$r|����d{V���wxYwr�)r.r0r�)r�r�s rBr�z_TracedAsyncStream.__aiter__ s����� %�"�m� � � � � � � �d�� � � � � �,�m� �"�"�$�$� $� $� $� $� $� $� $� $� $�� � � � ��"�"�$�$� $� $� $� $� $� $� $� � ���s�6��6�&Ac��DK�|j����d{V��Sr�)rrQrHs rBrQz_TracedAsyncStream.__aenter__s-�����'�2�2�4�4�4�4�4�4�4�4�4rAc���K� |j�|||���d{V�� |����d{V��S#|����d{V��wxYwr�)rrSr0r&s rBrSz_TracedAsyncStream.__aexit__s����� %��+�5�5�h���P�P�P�P�P�P�P�P� P��"�"�$�$� $� $� $� $� $� $� $� $��$�"�"�$�$� $� $� $� $� $� $� $� $���s �!A�Ar))r�r�r�r�rrr{rur{r�rr*)r<r�) r�r�r�r�r"r0r2r�rQrSr}r+s@rBr�r��s��������I�I� )-�,0�  � � � � � � �(7�7�7�7�7�����%�%�%�%�5�5�5�%�%�%�%�%�%�%rAr�r c��|rC|�? ||��S#t$r&}t�|��|cYd}~Sd}~wwxYw|SdSr�)r�r�r�)r�rrr�s rBr�r� ss���� � � � �y��)�)�)�� � � � �� � �Q��������������� �����N��s� � A�<�A�Ar�r�c���|j}|rp|j�`|jjj�t ��r7|j}|j�)|jjj�t ���7||_dSdSr�)� __traceback__�tb_next�tb_frame�f_code� co_filename�endswith�_EXCLUDED_FRAME_FNAME)r��tb_s rBr�r�,s��� �/�C� ���k�%�#�,�*=�*I�*R�*R� !�+ �+ �%��+�C��k�%�#�,�*=�*I�*R�*R� !�+ �+ �%������ �rA)r<r=r�)rDrEr<rF)r7rQr8rRr9rSr6rTr:rUr$rVrWrr<rX)rerr<rf)rervrqrQr9rwr8rRr$rVrrrxr7rQrsryrtrzrur{r<r|)rer r<r�)rervr<r|rT)r�r�rqrQr9rwr8rRr$rVrrrxr7rQrsryrtrzrur{r�ryr�r�r<r�)r�rrWrr<r�)r7rQr<rQ)rZr r<r+r))r�r�r�r�r�r9r<r<)r�rpr�r�r<rp)r�r�r�r�r<r=rU) rer r�rr�r�r�rrWrr<r�) r�r�r�r�r�r9r�rzr<r<)rerr<r�)r�r�r�rrWrr<rF)r�r�r<r�)r�r�r<r�)r�r�r�rrWrr<r�)rDrS) r�r�r�r�r�r�r�r�rur{r<r�)r�r�r�r�r�r�r�r�r�r�rur{r<r�)r�r rrr r<r)r�r�)wr�� __future__rr�� contextlib� contextvarsr�rkr�r�r�r\r�typingrrrrr r r r r rrrrrrrrrrrrrrrr�typing_extensionsrrr r!r"� langsmithr$rr%r&r'�langsmith._internalr(r�� langsmith.envr)�typesr*rr+� getLoggerr�r�� ContextVarr,r,rr/r0r1r�r3�Clientr4r;r�r>rCrP�contextmanagerrd�get_run_tree_contextrorr�r�r�r�r�r~rr*r�rr�rr�r�r_r�rrir�r�r�r�� lru_cacher�r�r�rbr�r�r�rr�r�r�r�r@rArB�<module>rOs ��7�7�7�"�"�"�"�"�"����������������������������� � � � �����$�$�$�$�$�$�������������������������������������������������������8T�S�S�S�S�S�S�S�S�S�S�S�S�S�)�)�)�)�)�)�/�/�/�/�/�/�/�/�/�/�4�4�4�4�4�4�&�&�&�&�&�&��2�#�#�#�#�#�#�1�1�1�1�1�1� �� �8� $� $��F�;�)�(�9�3D�*E�F�������6� �&�x��}�5�o�t�T�T�T� �3� ��x��S� �2�3�G�T�J�J�J�� <�K� "�8�D��c��N�#;� <�[�RV� W� W� W� �S�;�)�(�5��w�w�?O�9O�3P�*Q�R������� =�+� ��)�*:�!;� <�Y�PT� U� U� U���!� ���� 4�4� �����3��"�"�"�"� .2� A� A� A� A� A�  ��#'� $�)-�OS�7;�)-�6.�6.�6.�6.�6.���6.�t,�������,0� $�)-�6:�"&�7;�59�(,� � � � � � �:����E�E�E�E�E�Y�e�E�E�E�E�6 �G�C�4� � � �� �I�c�N�N��� � � � � �X�w�q�!�t�}� � ��� �D �'�'�'� ��'�  �%,�C��,0� $�)-�6:�"&�7;�59�(,�>B�).�C�C�C�C�C� ��C�"w�w�w�w�tP7�P7�P7�P7�P7�P7�P7�P7�f � � � �p+�p+�p+�p+�h���� !� !� !� !� !�)�5� !� !� !� !�1�1�1�1�1�i�u�1�1�1�1�&"�%)�'B�'B�'B�'B�'B�T����"�1�1�1�1�1�n15��� ~�~�~�~�~�F"�%)�7;� F� F� F� F� F�;�;�;�;�����(0�0�0�0��������� ���T�"�"�"�.�.�.�#�"�.�2�2�2�2�4�4�4�4�(���������>% �% �% �% �P �G�C�L�L��7)�7)�7)�7)�7)��� �7)�7)�7)�t.A�.A�.A�.A�.A�%�w�q�z�.A�.A�.A�b6%�6%�6%�6%�6%�*�G�A�J�6%�6%�6%�r � � � ������rA
Memory