
    ;i              
         S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	r	S SK
r
S SKrS SKrS SKrS SKrS SKrS SKJrJr  S SKJr  S SKJr  S SKJrJrJrJrJrJrJr  S SKJ r   S SK!J"r#  S S	K!J$r%  S S
K!J&r'  S SK!J(r(  S SK!J(r)  S SK!J*r+  S SK!J,r-  S SK.J/r/  S SK0J1r1  S SK2J3r3   S SK4r4\4Rj                  Rl                  r7\
Rr                  " \:5      r;\Rx                  " S5      r=\" S5      r>\" S5      r?S=S jr@\    S>S j5       rA\SSSSSSSSS.                 S?S jj5       rAS@S jrASAS jrBSBS jrC      SCS jrD      SDS jrE SE       SFS jjrF        SGS  jrGSHS" jrH\" S#\\I   S$9rJSIS% jrK " S& S!5      rL " S' S(5      rM\R                  \\M      " S)SS*9rO " S+ S,\ S-S.9rP            SJS/ jrQ            SKS0 jrR            SKS1 jrS\ArTSLS2 jrUSMS3 jrVSNS4 jrW SESSS5.           SOS6 jjjrX\R                  S7S8.   SPS9 jj5       rZSQS: jr[SRS; jr\\R                  SSS< j5       r^g! \8 a     " S S\65      r7 GNf = f)T    )annotationsN)	GeneratorSequence)Future)Path)AnyCallableOptionalTypeVarUnioncastoverload)	TypedDict)client)env)run_helpers)	run_trees)schemas)utils)_orjson)
dumps_json)ID_TYPEc                      \ rS rSrSrg)SkipException1    N)__name__
__module____qualname____firstlineno____static_attributes__r       k/home/dmtnaga/Documents/work/airagagent/rag_env/lib/python3.13/site-packages/langsmith/testing/_internal.pyr   r   1   s    r"   r   z$6ba7b810-9dad-11d1-80b4-00c04fd430c8TUc                |    [        U 5      n[        R                  " UR                  5       5      R	                  5       $ )z4Hash an object to generate a consistent hash string.)
_stringifyhashlibsha256encode	hexdigest)obj
serializeds     r#   _object_hashr.   >   s.     CJ>>*++-.88::r"   c                    g Nr   )funcs    r#   testr2   E   s     r"   idoutput_keysr   test_suite_namemetadatarepetitionssplitcached_hostsc                    g r0   r   r3   s           r#   r2   r2   K   s     &)r"   c                   ^^ UR                  SS5      n[        R                  " UR                  SS5      5      nU(       a  U(       d  [        S5      e[	        UR                  SS5      UR                  SS5      UR                  SS5      UR                  SS5      UUR                  S	S5      UR                  S
S5      UR                  SS5      US9	mU(       a'  [
        R                  " SUR                  5        35        [        R                  " 5       mT(       a  [        R                  S5        SUU4S jjnU (       a  [        U S   5      (       a  U" U S   5      $ U$ )a\!  Trace a pytest test case in LangSmith.

This decorator is used to trace a pytest test to LangSmith. It ensures
that the necessary example data is created and associated with the test function.
The decorated function will be executed as a test case, and the results will be
recorded and reported by LangSmith.

Args:
    - id (Optional[uuid.UUID]): A unique identifier for the test case. If not
        provided, an ID will be generated based on the test function's module
        and name.
    - output_keys (Optional[Sequence[str]]): A list of keys to be considered as
        the output keys for the test case. These keys will be extracted from the
        test function's inputs and stored as the expected outputs.
    - client (Optional[ls_client.Client]): An instance of the LangSmith client
        to be used for communication with the LangSmith service. If not provided,
        a default client will be used.
    - test_suite_name (Optional[str]): The name of the test suite to which the
        test case belongs. If not provided, the test suite name will be determined
        based on the environment or the package name.
    - cached_hosts (Optional[Sequence[str]]): A list of hosts or URL prefixes to
        cache requests to during testing. If not provided, all requests will be
        cached (default behavior). This is useful for caching only specific
        API calls (e.g., ["api.openai.com"] or ["https://api.openai.com"]).

Returns:
    Callable: The decorated test function.

Environment:
    - LANGSMITH_TEST_CACHE: If set, API calls will be cached to disk to
        save time and costs during testing. Recommended to commit the
        cache files to your repository for faster CI/CD runs.
        Requires the 'langsmith[vcr]' package to be installed.
    - LANGSMITH_TEST_TRACKING: Set this variable to the path of a directory
        to enable caching of test results. This is useful for re-running tests
         without re-executing the code. Requires the 'langsmith[vcr]' package.

Example:
    For basic usage, simply decorate a test function with `@pytest.mark.langsmith`.
    Under the hood this will call the `test` method:

    .. code-block:: python

        import pytest


        # Equivalently can decorate with `test` directly:
        # from langsmith import test
        # @test
        @pytest.mark.langsmith
        def test_addition():
            assert 3 + 4 == 7


    Any code that is traced (such as those traced using `@traceable`
    or `wrap_*` functions) will be traced within the test case for
    improved visibility and debugging.

    .. code-block:: python

        import pytest
        from langsmith import traceable


        @traceable
        def generate_numbers():
            return 3, 4


        @pytest.mark.langsmith
        def test_nested():
            # Traced code will be included in the test case
            a, b = generate_numbers()
            assert a + b == 7

    LLM calls are expensive! Cache requests by setting
    `LANGSMITH_TEST_CACHE=path/to/cache`. Check in these files to speed up
    CI/CD pipelines, so your results only change when your prompt or requested
    model changes.

    Note that this will require that you install langsmith with the `vcr` extra:

    `pip install -U "langsmith[vcr]"`

    Caching is faster if you install libyaml. See
    https://vcrpy.readthedocs.io/en/latest/installation.html#speed for more details.

    .. code-block:: python

        # os.environ["LANGSMITH_TEST_CACHE"] = "tests/cassettes"
        import openai
        import pytest
        from langsmith import wrappers

        oai_client = wrappers.wrap_openai(openai.Client())


        @pytest.mark.langsmith
        def test_openai_says_hello():
            # Traced code will be included in the test case
            response = oai_client.chat.completions.create(
                model="gpt-3.5-turbo",
                messages=[
                    {"role": "system", "content": "You are a helpful assistant."},
                    {"role": "user", "content": "Say hello!"},
                ],
            )
            assert "hello" in response.choices[0].message.content.lower()

    You can also specify which hosts to cache by using the `cached_hosts` parameter.
    This is useful when you only want to cache specific API calls:

    .. code-block:: python

        @pytest.mark.langsmith(cached_hosts=["https://api.openai.com"])
        def test_openai_with_selective_caching():
            # Only OpenAI API calls will be cached, other API calls will not
            # be cached
            response = oai_client.chat.completions.create(
                model="gpt-3.5-turbo",
                messages=[
                    {"role": "system", "content": "You are a helpful assistant."},
                    {"role": "user", "content": "Say hello!"},
                ],
            )
            assert "hello" in response.choices[0].message.content.lower()

    LLMs are stochastic. Naive assertions are flakey. You can use langsmith's
    `expect` to score and make approximate assertions on your results.

    .. code-block:: python

        import pytest
        from langsmith import expect


        @pytest.mark.langsmith
        def test_output_semantically_close():
            response = oai_client.chat.completions.create(
                model="gpt-3.5-turbo",
                messages=[
                    {"role": "system", "content": "You are a helpful assistant."},
                    {"role": "user", "content": "Say hello!"},
                ],
            )
            # The embedding_distance call logs the embedding distance to LangSmith
            expect.embedding_distance(
                prediction=response.choices[0].message.content,
                reference="Hello!",
                # The following optional assertion logs a
                # pass/fail score to LangSmith
                # and raises an AssertionError if the assertion fails.
            ).to_be_less_than(1.0)
            # Compute damerau_levenshtein distance
            expect.edit_distance(
                prediction=response.choices[0].message.content,
                reference="Hello!",
                # And then log a pass/fail score to LangSmith
            ).to_be_less_than(1.0)

    The `@test` decorator works natively with pytest fixtures.
    The values will populate the "inputs" of the corresponding example in LangSmith.

    .. code-block:: python

        import pytest


        @pytest.fixture
        def some_input():
            return "Some input"


        @pytest.mark.langsmith
        def test_with_fixture(some_input: str):
            assert "input" in some_input

    You can still use pytest.parametrize() as usual to run multiple test cases
    using the same test function.

    .. code-block:: python

        import pytest


        @pytest.mark.langsmith(output_keys=["expected"])
        @pytest.mark.parametrize(
            "a, b, expected",
            [
                (1, 2, 3),
                (3, 4, 7),
            ],
        )
        def test_addition_with_multiple_inputs(a: int, b: int, expected: int):
            assert a + b == expected

    By default, each test case will be assigned a consistent, unique identifier
    based on the function name and module. You can also provide a custom identifier
    using the `id` argument:

    .. code-block:: python

        import pytest
        import uuid

        example_id = uuid.uuid4()


        @pytest.mark.langsmith(id=str(example_id))
        def test_multiplication():
            assert 3 * 4 == 12

    By default, all test inputs are saved as "inputs" to a dataset.
    You can specify the `output_keys` argument to persist those keys
    within the dataset's "outputs" fields.

    .. code-block:: python

        import pytest


        @pytest.fixture
        def expected_output():
            return "input"


        @pytest.mark.langsmith(output_keys=["expected_output"])
        def test_with_expected_output(some_input: str, expected_output: str):
            assert expected_output in some_input


    To run these tests, use the pytest CLI. Or directly run the test functions.

    .. code-block:: python

        test_output_semantically_close()
        test_addition()
        test_nested()
        test_with_fixture("Some input")
        test_with_expected_output("Some input", "Some")
        test_multiplication()
        test_openai_says_hello()
        test_addition_with_multiple_inputs(1, 2, 3)
r:   Ncachea2  cached_hosts parameter requires caching to be enabled. Please set the LANGSMITH_TEST_CACHE environment variable to a cache directory path, or pass a cache parameter to the test decorator. Example: LANGSMITH_TEST_CACHE='tests/cassettes' or @pytest.mark.langsmith(cache='tests/cassettes', cached_hosts=[...])r4   r5   r   r6   r7   r8   r9   )	r4   r5   r   r6   r=   r7   r8   r9   r:   zUnexpected keyword arguments: zLLANGSMITH_TEST_TRACKING is set to 'false'. Skipping LangSmith test tracking.c                &  >^ ^ TR                  SS5      =(       d    Sm[        R                  " T 5      (       a/  [        R                  " T 5      S S.     SUU UU4S jjj5       nU$ [        R                  " T 5      S S.SUU UU4S jjj5       nU$ )Nr8      )requestc                   >#    T(       a  T" U0 UD6I S h  vN $ [        T5       H0  nTR                  5       n[        T/UQ7SU 0UDSU0D6I S h  vN   M2     g  ND N7fNpytest_requestlangtest_extra)rangecopy
_arun_test	r@   	test_argstest_kwargsirepetition_extradisable_trackingr1   rD   r8   s	        r#   async_wrapper.test.<locals>.decorator.<locals>.async_wrapperv  s      $!%y!@K!@@@ {+A'5':':'<$$" (/ &	
 (8   , A
s!   AA:AA
AAc                   > T(       a  T" U0 UD6$ [        T5       H(  nTR                  5       n[        T/UQ7SU 0UDSU0D6  M*     g rB   )rE   rF   	_run_testrH   s	        r#   wrapper(test.<locals>.decorator.<locals>.wrapper  sg    Y6+66 ;'#1#6#6#8  $+ "	
 $4 (r"   )rI   r   r@   r   rJ   r   )getinspectiscoroutinefunction	functoolswraps)r1   rN   rR   r8   rM   rD   s   `  @r#   	decoratortest.<locals>.decoratorp  s    $((:?a&&t,,__T"04*-EH  #" ! 		48 	 	 
	 r"   r   r1   r	   returnr	   )popls_utilsget_cache_dir
ValueError_UTExtrawarningswarnkeystest_tracking_is_disabledloggerinfocallable)argskwargsr:   	cache_dirrY   rM   rD   s        @@r#   r2   r2   Y   s5   j ::nd3L&&vzz'4'@AI IU
 	
 ::dD!JJ}d3zz(D)

#4d;J-JJ}d3jj$'!
N 6v{{}oFG99;1	

* *X a!!a!!r"   c                L   [         R                  R                  S5      (       av  [        R                  R                  S5      (       aR  U [         R                  S   -   n[        [        R                  " [        R                  U5      R                  S S 5      nO+[        [        R                  " 5       R                  S S 5      n[         R                  R                  S5      (       a  [         R                  S   nO[        R                  " S5      =(       d    SnU SU 3nU$ )NPYTEST_XDIST_TESTRUNUIDxdist   LANGSMITH_EXPERIMENTFTestSuiteResult:)osenvironrT   	importlibutil	find_specstruuiduuid5NAMESPACE_DNShexuuid4r^   get_tracer_project)r6   id_nameid_prefixnames        r#   _get_experiment_namer     s     
zz~~/00Y^^5M5Mg5V5V!BJJ/H$II$**T//9==bqAB$**,""2A&'	zz~~,--23,,U3H7HXQseDKr"   c                0   [         R                  " S5      nU(       a  U$ [        R                  " 5       S   n [        R
                  " U 5      nU(       a  U SUR                   3$  [        S5      e! [         a    [        R                  S5         N,f = f)N
TEST_SUITE	repo_name.z3Could not determine test suite name from file path.z9Please set the LANGSMITH_TEST_SUITE environment variable.)r^   get_env_varls_envget_git_inforU   	getmoduler   BaseExceptionrf   debugr`   )r1   r6   r   mods       r#   _get_test_suite_namer     s    **<8O##%k2IL%[#,,00 
 P
QQ  LJKLs   -A3 3BBc                >   U R                  US9(       a  U R                  US9$ [        R                  " 5       R	                  S5      =(       d    SnSnU(       a  USU 3-  n U R                  UUSS0S9$ ! [        R                   a    U R                  US9s $ f = f)	N)dataset_name
remote_url z
Test suitez for __ls_runnerpytest)r   descriptionr7   )has_datasetread_datasetr   r   rT   create_datasetr^   LangSmithConflictError)r   r6   repor   s       r#   _get_test_suiter     s     7"""@@""$((6<""U4&>)K	E((,''2 )  
 .. 	E&&O&DD	Es   #A6 6#BBc                   [        UR                  5      n U R                  UUR                  S[        R
                  " 5       R                  S5      SS.S9$ ! [        R                   a    U R                  US9s $ f = f)NzTest Suite Results.revision_idr   )r   r   )reference_dataset_idr   r7   )project_name)
r   r   create_projectr4   r   get_langchain_env_var_metadatarT   r^   r   read_project)r   
test_suiteexperiment_names      r#   _start_experimentr     s     +:??;OA$$!+-%DDFJJ!   (		 % 

 
	
 ** A"""@@As   A A #A>=A>c                    U [        U5      [        U=(       d    0 5      4n[        U5      n[        R                  " [        U5      $ )z=Generate example ID based on inputs, outputs, and dataset ID.)r.   r'   ry   rz   UUID5_NAMESPACE)
dataset_idinputsoutputsidentifier_obj
identifiers        r#   _get_example_idr     s:     !,v"6W]PR8STNN+J::oz22r"   c                    [        [        [        R                  " U 5      5      R	                  [        R
                  " 5       5      5      nU U SU R                   3n[        U S5      (       a/  [        S U R                   5       5      (       a  U[        U5      -  n[        R                  " [        R                  U5      U[!        [        U5      5      S  4$ ! [         a    U R                  n Nf = f)Nz::
pytestmarkc              3  >   #    U  H  oR                   S :H  v   M     g7f)parametrizeNr   ).0ms     r#   	<genexpr>)_get_example_id_legacy.<locals>.<genexpr>  s      +)8A-s   )rx   r   rU   getfilerelative_tocwdr`   r   r   hasattranyr   r'   ry   rz   r{   len)r1   r   suite_id	file_pathr   s        r#   _get_example_id_legacyr     s    $W__T23??
KL	 :i[4==/:Jt\""s +)-+ ( ( 	j((
::d((*5z#c(mBTBV7WWW  $OO	$s   A
C C43C4_LangSmithTestSuitec                   [         R                  " 5       =(       d    0 nU R                  5         U R                  5       nU R                  R
                  nU R                  R                  U R                  0 UEU[         R                  " 5       R                  S5      SS.ES9  U(       a'  US   b!  U R                  R                  UUSUS    3S9  U(       a)  US   b"  U R                  R                  UUS	US    3S9  g g g )
Nr   r   )dataset_versionr   r   )r7   commitzgit:commit:)r   as_oftagbranchzgit:branch:)r   r   shutdownget_dataset_version_datasetr4   r   update_projectexperiment_idr   rT   update_dataset_tag)r   git_infor   r   s       r#   
_end_testsr     s   ""$*H 446O$$''J$$  

.!@@BFF}U#	
 %  8H-9,,!!hx012 	- 	

 8H-9,,!!hx012 	- 	
 :r"   VT)boundc                    U c  [        [        U 5      $ [        R                  " U 5      n[        R
                  " U5      $ r0   )r   r   	ls_client_dumps_jsonr   loads)valuesbtss     r#   _serde_example_valuesr   *  s3    ~B



'C==r"   c                     \ rS rSr% SrS\S'   \R                  " 5       r      SS jr	\
S 5       r\
S 5       r\
S 5       r\ S       SS	 jj5       r\
S
 5       rS r    S           SS jjrSS jrSSSSSSS.           SS jjr  S         SS jjrSS jrS r  S S S jjr  S!S jrSrg)"r   i1  NOptional[dict]
_instancesc                    U=(       d    [         R                  " 5       U l        X l        X0l        UR
                  U l        [        R                  " 5       U l	        [        R                  " [        U 5        g r0   )rtget_cached_clientr   _experimentr   modified_at_dataset_versionr^   ContextThreadPoolExecutor	_executoratexitregisterr   )selfr   
experimentdatasets       r#   __init___LangSmithTestSuite.__init__5  sP     6 4 4 6%=D=P=P!;;=
D)r"   c                .    U R                   R                  $ r0   )r   r4   r   s    r#   r4   _LangSmithTestSuite.idB  s    }}r"   c                .    U R                   R                  $ r0   )r   r4   r   s    r#   r   !_LangSmithTestSuite.experiment_idF  s    """r"   c                    U R                   $ r0   )r   r   s    r#   r   _LangSmithTestSuite.experimentJ  s    r"   c                v   U=(       d    [         R                  " 5       nU=(       d    [        U5      nU R                     U R                  (       d  0 U l        X0R                  ;  a,  [        X5      n[        X5      nU " XU5      U R                  U'   S S S 5        U R                  U   $ ! , (       d  f       N= fr0   )r   r   r   _lockr   r   r   )clsr   r1   r6   r   r   s         r#   	from_test_LangSmithTestSuite.from_testN  s     12//1)G-A$-GYY>>!#nn4,VE
.vB
25f*2U/  ~~o.. Ys   AB**
B8c                .    U R                   R                  $ r0   )r   r   r   s    r#   r   _LangSmithTestSuite.name`  s    $$$r"   c                    U R                   $ r0   )r   r   s    r#   r   '_LangSmithTestSuite.get_dataset_versiond  s    $$$r"   c                    U(       a  S nSnOU(       a  SnSnOSnSnU(       a  U(       a  UR                  USU05        U R                  R                  U R                  X5        g )Nskippedr   failedr?   passedstatus)update_process_statusr   submit_submit_result)r   run_iderrorr   pytest_pluginpytest_nodeidscorer   s           r#   submit_result!_LangSmithTestSuite.submit_resultg  s\     EFEFEF]//&?QRd116Ar"   c                :    U R                   R                  USX!S9  g )Npass)keyr  trace_idr   create_feedback)r   r  r  s      r#   r  "_LangSmithTestSuite._submit_result|  s     	##Fe#Ur"   )r   r   r7   r9   r  r  c                  U=(       d    0 nU(       aE  U(       a>  X#S.nUR                  5        V	V
s0 s H  u  pU
c  M
  X_M     nn	n
UR                  Xx5        U(       a  UR                  5       OUn[        U5      n[        U5      n U R                  R                  US9nUn[        U[        5      (       a  U/nU(       a  U(       a  XS'   UR                  =(       d    0 R                  S5      nX+R                  :w  dX  Ub  X;R                  :w  dF  Ub  XKR                  :w  d4  [        UR                  5      [        U R                  5      :w  d  UbZ  X:w  aU  U R                  R                  UR                  UUUUU R                  S9  U R                  R                  UR                  S9nU R(                  c  UR*                  U l        g UR*                  (       a?  U R(                  (       a-  UR*                  U R(                  :  a  UR*                  U l        g g g g s  sn
n	f ! [        R                    a@    U R                  R#                  UUUU R                  UUU R$                  R&                  S9n Nf = f)N)r   reference_outputs)
example_iddataset_split)r  r   r   r7   r9   r   )r  r   r   r   r7   r9   
created_at)itemsr   rF   r   r   read_example
isinstancerx   r7   r]   r   r   r   r4   update_exampler^   LangSmithNotFoundErrorcreate_exampler   
start_timer   r   )r   r  r   r   r7   r9   r  r  updatekvexamplenormalized_splitexisting_dataset_splits                 r#   sync_example _LangSmithTestSuite.sync_example  s,    2] &EF'-||~G~tqdad~FG//F&.8==?H&v.'0%	Jkk..*.EG  %*C00$4#5 H,<)&-&6&6&<"%A%A/%R">>)'G,F(X9I9I-Iw))*c$''l:$0.B **&zz!#%#ww +  ++22gjj2I  ($+$7$7D!%%##d&;&;;$+$7$7D! < &  ] H .. 		kk00%77!++66 1 G		s   	HHH# #AI76I7c                
   [        U[        5      (       a  UOU/nU Hc  nU(       a0  U(       a)  SU;   a  US   OUS   nUR                  USUS   U005        U R                  R                  " U R
                  4XS.UD6  Me     g )Nr  valuefeedbackr  )r  r'  )r  listr   r   r  _create_feedback)r   r  r'  r  r  rj   fbvals           r#   _submit_feedback$_LangSmithTestSuite._submit_feedback  s      *(D998zB%,]bk733!JEC0@#A NN!!%%.4EK r"   c                J    U R                   R                  " U40 UDUDSU0D6  g )Nr  r  )r   r  r'  rj   s       r#   r)  $_LangSmithTestSuite._create_feedback  s%     	##FRhR&R6Rr"   c                8    U R                   R                  5         g r0   )r   r   r   s    r#   r   _LangSmithTestSuite.shutdown  s    !r"   c	                X    U R                   R                  U R                  UUUUUUUUS9	$ )N)run_treer  r   r  r7   r9   r  r  )r   r  _end_run	r   r3  r  r   r  r7   r9   r  r  s	            r#   end_run_LangSmithTestSuite.end_run  s?     ~~$$MM!/'' % 

 
	
r"   c	                    U R                  UUR                  UUUS9  X!l        UR                  USU0S9  UR	                  5         g )N)r   r   r9   r7   reference_example_id)r   r7   )r#  r   r9  endpatchr5  s	            r#   r4  _LangSmithTestSuite._end_run  sR     	??% 	 	
 )3%W0F
/STr"   )r   r   r   r   r   )r   Optional[ls_client.Client]r   ls_schemas.TracerSessionr   ls_schemas.Datasetr0   )r   r=  r1   r	   r6   Optional[str]r\   r   )NFNN)r  	uuid.UUIDr  r@  r   boolr  r   r  r   r\   None)r  rA  r  Optional[int]r\   rC  )r  rA  r   r   r   r   r7   r   r9   Optional[Union[str, list[str]]]r\   rC  )NN)
r  r   r'  zUnion[dict, list]r  r   r  r   rj   r   )r  r   r'  dictrj   r   r\   rC  )r\   r   r\   rC  )r   r   r   r    r   __annotations__	threadingRLockr   r   propertyr4   r   r   classmethodr   r   r   r  r  r#  r,  r)  r   r6  r4  r!   r   r"   r#   r   r   1  s   !%J%OOE*** -* $	*     # #     
 *.	/*/ / '	/
 
/ /" % %%  $!!BB B 	B
 B B 
B*V "&"&#'15@8@8 	@8
  @8 !@8 /@8 
@8L "! $ 	
  &S
" 
 

. 
r"   c                      \ rS rSr       S                   SS jjrSS jrSS jrSS jrSS jr  S     SS jjr	SS	 jr
SS
 jrSS jrSrg)	_TestCasei  Nc
                Z   Xl         X0l        X l        X@l        XPl        X`l        Xpl        Xl        Xl        S U l	        S U l
        U(       a`  U(       aX  UR                  UR                  R                  U5        U(       a  U R                  U5        U	(       a  U R                  U	5        g g g g r0   )r   r  r  r7   r9   r  r  r   r  _logged_reference_outputs_logged_outputsadd_process_to_test_suiter   r   
log_inputslog_reference_outputs)
r   r   r  r  r7   r9   r  r  r   r  s
             r#   r   _TestCase.__init__  s     %$ 
**!29=&/3]33##((- ' **+<= ! +=r"   c           
     |    U R                   R                  " U0 0 UE[        U R                  U R                  S9ED6  g )N)r  r  )r   r,  rF  r  r  )r   ri   rj   s      r#   submit_feedback_TestCase.submit_feedback0  sH    ((		
"&"4"4"&"4"4		
r"   c                    U R                   (       a;  U R                  (       a)  U R                   R                  U R                  SU05        g g g )Nr   )r  r  r   )r   r   s     r#   rS  _TestCase.log_inputs<  s?    $"4"444""Xv$6 #5r"   c                    Xl         U R                  (       a;  U R                  (       a)  U R                  R                  U R                  SU05        g g g )Nr   )rQ  r  r  r   )r   r   s     r#   log_outputs_TestCase.log_outputsB  sE    &$"4"444""Y$8 #5r"   c                    Xl         U R                  (       a;  U R                  (       a)  U R                  R                  U R                  SU05        g g g )Nr  )rP  r  r  r   )r   r  s     r#   rT  _TestCase.log_reference_outputsI  sG    ):&$"4"444""%8:K$L #5r"   c                x    U R                   R                  U R                  UUU R                  U R                  S9$ )N)r  r   r  r  )r   r  r  r  r  )r   r  r   s      r#   submit_test_result_TestCase.submit_test_resultP  s?    
 ,,KK,,,, - 
 	
r"   c                    U R                   (       aN  U R                  (       a<  U R                   R                  U R                  S[        R                  " 5       05        g g g )Nr  r  r  r   timer   s    r#   r  _TestCase.start_time]  sE    $"4"444""\499;$? #5r"   c                    U R                   (       aN  U R                  (       a<  U R                   R                  U R                  S[        R                  " 5       05        g g g )Nend_timerd  r   s    r#   rh  _TestCase.end_timec  sE    $"4"444""Z$= #5r"   c                |   Ub  [        U[        5      (       d  SU0nU R                  =(       d;    [        [	        U R
                  R                  5      U R                  =(       d    0 US9nU R
                  R                  UUUU R                  U R                  U R                  U R                  U R                  S9  g )Noutput)r   r   r   )r  r7   r9   r  r  )r  rF  r  r   rx   r   r4   r   r6  rP  r7   r9   r  r  )r   r3  r   r  s       r#   r6  _TestCase.end_runi  s    :gt#<#<)G__ 
4??--.;;$")


 	"<<]]**,,,, 	  		
r"   )rQ  rP  r  r   r7   r  r  r  r  r9   r   )NNNNNNN)r   r   r  rA  r  Optional[uuid.UUID]r7   r   r9   rE  r  r   r  r   r   r   r  r   r\   rC  )rj   r   r   rF  r\   rC  r   rF  r\   rC  r  rF  r\   rC  )NF)r  r@  r   rB  r\   rC  rG  )r   r   r\   rC  )r   r   r   r    r   rW  rS  r\  rT  ra  r  rh  r6  r!   r   r"   r#   rN  rN    s    
 +/#'15!!!%,0>'> > (	>
 !> /> > > > *> 
>B

  $

 
 
	

r"   rN  
_TEST_CASE)defaultc                  p    \ rS rSr% S\S'   S\S'   S\S'   S\S	'   S\S
'   S\S'   S\S'   S\S'   S\S'   Srg)ra   i  r=  r   rm  r4   Optional[Sequence[str]]r5   r@  r6   r=   r   r7   rD  r8   rE  r9   r:   r   N)r   r   r   r    rH  r!   r   r"   r#   ra   ra     s8    &&((""**))r"   ra   F)totalc                  US   =(       d    [         R                  " 5       nUS   nUS   nUS   n[        R                  " U 5      n	[        R
                  " U	/UQ70 UD6=(       d    S n
S nU(       a3  0 nU
(       d  Sn[        U5      eU H  nU
R                  US 5      X'   M     [        R                  XPUR                  S5      5      nUS   nUR                  R                  =(       aa    UR                  R                  R                  S5      =(       a5    UR                  R                  R                  S0 5      R                  S	5      nU(       a  [        R                  " US
5      (       d$  [        X
UR                   5      u  nnU=(       d    UnU(       a%  UR"                  R$                  R'                  S5      OS nU(       a  UR(                  R*                  OS nU(       a^  [-        [.        UR                  R0                  5      S-   [/        UR2                  5      -   UR4                  UR                  R6                  '   [9        U[:        R<                  " 5       UUUU
UUUS9	nU$ )Nr   r5   r7   r9   zU'output_keys' should only be specified when marked test function has input arguments.r6   r4   runtimesdk_versionz0.4.33langsmith_output_pluginz/compare?selectedSessions=)r  r  r7   r9   r   r  r  r  )r   r   rU   	signaturerh_get_inputs_safer`   r]   r   r   rT   r   r7   r^   is_version_greater_or_equalr   r4   configpluginmanager
get_pluginnodenodeidr   rx   urlr   test_suite_urlsr   rN  ry   r}   )r1   rC   rD   ri   rj   r   r5   r7   r9   rz  r   r   msgr  r   r  dataset_sdk_versionlegacy_example_idexample_namer  r  	test_cases                         r#   _create_test_caser    sE    H%?)=)=)?F /Kj)H7#E!!$'I  <T<V<DFG#  S/!AAt,GJ $..n(():;J  %J$$ 	O((,,Y7	O((,,Y;??N 
 h&J&JX' ' +A*--+
'<  4#4
  	++667PQ 
 3AN''..dMj))--.*+***+, 	%%j&9&9&>&>?
 zz|!##
I r"   c                 ^ ^^^ [        T /TQ70 TDUUS.D6m[        R                  T5        U UUU4S jnUS   (       a)  [        US   5      TR                  R
                   S3-  nOS n[        R                  " 5       n0 US   =(       d    0 ESTR                  R                  R                  0EnTR                  R                  R                  /n	UR                  S5      =(       d    S n
[        R                  " S	0 0 UESU0ED6   [        R                  " XiU
S9   U" 5         S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       g = f)
NrC   rD   c                   > TR                  5         [        R                  " [        TSS5      TR                  TR
                  TR                  =(       d    0 R                  5        V Vs0 s H  u  pSU  3U_M     snn TR                  R                  [        4SS9 n T" T0 TD6nTR                  X#5         TR                  5          S S S 5         TR                  5         g s  snn f ! [         a=  nTR                  [        U5      SS9  TR                  US[        U5      05        UeS nAf[         a1  nTR                  [        U5      S	9  TR                  US 5        UeS nAff = f! TR                  5         f = f! , (       d  f       N= f! [         a/  n[         R#                  S
TR                   SU 35         S nAg S nAff = f)Nr   Testls_example_F)r   r  r   r7   r   exceptions_to_handle_end_on_exitTr  r   skipped_reasonr  %Failed to create feedback for run_id :
)r  r{  tracegetattrr  r   r7   r  r   r   r   r6  ra  reprr   rh  rf   warning	r  r  r3  resulter1   rI   r  rJ   s	        r#   _test_run_test.<locals>._test  s   XXz62#### '//52<<> ?DA aS!1$> #--22"/!1
 %y8K8 !!(3""$;
<	((*7 ! ,,47D,I!!(-=tAw,GH  ,,47,;!!(D1 ""$;
 
@  	NN7	8H8H7IQCP 	sl   *C%F C+(E.:FF +
E+58D--E+:,E&&E++E..F  F
F
G%GGr=   .yamlr7   r   r:   ignore_hostsallow_hostsr   )r  rq  setr   r   r4   r{  get_tracing_contextr   r   r   api_urlrT   tracing_contextr^   with_optional_cache)r1   rC   rD   rI   rJ   r  
cache_pathcurrent_contextr7   r  r  r  s   `  ``      @r#   rQ   rQ     sZ    "	  &%I NN9% %N g.12	8L8L8O8O7PPU5VV

,,.O:&," )..99>>
H ((//778L $$^4<K 	GFFXFG$${	
 			
 	HG	
 	
 	HGs$   
E
 D9(E
9
E	E


Ec                 ^ ^^^#    [        T /TQ70 TDUUS.D6m[        R                  T5        U UUU4S jnUS   (       a)  [        US   5      TR                  R
                   S3-  nOS n[        R                  " 5       n0 US   =(       d    0 ETR                  R                  R                  [        TR                  5      S.EnTR                  R                  R                  /n	UR                  S5      n
U
(       a  U
OS n[        R                  " S	0 0 UESU0ED6   [         R"                  " XiUS9   U" 5       I S h  vN   S S S 5        S S S 5        g  N! , (       d  f       N= f! , (       d  f       g = f7f)
Nr  c                   >#    TR                  5         [        R                  " [        TSS5      TR                  TR
                  TR                  TR                  =(       d    0 R                  5        V Vs0 s H  u  pSU  3U_M     snn TR                  R                  [        4SS9 n T" T0 TD6I S h  vN nTR                  X#5         TR!                  5          S S S 5         TR                  5         g s  snn f  NG! [         a=  nTR                  [        U5      SS9  TR                  US[        U5      05        UeS nAf[         a1  nTR                  [        U5      S	9  TR                  US 5        UeS nAff = f! TR!                  5         f = f! , (       d  f       N= f! [         a/  n["        R%                  S
TR                   SU 35         S nAg S nAff = f7f)Nr   r  r  F)r   r  r9  r   r7   r   r  r  Tr  r  r  r  r  )r  r{  r  r  r  r  r   r7   r  r   r   r   r6  ra  r  r   rh  rf   r  r  s	        r#   r  _arun_test.<locals>._test-  s    XXz62##!*!5!5## '//52<<> ?DA aS!1$> #--22"/!1
  %#Y>+>> !!(3""$=
>	((*7 ?  ,,47D,I!!(-=tAw,GH  ,,47,;!!(D1 ""$=
 
B  	NN7	8H8H7IQCP 	s   A4G'7C:	"G'+F-D8D 9D=FF G')F+ 9G' D
F8EF,E==FFFF
F($G'+
G$5%GG'G$$G'r=   r  r7   )r   r9  r:   r  r   )r  rq  r  r   r   r4   r{  r  r   r   rx   r  r   r  rT   r  r^   r  )r1   rC   rD   rI   rJ   r  r  r  r7   r  r:   r  r  s   `  ``       @r#   rG   rG     su     "	  &%I NN9& &P g.12	8L8L8O8O7PPU5VV

,,.O:&," $..99>>$'	(<(<$=
H ((//778L!%%n5L".,DK 	GFFXFG$${	
 g		
 	HG
 			
 	
 	HGsH   DE="E,8EEEE,	E=E
E)	%E,,
E:6E=c               0   [         R                  " 5       (       a  [        R                  S5        g[        R
                  " 5       n[        R                  5       nU(       a  U(       d  Sn[        U5      eUR                  U 5        UR                  U 5        g)a  Log run inputs from within a pytest test run.

.. warning::

    This API is in beta and might change in future versions.

Should only be used in pytest tests decorated with @pytest.mark.langsmith.

Args:
    inputs: Inputs to log.

Example:
    .. code-block:: python

        from langsmith import testing as t


        @pytest.mark.langsmith
        def test_foo() -> None:
            x = 0
            y = 1
            t.log_inputs({"x": x, "y": y})
            assert foo(x, y) == 2
z?LANGSMITH_TEST_TRACKING is set to 'false'. Skipping log_inputs.Nzlog_inputs should only be called within a pytest test decorated with @pytest.mark.langsmith, and with tracing enabled (by setting the LANGSMITH_TRACING environment variable to 'true').)r^   re   rf   rg   r{  get_current_run_treerq  rT   r`   
add_inputsrS  )r   r3  r  r  s       r#   rS  rS  s  su    2 ))++UV&&(H I9A 	
 o r"   c               F   [         R                  " 5       (       a  [        R                  S5        g[        R
                  " 5       n[        R                  5       nU(       a  U(       d  Sn[        U5      e[        U 5      n UR                  U 5        UR                  U 5        g)a
  Log run outputs from within a pytest test run.

.. warning::

    This API is in beta and might change in future versions.

Should only be used in pytest tests decorated with @pytest.mark.langsmith.

Args:
    outputs: Outputs to log.

Example:
    .. code-block:: python

        from langsmith import testing as t


        @pytest.mark.langsmith
        def test_foo() -> None:
            x = 0
            y = 1
            result = foo(x, y)
            t.log_outputs({"foo": result})
            assert result == 2
z@LANGSMITH_TEST_TRACKING is set to 'false'. Skipping log_outputs.Nzlog_outputs should only be called within a pytest test decorated with @pytest.mark.langsmith, and with tracing enabled (by setting the LANGSMITH_TRACING environment variable to 'true').)r^   re   rf   rg   r{  r  rq  rT   r`   _dumpdadd_outputsr\  )r   r3  r  r  s       r#   r\  r\    s~    4 ))++VW&&(H I9A 	
 oWoG!'"r"   c                   [         R                  " 5       (       a  [        R                  S5        g[        R                  5       nU(       d  Sn[        U5      eUR                  U 5        g)a3  Log example reference outputs from within a pytest test run.

.. warning::

    This API is in beta and might change in future versions.

Should only be used in pytest tests decorated with @pytest.mark.langsmith.

Args:
    outputs: Reference outputs to log.

Example:
    .. code-block:: python

        from langsmith import testing


        @pytest.mark.langsmith
        def test_foo() -> None:
            x = 0
            y = 1
            expected = 2
            testing.log_reference_outputs({"foo": expected})
            assert foo(x, y) == expected
zJLANGSMITH_TEST_TRACKING is set to 'false'. Skipping log_reference_outputs.Nzglog_reference_outputs should only be called within a pytest test decorated with @pytest.mark.langsmith.)r^   re   rf   rg   rq  rT   r`   rT  )r  r  r  s      r#   rT  rT    s[    4 ))++X	
 	 I5 	 o##$56r"   )r  r&  c                 [         R                  " 5       (       a  [        R                  S5        gU (       a  [	        XU45      (       a  Sn[        U5      eU (       d  U(       d  Sn[        U5      eU(       a  SU0n Ub  X S'   Ub  X0S'   O [        R                  " 5       n[        R                  5       nU(       a  U(       d  Sn[        U5      eUR                  S	:X  ai  UR                  R                  S
5      (       aI  UR                  S
   nUR                  [        U [        5      (       a  U OSU 05        UR                  US'   OUR                   nUR"                  " U[%        [&        [(        [        4   U 5      40 UD6  g)a  Log run feedback from within a pytest test run.

.. warning::

    This API is in beta and might change in future versions.

Should only be used in pytest tests decorated with @pytest.mark.langsmith.

Args:
    key: Feedback name.
    score: Numerical feedback value.
    value: Categorical feedback value
    kwargs: Any other Client.create_feedback args.

Example:
    .. code-block:: python

        import pytest
        from langsmith import testing as t


        @pytest.mark.langsmith
        def test_foo() -> None:
            x = 0
            y = 1
            expected = 2
            result = foo(x, y)
            t.log_feedback(key="right_type", score=isinstance(result, int))
            assert result == expected
ALANGSMITH_TEST_TRACKING is set to 'false'. Skipping log_feedback.NzGMust specify one of 'feedback' and ('key', 'score', 'value'), not both.zDMust specify at least one of 'feedback' or ('key', 'score', value').r  r  r&  zlog_feedback should only be called within a pytest test decorated with @pytest.mark.langsmith, and with tracing enabled (by setting the LANGSMITH_TRACING environment variable to 'true').
evaluatorsreference_run_idr'  source_run_id)r^   re   rf   rg   r   r`   r{  r  rq  rT   session_namer7   r  r  rF  r4   r  rW  r   r   r(  )	r'  r  r  r&  rj   r  r3  r  r  s	            r#   log_feedbackr    s[   N ))++WXCU+,,Wo#To	3< %W %W&&(H I9A 	
 o,1B1B1F1F2 2 ""#56"8T22HX8N	
 #+++""fd5t+<h&GR6Rr"   Feedbackr   c              #    #    [         R                  " 5       (       a  [        R                  S5        Sv   g[        R                  5       nU(       d  Sn[        U5      eUR                  R                  R                  UR                  UR                  S.n[        R                  " U UR                  SSUS9 nUv   SSS5        g! , (       d  f       g= f7f)ad  Trace the computation of a pytest run feedback as its own run.

.. warning::

    This API is in beta and might change in future versions.

Args:
    name: Feedback run name. Defaults to "Feedback".

Example:
    .. code-block:: python

        import openai
        import pytest

        from langsmith import testing as t
        from langsmith import wrappers

        oai_client = wrappers.wrap_openai(openai.Client())


        @pytest.mark.langsmith
        def test_openai_says_hello():
            # Traced code will be included in the test case
            text = "Say hello!"
            response = oai_client.chat.completions.create(
                model="gpt-4o-mini",
                messages=[
                    {"role": "system", "content": "You are a helpful assistant."},
                    {"role": "user", "content": text},
                ],
            )
            t.log_inputs({"text": text})
            t.log_outputs({"response": response.choices[0].message.content})
            t.log_reference_outputs({"response": "hello!"})

            # Use this context manager to trace any steps used for generating evaluation
            # feedback separately from the main application logic
            with t.trace_feedback():
                grade = oai_client.chat.completions.create(
                    model="gpt-4o-mini",
                    messages=[
                        {
                            "role": "system",
                            "content": "Return 1 if 'hello' is in the user message and 0 otherwise.",
                        },
                        {
                            "role": "user",
                            "content": response.choices[0].message.content,
                        },
                    ],
                )
                # Make sure to log relevant feedback within the context for the
                # trace to be associated with this feedback.
                t.log_feedback(
                    key="llm_judge", score=float(grade.choices[0].message.content)
                )

            assert "hello" in response.choices[0].message.content.lower()
r  Nztrace_feedback should only be called within a pytest test decorated with @pytest.mark.langsmith, and with tracing enabled (by setting the LANGSMITH_TRACING environment variable to 'true').)r   r9  r  ignorer  )r   r   parentr   r7   )r^   re   rf   rg   rq  rT   r`   r   r   r   r  r  r{  r  rQ  )r   r  r  r7   r3  s        r#   trace_feedbackr  ?  s     @ ))++WX
 IA 	
 o**55:: ) 4 4%,,H
 
((!
 

 
 
s   B7C9C>	C
CCc                l     [        U 5      R                  SSS9$ ! [         a    [        U 5      s $ f = f)Nzutf-8surrogateescape)errors)r   decode	Exceptionrx   )xs    r#   r'   r'     s:    !}##G4E#FF 1vs    33c                b    [        5       nU(       d  U $  U" U 5      nU$ ! [         a    U s $ f = f)z)Serialize LangChain Serializable objects.)_get_langchain_dumpdr  )r  dumpdr-   s      r#   r  r    s:     "E1X
 s   	 ..c                 4     SSK Jn   U $ ! [         a     g f = f)Nr   r  )langchain_core.loadr  ImportErrorr  s    r#   r  r    s!    - s   
 
)r,   r   r\   rx   r[   )r4   rm  r5   rt  r   r=  r6   r@  r7   r   r8   rD  r9   z Optional[Union[str | list[str]]]r:   rt  r\   zCallable[[Callable], Callable])ri   r   rj   r   r\   r	   )r6   rx   r\   rx   )r1   r	   r\   rx   )r   ls_client.Clientr6   rx   r\   r?  )r   r  r   r?  r\   r>  r0   )r   rx   r   rF  r   r   r\   rA  )r1   r	   r   r   r   rA  r\   ztuple[uuid.UUID, str])r   r   )r   r   r\   r   )r1   r	   ri   r   rC   r   rD   ra   rj   r   r\   rN  )r1   r	   rI   r   rC   r   rD   ra   rJ   r   r\   rC  rn  ro  rp  )r'  z!Optional[Union[dict, list[dict]]]r  rx   r  z!Optional[Union[int, bool, float]]r&  z&Optional[Union[str, int, float, bool]]rj   r   r\   rC  )r   rx   r\   z2Generator[Optional[run_trees.RunTree], None, None])r  r   r\   rx   )r  r   r\   r   )r\   zOptional[Callable])_
__future__r   r   
contextlibcontextvarsdatetimerW   r(   ru   rU   loggingrs   rI  re  ry   rb   collections.abcr   r   concurrent.futuresr   pathlibr   typingr   r	   r
   r   r   r   r   typing_extensionsr   	langsmithr   r   r   r   r   r{  r   r   r   
ls_schemasr   r^   langsmith._internalr   langsmith._internal._serder   langsmith.clientr   r   skipr  r   r  	getLoggerr   rf   UUIDr   r$   r%   r.   r2   r   r   r   r   r   r   r   rF  r   r   r   rN  
ContextVarrq  ra   r  rQ   rG   unitrS  r\  rT  r  contextmanagerr  r'   r  	lru_cacher  r   r"   r#   <module>r     s/   "          	     / %    ( ) # '  % + ' ' 1 $KK))M 
		8	$ ))BCCLCL; 

 

 
 #+/)-%)#!%.2,0
)
) )
) '	
)
 #
) 
) 
) ,
) *
) $
) 

)FX
$REE/2EE(AA"A A0 #333 3 	3X
X*X6?XX"
8 T$(Z Zzl
 l
^ ##HY$78tT
	*y 	*?
?? ? 	?
 ? ?DL
LL L 	L
 L 
L^O
OO O 	O
 O 
Of &!R(#V&7T 37LS
 0448LS/LS 
	LS
 -LS 2LS LS 
LS^ WW7W Wt	  +  	 s   (I I$#I$