
    )h.5                     `   S SK r S SKJrJr  S SK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r\" S	 \ 5       5      (       d  \" S
5      eS SKJs  Jr  SrS SKJr  \ R4                  " \5      r\(       a!   " S S\5      r " S S\R<                  5      rgg! \ a    Sr " S S5      r NUf = f)    N)datetimetimezone)Optional	TypedDict)uuid4)	run_trees)get_current_run_tree)tracing)TracingProcessorTraceSpanResponseSpanDatac              #   B   #    U  H  n[        [        U5      v   M     g 7fN)hasattrr
   ).0names     Q/root/34ku/venv/lib/python3.13/site-packages/langsmith/wrappers/_openai_agents.py	<genexpr>r      s     ;($ww%%(s   z&The `agents` package is not installed.TFc                       \ rS rSrSrS rSrg)OpenAIAgentsTracingProcessor   a  Tracing processor for the `OpenAI Agents SDK <https://openai.github.io/openai-agents-python/>`_.

Traces all intermediate steps of your OpenAI Agent to LangSmith.

Requirements: Make sure to install ``pip install -U langsmith[openai-agents]``.

Args:
    client: An instance of langsmith.client.Client. If not provided,
        a default client is created.

Example:
    .. code-block:: python

        from agents import (
            Agent,
            FileSearchTool,
            Runner,
            WebSearchTool,
            function_tool,
            set_trace_processors,
        )

        from langsmith.wrappers import OpenAIAgentsTracingProcessor

        set_trace_processors([OpenAIAgentsTracingProcessor()])


        @function_tool
        def get_weather(city: str) -> str:
            return f"The weather in {city} is sunny"


        haiku_agent = Agent(
            name="Haiku agent",
            instructions="Always respond in haiku form",
            model="o3-mini",
            tools=[get_weather],
        )
        agent = Agent(
            name="Assistant",
            tools=[WebSearchTool()],
            instructions="speak in spanish. use Haiku agent if they ask for a haiku or for the weather",
            handoffs=[haiku_agent],
        )

        result = await Runner.run(
            agent,
            "write a haiku about the weather today and tell me a recent news story about new york",
        )
        print(result.final_output)
c                     [        S5      e)NzeThe `agents` package is not installed. Please install it with `pip install langsmith[openai-agents]`.)ImportError)selfargskwargss      r   __init__%OpenAIAgentsTracingProcessor.__init__P   s    Q      N)__name__
__module____qualname____firstlineno____doc__r   __static_attributes__r!   r    r   r   r      s    2	h	r    r   )clientc                   n    \ rS rSr% \\S'   \\S'   \\S'   \\S'   \\   \S'   \\   \S'   \\   \S'   S	rg
)RunData]   idtrace_id
start_timedotted_orderparent_run_idproject_namer   r!   N)	r"   r#   r$   r%   str__annotations__r   r   r'   r!   r    r   r*   r*   ]   s6    }$sm#smr    r*   c                   .   \ rS rSrSr SSSSSS.S\\R                     S\\   S\\	\
      S\\
   S	\\
   4
S
 jjjrS\R                  SS4S jrS\R                  SS4S jrS\R                   SS4S jrS\R                   SS4S jrSS jrSS jrSrg)r   f   ay  Tracing processor for the `OpenAI Agents SDK <https://openai.github.io/openai-agents-python/>`_.

Traces all intermediate steps of your OpenAI Agent to LangSmith.

Requirements: Make sure to install ``pip install -U langsmith[openai-agents]``.

Args:
    client: An instance of langsmith.client.Client. If not provided,
        a default client is created.
    metadata: Metadata to associate with all traces.
    tags: Tags to associate with all traces.
    project_name: LangSmith project to trace to.
    name: Name of the root trace.

Example:
    .. code-block:: python

        from agents import (
            Agent,
            FileSearchTool,
            Runner,
            WebSearchTool,
            function_tool,
            set_trace_processors,
        )

        from langsmith.wrappers import OpenAIAgentsTracingProcessor

        set_trace_processors([OpenAIAgentsTracingProcessor()])


        @function_tool
        def get_weather(city: str) -> str:
            return f"The weather in {city} is sunny"


        haiku_agent = Agent(
            name="Haiku agent",
            instructions="Always respond in haiku form",
            model="o3-mini",
            tools=[get_weather],
        )
        agent = Agent(
            name="Assistant",
            tools=[WebSearchTool()],
            instructions="speak in spanish. use Haiku agent if they ask for a haiku or for the weather",
            handoffs=[haiku_agent],
        )

        result = await Runner.run(
            agent,
            "write a haiku about the weather today and tell me a recent news story about new york",
        )
        print(result.final_output)
