3232
3333logger = logging .getLogger (__name__ )
3434
35+ _LANGSMITH_UNSUPPORTED = "LangSmith features are not supported by AgentOpsAdapter"
36+
3537
3638class AgentOpsAdapter (MultiAgentAdapter , ProductionIntegration ):
3739 """
@@ -67,21 +69,9 @@ def __init__(
6769 self .project_id = project_id
6870 self .default_tags = default_tags or []
6971 self .auto_start_session = auto_start_session
72+ self .enable_tracing : bool = kwargs .get ("enable_tracing" , True )
73+ self .client : Any = None
7074 self .platform = MonitoringPlatform .AGENTOPS
71- """
72- Initialize LangSmith adapter.
73-
74- Args:
75- api_key: LangSmith API key
76- project_name: Project name for organizing traces
77- endpoint: Optional custom LangSmith endpoint
78- enable_tracing: Whether to enable automatic tracing
79- enable_feedback: Whether to collect feedback data
80- """
81- self .api_key = api_key
82- self .project_id = project_id
83- self .default_tags = default_tags or []
84- self .auto_start_session = auto_start_session
8575
8676 # Initialize AgentOps client
8777 self ._initialize_agentops ()
@@ -98,21 +88,18 @@ def _initialize_agentops(self):
9888 """Initialize AgentOps client and verify connection."""
9989 try :
10090 # Import AgentOps SDK
101- import agentops
102-
103- self .agentops = agentops
91+ import agentops # type: ignore[import-not-found]
10492
10593 # Initialize AgentOps
106- if self .api_key :
107- agentops .init (
108- api_key = self .api_key ,
109- default_tags = self .default_tags ,
110- auto_start_session = self .auto_start_session ,
111- )
112- else :
113- agentops .init (
114- default_tags = self .default_tags , auto_start_session = self .auto_start_session
115- )
94+ session = agentops .init (
95+ api_key = self .api_key ,
96+ default_tags = self .default_tags ,
97+ auto_start_session = self .auto_start_session ,
98+ )
99+
100+ self .agentops = agentops
101+ self .session = session
102+ self .client = session
116103
117104 logger .info ("Successfully connected to AgentOps" )
118105
@@ -124,11 +111,6 @@ def _initialize_agentops(self):
124111 logger .error (f"Failed to connect to AgentOps: { e } " )
125112 raise
126113
127- @property
128- def platform (self ) -> MonitoringPlatform :
129- """Return the monitoring platform type."""
130- return MonitoringPlatform .AGENTOPS
131-
132114 def create_agent (self , role : AgentRole , agent_id : str | None = None , ** kwargs ) -> AgentMetadata :
133115 """
134116 Create an agent for AgentOps monitoring.
@@ -244,7 +226,7 @@ def send_message(
244226 interaction = AgentInteraction (
245227 interaction_id = interaction_id ,
246228 from_agent = from_agent ,
247- to_agent = to_agent ,
229+ to_agent = to_agent or "broadcast" ,
248230 content = message ,
249231 timestamp = timestamp ,
250232 metadata = metadata or {},
@@ -339,12 +321,15 @@ def calculate_coordination_metrics(self) -> dict[str, float]:
339321 for interaction in self .session_interactions :
340322 unique_agents .add (interaction .from_agent )
341323 if interaction .to_agent :
342- unique_agents .add (interaction .to_agent )
324+ if isinstance (interaction .to_agent , list ):
325+ unique_agents .update (interaction .to_agent )
326+ else :
327+ unique_agents .add (interaction .to_agent )
343328
344329 agent_participation = len (unique_agents )
345330
346331 # Calculate message distribution
347- agent_counts = {}
332+ agent_counts : dict [ str , int ] = {}
348333 for interaction in self .session_interactions :
349334 from_agent = interaction .from_agent
350335 agent_counts [from_agent ] = agent_counts .get (from_agent , 0 ) + 1
@@ -374,34 +359,38 @@ def calculate_coordination_metrics(self) -> dict[str, float]:
374359 else 0.0 ,
375360 }
376361
377- def run_scenario (self , scenario : Scenario ) -> ScenarioResult :
362+ async def run_scenario (self , scenario : Scenario ) -> ScenarioResult :
378363 """
379- Run a scenario with LangSmith integration.
364+ Run a scenario with AgentOps integration.
380365
381366 Args:
382367 scenario: Scenario to execute
383368
384369 Returns:
385- ScenarioResult: Execution results with LangSmith trace data
370+ ScenarioResult: Execution results with AgentOps trace data
386371 """
387- logger .info (f"Running scenario with LangSmith: { scenario .name } " )
372+ if self .enable_tracing :
373+ logger .warning (
374+ "run_scenario tracing uses LangSmith-style APIs which are not fully supported by AgentOps"
375+ )
376+
377+ logger .info (f"Running scenario with AgentOps: { scenario .name } " )
388378
389- # Start LangSmith run for the scenario
390- scenario_run_id = None
379+ # Start AgentOps run for the scenario
380+ agentops_trace = None
381+ scenario_run_id : str | None = None
391382 if self .enable_tracing :
392383 try :
393- run = self .client .create_run (
394- name = f"Scenario: { scenario .name } " ,
395- run_type = "chain" ,
396- project_name = self .project_name ,
397- inputs = {"scenario" : scenario .name , "description" : scenario .description },
398- tags = ["agentunit" , "scenario" ],
384+ agentops_trace = self .agentops .start_trace (
385+ trace_name = f"Scenario: { scenario .name } " ,
386+ tags = [* self .default_tags , "agentunit" , "scenario" ],
399387 )
400- scenario_run_id = str (run . id )
388+ scenario_run_id = str (agentops_trace )
401389 except Exception as e :
402- logger .warning (f"Failed to create scenario run : { e } " )
390+ logger .warning (f"Failed to create scenario trace : { e } " )
403391
404392 # Execute scenario (this would typically involve running the actual test)
393+ session_summary : dict [str , Any ] = {}
405394 start_time = time .time ()
406395
407396 try :
@@ -459,9 +448,11 @@ def run_scenario(self, scenario: Scenario) -> ScenarioResult:
459448 # Create trace log
460449 from agentunit .core .trace import TraceLog
461450
462- trace = TraceLog ()
463- trace .record (
464- "scenario_complete" , run_id = scenario_run_id , session_summary = session_summary
451+ trace_log = TraceLog ()
452+ trace_log .record (
453+ "scenario_complete" ,
454+ run_id = scenario_run_id or "unknown" ,
455+ session_summary = session_summary ,
465456 )
466457
467458 # Create scenario run
@@ -473,27 +464,32 @@ def run_scenario(self, scenario: Scenario) -> ScenarioResult:
473464 success = True ,
474465 metrics = session_summary .get ("metrics" , {}),
475466 duration_ms = execution_time * 1000 ,
476- trace = trace ,
467+ trace = trace_log ,
477468 )
478469
479470 # Create result
480471 result = ScenarioResult (name = scenario .name )
481472 result .add_run (scenario_run )
482473
483- # Update LangSmith run with results
484- if scenario_run_id and self .enable_tracing :
474+ # Update AgentOps run with results
475+ trace_metadata = {
476+ "scenario_name" : scenario .name ,
477+ "metrics" : session_summary .get ("metrics" , {}),
478+ "success" : True ,
479+ }
480+
481+ if trace_log and self .enable_tracing :
485482 try :
486- self .client .update_run (
487- run_id = scenario_run_id ,
488- outputs = {
489- "result" : result .passed ,
490- "execution_time" : execution_time ,
491- "details" : result .details ,
492- },
493- end_time = datetime .now (timezone .utc ),
483+ self .agentops .update_trace_metadata (
484+ trace_metadata ,
485+ prefix = "trace.metadata" ,
486+ )
487+ self .agentops .end_trace (
488+ trace = agentops_trace ,
489+ end_state = "success" ,
494490 )
495491 except Exception as e :
496- logger .warning (f"Failed to update scenario run : { e } " )
492+ logger .warning (f"Failed to finalize scenario trace : { e } " )
497493
498494 logger .info (f"Scenario completed: { scenario .name } " )
499495 return result
@@ -504,8 +500,8 @@ def run_scenario(self, scenario: Scenario) -> ScenarioResult:
504500 # Create trace log
505501 from agentunit .core .trace import TraceLog
506502
507- trace = TraceLog ()
508- trace .record ("scenario_error" , error = str (e ), run_id = scenario_run_id )
503+ trace_log = TraceLog ()
504+ trace_log .record ("scenario_error" , error = str (e ), run_id = scenario_run_id or "unknown" )
509505
510506 # Create scenario run
511507 from agentunit .reporting .results import ScenarioRun
@@ -516,30 +512,40 @@ def run_scenario(self, scenario: Scenario) -> ScenarioResult:
516512 success = False ,
517513 metrics = {},
518514 duration_ms = (time .time () - start_time ) * 1000 ,
519- trace = trace ,
515+ trace = trace_log ,
520516 error = str (e ),
521517 )
522518
523519 # Create result
524520 result = ScenarioResult (name = scenario .name )
525521 result .add_run (scenario_run )
526522
527- # Update LangSmith run with error
528- if scenario_run_id and self .enable_tracing :
523+ # Update AgentOps run with error
524+ trace_metadata = {
525+ "scenario_name" : scenario .name ,
526+ "metrics" : session_summary .get ("metrics" , {}),
527+ "success" : False ,
528+ "error" : str (e ),
529+ }
530+
531+ if trace_log and self .enable_tracing :
529532 try :
530- self .client .update_run (
531- run_id = scenario_run_id ,
532- outputs = {"error" : str (e )},
533- end_time = datetime .now (timezone .utc ),
533+ self .agentops .update_trace_metadata (
534+ trace_metadata ,
535+ prefix = "trace.metadata" ,
536+ )
537+ self .agentops .end_trace (
538+ trace = agentops_trace ,
539+ end_state = "failed" ,
534540 )
535541 except Exception as e :
536- logger .warning (f"Failed to update failed scenario run : { e } " )
542+ logger .warning (f"Failed to finalize failed scenario trace : { e } " )
537543
538544 return result
539545
540546 def collect_metrics (self , scenario : Any , result : Any , ** kwargs ) -> ProductionMetrics :
541547 """
542- Collect production metrics from LangSmith .
548+ Collect production metrics from AgentOps .
543549
544550 Args:
545551 scenario: The scenario being evaluated
@@ -549,9 +555,12 @@ def collect_metrics(self, scenario: Any, result: Any, **kwargs) -> ProductionMet
549555 Returns:
550556 ProductionMetrics: Current production metrics
551557 """
558+
559+ raise NotImplementedError (_LANGSMITH_UNSUPPORTED )
560+
552561 try :
553- # Query recent runs from LangSmith
554- runs = list (self .client .list_runs (project_name = self .project_name , limit = 100 ))
562+ # Query recent runs from AgentOps
563+ runs = list (self .client .list_runs (project_name = self .project_id , limit = 100 ))
555564
556565 if not runs :
557566 return ProductionMetrics (
@@ -626,6 +635,8 @@ def establish_baseline(
626635 Returns:
627636 BaselineMetrics: Calculated baseline metrics
628637 """
638+ raise NotImplementedError (_LANGSMITH_UNSUPPORTED )
639+
629640 days = kwargs .get ("days" , 7 )
630641 try :
631642 from datetime import timedelta
@@ -637,7 +648,7 @@ def establish_baseline(
637648 # Query historical runs
638649 runs = list (
639650 self .client .list_runs (
640- project_name = self .project_name , start_time = start_date , end_time = end_date
651+ project_name = self .project_id , start_time = start_date , end_time = end_date
641652 )
642653 )
643654
@@ -680,7 +691,7 @@ def establish_baseline(
680691 )
681692
682693 def _extract_run_metrics (self , runs ):
683- """Extract metrics from LangSmith runs."""
694+ """Extract metrics from AgentOps runs."""
684695 durations = []
685696 token_counts = []
686697 success_count = 0
@@ -717,7 +728,7 @@ def create_evaluation_dataset(
717728 self , name : str , examples : list [dict [str , Any ]], description : str | None = None
718729 ) -> str :
719730 """
720- Create an evaluation dataset in LangSmith .
731+ Create an evaluation dataset in AgentOps .
721732
722733 Args:
723734 name: Dataset name
@@ -727,6 +738,9 @@ def create_evaluation_dataset(
727738 Returns:
728739 str: Created dataset ID
729740 """
741+
742+ raise NotImplementedError (_LANGSMITH_UNSUPPORTED )
743+
730744 try :
731745 dataset = self .client .create_dataset (
732746 dataset_name = name ,
@@ -749,9 +763,9 @@ def create_evaluation_dataset(
749763 logger .error (f"Failed to create LangSmith dataset: { e } " )
750764 raise
751765
752- def run_evaluation (self , dataset_id : str , evaluator_function : Any , ** kwargs ) -> dict [ str , Any ] :
766+ def run_evaluation (self , dataset_id : str , evaluator_function : Any , ** kwargs ) -> Any :
753767 """
754- Run evaluation on a LangSmith dataset.
768+ Run evaluation on a AgentOps dataset.
755769
756770 Args:
757771 dataset_id: Dataset ID to evaluate
@@ -761,13 +775,16 @@ def run_evaluation(self, dataset_id: str, evaluator_function: Any, **kwargs) ->
761775 Returns:
762776 Dict[str, Any]: Evaluation results
763777 """
778+
779+ raise NotImplementedError (_LANGSMITH_UNSUPPORTED )
780+
764781 try :
765782 from langsmith .evaluation import evaluate
766783
767784 results = evaluate (
768785 evaluator_function ,
769786 data = dataset_id ,
770- project_name = f"{ self .project_name } -evaluation" ,
787+ experiment_prefix = f"{ self .project_id } -evaluation" ,
771788 ** kwargs ,
772789 )
773790
0 commit comments