N)metadatatagsr1   r   r(   r6   r7   r1   r   c                    U=(       d    [         R                  " 5       U l        X l        X0l        X@l        XPl        0 U l        0 U l        0 U l	        g r   )
rtget_cached_clientr(   	_metadata_tags_project_name_name_first_response_inputs_last_response_outputs_runs)r   r(   r6   r7   r1   r   s         r   r   r      sE     !:B$8$8$:DK%NJ!-J02D'02D'-/DJr    tracereturnc                    [        5       nU R                  (       a  U R                  nO UR                  (       a  UR                  nOSn[        [	        5       5      n[
        R                  " [        R                  5      nU R                  nUbV  [        UR                  5      n[        UR                  5      nUR                  n	U R                  =(       d    UR                  nOUnS nS n	[        R                  " UUU	S9n
[!        UUUU
UUUS9U R"                  UR                  '   SU R$                  =(       d    0 0nUR'                  5       =(       d    0 nUR)                  S5      b  US   US   S'    [+        U0 SUUUU
US UU R,                  US9nU R.                  R0                  " S
0 UD6  g ! [2         a"  n[4        R7                  S	U 35         S nAg S nAff = f)NzAgent workflowr.   run_idparent_dotted_orderr,   r-   r.   r/   r0   r1   r   r6   group_id	thread_idchain)r   inputsrun_typer,   r-   r0   r/   r.   revision_idextrar7   r1   zError creating trace run: r!   )r	   r>   r   r2   r   r   nowr   utcr=   r-   r,   r/   session_nameagent_utilsensure_dotted_orderr*   rA   r;   exportgetdictr<   r(   
create_run	Exceptionlogger	exception)r   rB   current_run_treerun_nametrace_run_idr.   r1   r-   r0   rG   r/   	run_extra
trace_dictrun_dataes                  r   on_trace_start+OpenAIAgentsTracingProcessor.on_trace_start   s   35zz:: ::+uw<L!hll3J  --L+/889 #$4$7$7 8&6&C&C##11R5E5R5R' $&*#&::%#$7L
 *1!%)+)*DJJu~~& $T^^%9r:I-2J~~j)55?
5K	*%k2C!%!$#%"/!-) $#!-" &&22 C  #=aS!ABBCs   5:F0 0
G:GGc                 F   U R                   R                  UR                  S 5      nUR                  5       =(       d    0 n0 UR	                  S5      =(       d    0 EU R
                  =(       d    0 EnU(       a{   U R                  R                  US   US   US   US   U R                  R                  UR                  0 5      U R                  R                  UR                  0 5      SU0US   S9  g g ! [         a"  n[        R                  SU 35         S nAg S nAff = f)	Nr6   r,   r-   r0   r/   r1   )rF   r-   r0   r/   rL   outputsrO   r1   zError updating trace run: )rA   popr-   rU   rV   r;   r(   
update_runr?   r@   rY   rZ   r[   )r   rB   runr`   r6   rb   s         r   on_trace_end)OpenAIAgentsTracingProcessor.on_trace_end   s   **..6C-2JW:>>*5;WAUSUWHGKK**"4y!$Z&)/&:%(%8#::>>u~~rR $ ; ; ? ?PR S)84%(%8 + 	  ! G$$'A!%EFFGs   9A9C4 4
D >DD spanc                 |   UR                   (       a%  U R                  R                  UR                   5      O$U R                  R                  UR                  5      nUc#  [        R                  SUR                   35        g US   n[        [        5       5      nUR                  (       a   [        R                  " UR                  5      O#[        R                  " [        R                  5      n[        R                   " UUU(       a  US   OS S9n[        R"                  " U5      n[%        UR&                  [(        R*                  5      (       ao  UR                  S5      n[-        USS 5      =(       d    [-        UR&                  SS 5      n	U	(       a  [        U	5      OUn
U(       a  U SU
 3R/                  5       nOU
n[        R0                  " U5      n[        R2                  " U5      n[5        UUUUUS   US   US	9U R                  UR                  '    [7        UUUUUS   UUR                  S
0 5      US   S9nUR                  (       a#  [        R                  " UR                  5      US'   U R8                  R:                  " S0 UD6  g ! [<         a"  n[        R?                  SU 35         S nAg S nAff = f)Nz(No trace info found for span, skipping: r-   r/   rE   r    r,   r1   rH   rL   )r   rM   r,   r-   r0   r/   rL   r1   r.   zError creating span run: r!   ) 	parent_idrA   rV   r-   rZ   warningspan_idr2   r   
started_atr   fromisoformatrP   r   rQ   rS   rT   get_run_name
isinstance	span_datar
   r   getattrstripget_run_typeextract_span_datar*   rW   r(   rX   rY   r[   )r   rl   
parent_runr-   span_run_idspan_start_timer/   r]   parent_nameraw_span_name	span_namerM   	extractedra   rb   s                  r   on_span_start*OpenAIAgentsTracingProcessor.on_span_start  sQ    >> 

t~~.ZZ^^DMM2  !>t||nM !*-Heg,K ?? &&t7\\(,,/ 
 '::*"BLJ~$>RVL
 #//5H$..'*B*BCC(nnV4 'fd ; !wNNFD@ 3@C.X	"-a	{;AACH(H"//5H#55d;I'.!*)(.'7(DJJt||$B!%!%"%",T"2!-$==26!+N!;	" ??-5-C-CDOO-TH\*&&22 B  #<QC!@AABs   A6J 
J;J66J;c                    U R                   R                  UR                  S 5      nU(       Ga  [        R                  " U5      nUR                  S0 5      nUR                  US'   UR                  US'   UR                  US'   XCS'   UR                  S0 5      nUR                  S0 5      n[        US   US   US	   US
   UR                  (       a  [        UR                  5      OS UUUUS   S9	nUR                  (       a#  [        R                  " UR                  5      US'   [        UR                  [         R"                  5      (       a]  U R$                  R                  UR                  5      =(       d    UU R$                  UR                  '   XPR&                  UR                  '   U R(                  R*                  " S0 UD6  g g )Nr6   openai_parent_idopenai_trace_idopenai_span_idrf   rL   r,   r-   r/   r0   r1   )	rF   r-   r/   r0   errorrf   rL   rO   r1   end_timer!   )rA   rg   rq   rS   rz   rV   ro   r-   rW   r   r2   ended_atr   rs   ru   rv   r
   r   r?   r@   r(   rh   )r   rl   ri   r   r6   rf   rL   ra   s           r   on_span_end(OpenAIAgentsTracingProcessor.on_span_endK  sx   **..t4C'99$?	$==R8/3~~+,.2mm*+-1\\)*(0*%#--	26"x4!%t9 _!$^!4"%o"6-1ZZ#djj/T#!#!$^!4
" ==+3+A+A$--+PHZ(dnng.F.FGG3377FP& //> BI//>&&22; r    c                 8    U R                   R                  5         g r   r(   flushr   s    r   shutdown%OpenAIAgentsTracingProcessor.shutdownl      KKr    c                 8    U R                   R                  5         g r   r   r   s    r   force_flush(OpenAIAgentsTracingProcessor.force_flusho  r   r    )r?   r@   r;   r>   r=   rA   r<   r(   r   )rC   N)r"   r#   r$   r%   r&   r   	ls_clientClientrW   listr2   r   r
   r   rc   rj   r   r   r   r   r   r'   r!   r    r   r   r   f   s    6	t 26	0 (,(,*."&	0Y--.	0 tn		0
 49%	0 #3-	0 3-	0&@	C @	C$ @	CD	Ggmm 	G 	G(A	Bgll A	Bt A	BF	3GLL 	3T 	3B	 	 r    )loggingr   r   typingr   r   uuidr   	langsmithr   r9   langsmith.run_helpersr	   agentsr
   requiredallr   langsmith.wrappers._agent_utilswrappers_agent_utilsrS   HAVE_AGENTSr   r(   r   	getLoggerr"   rZ   r*   r   r!   r    r   <module>r      s     ' &  % 6KH ;(;;;BCC99K@ *			8	$) J w'?'? J  G  <K9 9<s   /B B-,B-