Python SDK for AI agent monitoring, LLM cost tracking, benchmarking, and more. Integrates with most LLMs and agent frameworks including CrewAI, Agno, OpenAI Agents SDK, Langchain, Autogen, AG2, and CamelAI
Python agentagentopsagents-sdkaianthropicautogencost-estimationcrewaievalsevaluation-metricsgroqlangchainllmmistralollamaopenaiopenai-agents
Fix Examples (#1145)
Dwijb47cf2f437ec
diff --git a/.github/workflows/examples-integration-test.yml b/.github/workflows/examples-integration-test.ymlindex ccddbd19..0458311f 100644--- a/.github/workflows/examples-integration-test.yml+++ b/.github/workflows/examples-integration-test.yml@@ -67,12 +67,6 @@ jobs:- { path: 'examples/ag2/async_human_input.py', name: 'AG2 Async Human Input' }- { path: 'examples/ag2/tools_wikipedia_search.py', name: 'AG2 Wikipedia Search' }- # Context Manager examples- - { path: 'examples/context_manager/basic_usage.py', name: 'Context Manager Basic' }- - { path: 'examples/context_manager/error_handling.py', name: 'Context Manager Errors' }- - { path: 'examples/context_manager/parallel_traces.py', name: 'Context Manager Parallel' }- - { path: 'examples/context_manager/production_patterns.py', name: 'Context Manager Production' }-# Agno examples- { path: 'examples/agno/agno_async_operations.py', name: 'Agno Async Operations' }- { path: 'examples/agno/agno_basic_agents.py', name: 'Agno Basic Agents' }@@ -84,7 +78,7 @@ jobs:- { path: 'examples/google_adk/human_approval.py', name: 'Google ADK Human Approval' }# LlamaIndex examples- - { path: 'examples/llamaindex/llamaindex_example.py', name: 'LlamaIndex' }+ # - { path: 'examples/llamaindex/llamaindex_example.py', name: 'LlamaIndex' }# Mem0 examples- { path: 'examples/mem0/mem0_memoryclient_example.py', name: 'Mem0 Memory Client' }@@ -157,6 +151,7 @@ jobs:LLAMA_API_KEY: ${{ secrets.LLAMA_API_KEY }}PERPLEXITY_API_KEY: ${{ secrets.PERPLEXITY_API_KEY }}REPLICATE_API_TOKEN: ${{ secrets.REPLICATE_API_TOKEN }}+ PINECONE_API_KEY: ${{ secrets.PINECONE_API_KEY }}PYTHONPATH: ${{ github.workspace }}run: |echo "Running ${{ matrix.example.name }}..."diff --git a/examples/ag2/async_human_input.py b/examples/ag2/async_human_input.pyindex a534b475..f07ced61 100644--- a/examples/ag2/async_human_input.py+++ b/examples/ag2/async_human_input.py@@ -1,8 +1,9 @@-# Agent Chat with Async Human Inputs+# Agent Chat with Async Operations#-# We are going to create an agent that can chat with a human asynchronously. The agent will be able to respond to messages from the human and will also be able to send messages to the human.+# We are going to create agents that can perform asynchronous operations and chat with each other.+# This example demonstrates async capabilities without requiring human input.#-# We are going to use AgentOps to monitor the agent's performance and observe its interactions with the human.+# We are going to use AgentOps to monitor the agent's performance and observe their interactions.# # Install required dependencies# %pip install agentops# %pip install ag2@@ -25,29 +26,45 @@ load_dotenv()os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY", "your_api_key_here")os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "your_openai_api_key_here")-agentops.init(auto_start_session=False, trace_name="AG2 Async Human Input")-tracer = agentops.start_trace(- trace_name="AG2 Agent chat with Async Human Inputs", tags=["ag2-chat-async-human-inputs", "agentops-example"]-)+agentops.init(auto_start_session=False, trace_name="AG2 Async Demo")+tracer = agentops.start_trace(trace_name="AG2 Async Agent Demo", tags=["ag2-async-demo", "agentops-example"])-# Define an asynchronous function that simulates some asynchronous task (e.g., I/O operation)-async def my_asynchronous_function():- print("Start asynchronous function")- await asyncio.sleep(2) # Simulate some asynchronous task (e.g., I/O operation)- print("End asynchronous function")- return "input"+# Define an asynchronous function that simulates async processing+async def simulate_async_processing(task_name: str, delay: float = 1.0) -> str:+ """+ Simulate some asynchronous processing (e.g., API calls, file operations, etc.)+ """+ print(f"🔄 Starting async task: {task_name}")+ await asyncio.sleep(delay) # Simulate async work+ print(f"✅ Completed async task: {task_name}")+ return f"Processed: {task_name}"-# Define a custom class `CustomisedUserProxyAgent` that extends `UserProxyAgent`-class CustomisedUserProxyAgent(UserProxyAgent):- # Asynchronous function to get human input+# Define a custom UserProxyAgent that simulates automated responses+class AutomatedUserProxyAgent(UserProxyAgent):+ def __init__(self, name: str, **kwargs):+ super().__init__(name, **kwargs)+ self.response_count = 0+ self.predefined_responses = [+ "Yes, please generate interview questions for these topics.",+ "The questions look good. Can you make them more specific to senior-level positions?",+ "Perfect! These questions are exactly what we need. Thank you!",+ ]+async def a_get_human_input(self, prompt: str) -> str:- # Call the asynchronous function to get user input asynchronously- user_input = await my_asynchronous_function()- return user_input+ # Simulate async processing before responding+ await simulate_async_processing(f"Processing user input #{self.response_count + 1}")++ if self.response_count < len(self.predefined_responses):+ response = self.predefined_responses[self.response_count]+ self.response_count += 1+ print(f"👤 User: {response}")+ return response+ else:+ print("👤 User: TERMINATE")+ return "TERMINATE"- # Asynchronous function to receive a messageasync def a_receive(self,message: Union[Dict, str],@@ -55,18 +72,10 @@ class CustomisedUserProxyAgent(UserProxyAgent):request_reply: Optional[bool] = None,silent: Optional[bool] = False,):- # Call the superclass method to handle message reception asynchronouslyawait super().a_receive(message, sender, request_reply, silent)-class CustomisedAssistantAgent(AssistantAgent):- # Asynchronous function to get human input- async def a_get_human_input(self, prompt: str) -> str:- # Call the asynchronous function to get user input asynchronously- user_input = await my_asynchronous_function()- return user_input-- # Asynchronous function to receive a message+class AsyncAssistantAgent(AssistantAgent):async def a_receive(self,message: Union[Dict, str],@@ -74,7 +83,8 @@ class CustomisedAssistantAgent(AssistantAgent):request_reply: Optional[bool] = None,silent: Optional[bool] = False,):- # Call the superclass method to handle message reception asynchronously+ # Simulate async processing before responding+ await simulate_async_processing("Analyzing request and preparing response", 0.5)await super().a_receive(message, sender, request_reply, silent)@@ -82,35 +92,58 @@ nest_asyncio.apply()async def main():- boss = CustomisedUserProxyAgent(- name="boss",- human_input_mode="ALWAYS",- max_consecutive_auto_reply=0,+ print("🚀 Starting AG2 Async Demo")+ print("=" * 50)++ # Create agents with automated behavior+ user_proxy = AutomatedUserProxyAgent(+ name="hiring_manager",+ human_input_mode="NEVER", # No human input required+ max_consecutive_auto_reply=3,code_execution_config=False,+ is_termination_msg=lambda msg: "TERMINATE" in str(msg.get("content", "")),)- assistant = CustomisedAssistantAgent(- name="assistant",- system_message="You will provide some agenda, and I will create questions for an interview meeting. Every time when you generate question then you have to ask user for feedback and if user provides the feedback then you have to incorporate that feedback and generate new set of questions and if user don't want to update then terminate the process and exit",+ assistant = AsyncAssistantAgent(+ name="interview_consultant",+ system_message="""You are an expert interview consultant. When given interview topics,+ you create thoughtful, relevant questions. You ask for feedback and incorporate it.+ When the user is satisfied with the questions, end with 'TERMINATE'.""",llm_config={"config_list": [{"model": "gpt-4o-mini", "api_key": os.environ.get("OPENAI_API_KEY")}]},+ is_termination_msg=lambda msg: "TERMINATE" in str(msg.get("content", "")),)- await boss.a_initiate_chat(- assistant,- message="Resume Review, Technical Skills Assessment, Project Discussion, Job Role Expectations, Closing Remarks.",- n_results=3,- )---# await main()-agentops.end_trace(tracer, end_state="Success")--# Let's check programmatically that spans were recorded in AgentOps-print("\n" + "=" * 50)-print("Now let's verify that our LLM calls were tracked properly...")-try:- agentops.validate_trace_spans(trace_context=tracer)- print("\n✅ Success! All LLM spans were properly recorded in AgentOps.")-except agentops.ValidationError as e:- print(f"\n❌ Error validating spans: {e}")- raise+ try:+ print("🤖 Initiating automated conversation...")+ await user_proxy.a_initiate_chat(+ assistant,+ message="""I need help creating interview questions for these topics:+ - Resume Review+ - Technical Skills Assessment+ - Project Discussion+ - Job Role Expectations+ - Closing Remarks++ Please create 2-3 questions for each topic.""",+ max_turns=6,+ )+ except Exception as e:+ print(f"\n❌ Error occurred: {e}")+ finally:+ agentops.end_trace(tracer, end_state="Success")++ # Validate AgentOps tracking+ print("\n" + "=" * 50)+ print("🔍 Validating AgentOps tracking...")+ try:+ agentops.validate_trace_spans(trace_context=tracer)+ print("✅ Success! All LLM spans were properly recorded in AgentOps.")+ except agentops.ValidationError as e:+ print(f"❌ Error validating spans: {e}")+ raise++ print("\n🎉 Demo completed successfully!")+++if __name__ == "__main__":+ asyncio.run(main())diff --git a/examples/ag2/requirements.txt b/examples/ag2/requirements.txtindex 4359d0e3..425a8331 100644--- a/examples/ag2/requirements.txt+++ b/examples/ag2/requirements.txt@@ -1,3 +1,4 @@ag2nest-asyncio-wikipedia-api\ No newline at end of file+wikipedia-api+ag2[openai]\ No newline at end of filediff --git a/examples/agno/agno_workflow_setup.py b/examples/agno/agno_workflow_setup.pyindex 42f44d64..a2d21967 100644--- a/examples/agno/agno_workflow_setup.py+++ b/examples/agno/agno_workflow_setup.py@@ -16,7 +16,6 @@ By using workflows, you can create sophisticated agent pipelines that are both p"""from agno.agent import Agent, RunResponse-import asyncioimport agentopsfrom dotenv import load_dotenvfrom agno.workflow import Workflow@@ -124,4 +123,4 @@ def demonstrate_workflows():raise-asyncio.run(demonstrate_workflows())+demonstrate_workflows()diff --git a/examples/agno/requirements.txt b/examples/agno/requirements.txtindex 96ab8a51..58d6a0cf 100644--- a/examples/agno/requirements.txt+++ b/examples/agno/requirements.txt@@ -1,2 +1,8 @@agno-aiohttp\ No newline at end of file+aiohttp+openai+googlesearch-python+pycountry+arxiv+pypdf+duckduckgo-search\ No newline at end of filediff --git a/examples/autogen/AgentChat.py b/examples/autogen/AgentChat.pyindex 5958e90d..347d9586 100644--- a/examples/autogen/AgentChat.py+++ b/examples/autogen/AgentChat.py@@ -1,5 +1,6 @@-# Microsoft Autogen Chat Example+# Microsoft Autogen Multi-Agent Collaboration Example#+# This example demonstrates AI-to-AI collaboration using multiple specialized agents working together without human interaction.# AgentOps automatically configures itself when it's initialized meaning your agent run data will be tracked and logged to your AgentOps dashboard right away.# First let's install the required packages# %pip install -U autogen-agentchat@@ -13,7 +14,7 @@ import asyncioimport agentops-from autogen_agentchat.agents import AssistantAgent, UserProxyAgent+from autogen_agentchat.agents import AssistantAgentfrom autogen_ext.models.openai import OpenAIChatCompletionClientfrom autogen_agentchat.teams import RoundRobinGroupChat@@ -32,9 +33,10 @@ os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY", "your_api_key_heros.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "your_openai_api_key_here")# When initializing AgentOps, you can pass in optional tags to help filter sessions-agentops.init(auto_start_session=False, trace_name="Autogen Agent Chat Example")+agentops.init(auto_start_session=False, trace_name="Autogen Multi-Agent Collaboration Example")tracer = agentops.start_trace(- trace_name="Microsoft Agent Chat Example", tags=["autogen-chat", "microsoft-autogen", "agentops-example"]+ trace_name="Microsoft Multi-Agent Collaboration Example",+ tags=["autogen-collaboration", "microsoft-autogen", "agentops-example"],)# AutoGen will now start automatically tracking@@ -45,7 +47,7 @@ tracer = agentops.start_trace(# * Correspondence between agents# * Tool usage# * Errors-# # Simple Chat Example+# # Multi-Agent Collaboration Example# Define model and API keymodel_name = "gpt-4o-mini" # Or "gpt-4o" / "gpt-4o-mini" as per migration guide examplesapi_key = os.getenv("OPENAI_API_KEY")@@ -53,30 +55,45 @@ api_key = os.getenv("OPENAI_API_KEY")# Create the model clientmodel_client = OpenAIChatCompletionClient(model=model_name, api_key=api_key)-# Create the agent that uses the LLM.-assistant = AssistantAgent(- name="assistant",- system_message="You are a helpful assistant.", # Added system message for clarity+# Create multiple AI agents with different roles+research_agent = AssistantAgent(+ name="research_agent",+ system_message="You are a research specialist. Your role is to gather information, analyze data, and provide insights on topics. You ask thoughtful questions and provide well-researched responses.",model_client=model_client,)-user_proxy_initiator = UserProxyAgent("user_initiator")+creative_agent = AssistantAgent(+ name="creative_agent",+ system_message="You are a creative strategist. Your role is to brainstorm innovative solutions, think outside the box, and propose creative approaches to problems. You build on others' ideas and suggest novel perspectives.",+ model_client=model_client,+)++analyst_agent = AssistantAgent(+ name="analyst_agent",+ system_message="You are a critical analyst. Your role is to evaluate ideas, identify strengths and weaknesses, and provide constructive feedback. You help refine concepts and ensure practical feasibility.",+ model_client=model_client,+)async def main():- termination = MaxMessageTermination(max_messages=2)+ # Set up a longer conversation to allow for meaningful AI-to-AI interaction+ termination = MaxMessageTermination(max_messages=8)group_chat = RoundRobinGroupChat(- [user_proxy_initiator, assistant], # Corrected: agents as positional argument+ [research_agent, creative_agent, analyst_agent], # AI agents working togethertermination_condition=termination,)- chat_task = "How can I help you today?"- print(f"User Initiator: {chat_task}")+ # A task that will engage all three agents in meaningful collaboration+ chat_task = "Let's develop a comprehensive strategy for reducing plastic waste in urban environments. I need research on current methods, creative solutions, and analysis of feasibility."+ print(f"🎯 Task: {chat_task}")+ print("\n" + "=" * 80)+ print("🤖 AI Agents Collaboration Starting...")+ print("=" * 80)try:stream = group_chat.run_stream(task=chat_task)- await Console().run(stream)+ await Console(stream=stream)agentops.end_trace(tracer, end_state="Success")except Exception as e:@@ -112,4 +129,4 @@ if __name__ == "__main__":# You can view data on this run at [app.agentops.ai](app.agentops.ai).#-# The dashboard will display LLM events for each message sent by each agent, including those made by the human user.+# The dashboard will display LLM events for each message sent by each agent, showing the full AI-to-AI collaboration process with research, creative, and analytical perspectives.diff --git a/examples/autogen/requirements.txt b/examples/autogen/requirements.txtindex 032705de..c0565f05 100644--- a/examples/autogen/requirements.txt+++ b/examples/autogen/requirements.txt@@ -1 +1,2 @@-pyautogen\ No newline at end of file+autogen-agentchat==0.6.1+autogen-ext[openai]\ No newline at end of filediff --git a/examples/context_manager/README.md b/examples/context_manager/README.mddeleted file mode 100644index 5ed88c88..00000000--- a/examples/context_manager/README.md+++ /dev/null@@ -1,93 +0,0 @@-# AgentOps Context Manager Examples--This directory contains examples demonstrating how to use AgentOps with context managers for automatic trace lifecycle management using the simplified, native implementation.--## Overview--AgentOps provides native context manager support through the `TraceContext` class. This implementation eliminates wrapper classes and leverages OpenTelemetry's built-in capabilities for clean, automatic trace management.--## Key Benefits--- **Automatic cleanup**: Traces end automatically when exiting the context-- **Error handling**: Traces are marked with appropriate error states when exceptions occur-- **Native OpenTelemetry integration**: Leverages OpenTelemetry's built-in context management-- **Simplified architecture**: No wrapper classes or monkey patching required-- **Thread safety**: Works seamlessly in multi-threaded environments-- **Performance optimized**: Direct method calls without wrapper overhead--## Quick Start--```python-import agentops--# Initialize AgentOps-agentops.init(api_key="your-api-key")--# Use native context manager support-with agentops.start_trace("my_task") as trace:- # Your code here- pass # Trace automatically ends with Success/Error state-```--## Examples--### `basic_usage.py`-- Native TraceContext context manager usage-- Multiple parallel traces-- Error handling basics-- Comparison with OpenTelemetry concepts--### `parallel_traces.py`-- Sequential and nested parallel traces-- Concurrent traces with threading and ThreadPoolExecutor-- Mixed success/error scenarios-- Different tag types (lists, dictionaries, none)--### `error_handling.py`-- Exception handling with different error types-- Nested exception handling and propagation-- Recovery patterns (retry, graceful degradation, partial success)-- Custom exceptions, finally blocks, and exception chaining--### `production_patterns.py`-- API endpoint monitoring and metrics-- Batch processing with individual item tracking-- Microservice communication patterns-- Production monitoring and alerting--## Running Examples--### Step 1: Install Dependencies--```bash-# Install required packages-pip install agentops python-dotenv-```--### Step 2: Set Up API Key--To use AgentOps and send trace data to your dashboard, you need to configure your API key using one of these methods:--**Option A: Environment Variable**-```bash-# Export directly in your shell-export AGENTOPS_API_KEY="your-api-key-here"-```--**Option B: .env File (Recommended)**-```bash-# Create a .env file in the project root-echo "AGENTOPS_API_KEY=your-api-key-here" > .env-```--**Important**: When using a `.env` file, the examples automatically load it using `load_dotenv()`. Make sure the `.env` file is in the same directory as the example scripts or in the project root.--### Step 3: Run the Examples--```bash-# Run each example to see different patterns-python examples/context_manager/basic_usage.py-python examples/context_manager/parallel_traces.py-python examples/context_manager/error_handling.py-python examples/context_manager/production_patterns.py-```diff --git a/examples/context_manager/basic_usage.py b/examples/context_manager/basic_usage.pydeleted file mode 100644index 94d57c5a..00000000--- a/examples/context_manager/basic_usage.py+++ /dev/null@@ -1,166 +0,0 @@-"""-Basic Context Manager Example--This example demonstrates how to use AgentOps context manager with the native-TraceContext support, eliminating the need for wrappers or monkey patching.-"""--import os-import agentops-from agentops import agent, task, tool-from dotenv import load_dotenv--load_dotenv()--# Get API key from environment-AGENTOPS_API_KEY = os.environ.get("AGENTOPS_API_KEY")---@agent-class SimpleAgent:- """A simple example agent."""-- def __init__(self, name: str):- self.name = name-- @task- def process_data(self, data: str) -> str:- """Process some data."""- result = f"Processed: {data}"- return self.use_tool(result)-- @tool- def use_tool(self, input_data: str) -> str:- """Use a tool to transform data."""- return f"Tool output: {input_data.upper()}"---def basic_context_manager_example():- """Example using the native TraceContext context manager."""- print("Basic Context Manager Example")-- # Initialize AgentOps- agentops.init(- api_key=AGENTOPS_API_KEY,- trace_name="Context Manager Basic Example",- tags=["context-manager", "agentops-example"],- )-- # Use native TraceContext context manager- with agentops.start_trace("Context Manager Basic Example", tags=["basic", "demo"]):- print("Trace started")-- # Create and use agent- agent = SimpleAgent("BasicAgent")- result = agent.process_data("sample data")- print(f"Result: {result}")-- print("Trace ended automatically")---def multiple_parallel_traces():- """Example showing multiple parallel traces."""- print("\nMultiple Parallel Traces")-- agentops.init(- api_key=AGENTOPS_API_KEY,- trace_name="Context Manager Multiple Parallel Traces",- tags=["context-manager", "agentops-example"],- )-- # First trace- with agentops.start_trace("Context Manager Task 1", tags=["parallel", "task-1"]):- print("Task 1 started")- agent1 = SimpleAgent("Agent1")- result1 = agent1.process_data("task 1 data")- print(f"Task 1 result: {result1}")-- # Second trace (independent)- with agentops.start_trace("task_2", tags=["parallel", "task-2"]):- print("Task 2 started")- agent2 = SimpleAgent("Agent2")- result2 = agent2.process_data("task 2 data")- print(f"Task 2 result: {result2}")-- print("All parallel traces completed")---def error_handling_example():- """Example showing error handling with context manager."""- print("\nError Handling Example")-- agentops.init(- api_key=AGENTOPS_API_KEY,- trace_name="Context Manager Error Handling",- tags=["context-manager", "agentops-example"],- )-- try:- with agentops.start_trace("error_example", tags=["error-handling"]):- print("Trace started")-- agent = SimpleAgent("ErrorAgent")- result = agent.process_data("some data")- print(f"Processing successful: {result}")-- # Simulate an error- raise ValueError("Simulated error")-- except ValueError as e:- print(f"Caught error: {e}")- print("Trace automatically ended with Error status")---def nested_traces_example():- """Example showing nested traces (which are parallel, not parent-child)."""- print("\nNested Traces Example")-- agentops.init(- api_key=AGENTOPS_API_KEY,- trace_name="Context Manager Nested Traces",- tags=["context-manager", "agentops-example"],- )-- # Outer trace- with agentops.start_trace("main_workflow", tags=["workflow", "main"]):- print("Main workflow started")-- # Inner trace (parallel, not child)- with agentops.start_trace("sub_task", tags=["workflow", "sub"]):- print("Sub task started")-- agent = SimpleAgent("WorkflowAgent")- result = agent.process_data("workflow data")- print(f"Sub task result: {result}")-- print("Sub task completed")- print("Main workflow completed")---if __name__ == "__main__":- print("AgentOps Context Manager Examples")- print("=" * 40)-- # Show basic usage- basic_context_manager_example()-- # Show multiple parallel traces- multiple_parallel_traces()-- # Show error handling- error_handling_example()-- # Show nested traces- nested_traces_example()-- print("\nAll examples completed!")-- # Let's check programmatically that spans were recorded in AgentOps- print("\n" + "=" * 50)- print("Now let's verify that our LLM calls were tracked properly...")- try:- agentops.validate_trace_spans(trace_context=None)- print("\n✅ Success! All LLM spans were properly recorded in AgentOps.")- except agentops.ValidationError as e:- print(f"\n❌ Error validating spans: {e}")- raisediff --git a/examples/context_manager/error_handling.py b/examples/context_manager/error_handling.pydeleted file mode 100644index f42d0a8e..00000000--- a/examples/context_manager/error_handling.py+++ /dev/null@@ -1,359 +0,0 @@-"""-Error Handling with Context Managers--This example demonstrates error handling patterns with AgentOps context managers,-showing how traces automatically handle different types of exceptions.-"""--import os-import time-import agentops-from agentops import agent, task, tool-from dotenv import load_dotenv--load_dotenv()--# Get API key from environment-AGENTOPS_API_KEY = os.environ.get("AGENTOPS_API_KEY")---@agent-class ErrorProneAgent:- """An agent that can encounter various types of errors."""-- def __init__(self, name: str):- self.name = name-- @task- def risky_operation(self, operation_type: str) -> str:- """Perform a risky operation that might fail."""- if operation_type == "value_error":- raise ValueError("Invalid value provided")- elif operation_type == "type_error":- raise TypeError("Wrong type provided")- elif operation_type == "runtime_error":- raise RuntimeError("Runtime error occurred")- elif operation_type == "custom_error":- raise CustomError("Custom error occurred")- else:- return f"Success: {operation_type}"-- @tool- def validate_input(self, data: str) -> str:- """Validate input data."""- if not data or data == "invalid":- raise ValueError("Invalid input data")- return f"Validated: {data}"-- @task- def multi_step_operation(self, steps: list) -> str:- """Perform multiple steps, any of which might fail."""- results = []- for i, step in enumerate(steps):- if step == "fail":- raise RuntimeError(f"Step {i + 1} failed")- results.append(f"Step{i + 1}:{step}")- return " -> ".join(results)---class CustomError(Exception):- """Custom exception for demonstration."""-- pass---def basic_exception_handling():- """Basic example of exception handling with context managers."""- print("Basic Exception Handling")-- agentops.init(- api_key=AGENTOPS_API_KEY,- trace_name="Context Manager Basic Exception Handling",- tags=["context-manager", "error-handling", "agentops-example"],- )-- error_types = ["value_error", "type_error", "runtime_error", "success"]-- for error_type in error_types:- try:- with agentops.start_trace(f"Context Manager Basic {error_type}", tags=["basic", "error-handling"]):- print(f"Started trace for {error_type}")-- agent = ErrorProneAgent(f"BasicAgent_{error_type}")- result = agent.risky_operation(error_type)- print(f"Success result: {result}")-- except ValueError as e:- print(f"Caught ValueError: {e}")- except TypeError as e:- print(f"Caught TypeError: {e}")- except RuntimeError as e:- print(f"Caught RuntimeError: {e}")-- print("Basic exception handling completed")---def nested_exception_handling():- """Example of exception handling in nested traces."""- print("\nNested Exception Handling")-- agentops.init(- api_key=AGENTOPS_API_KEY,- trace_name="Context Manager Nested Exception Handling",- tags=["context-manager", "error-handling", "agentops-example"],- )-- try:- with agentops.start_trace("outer_operation", tags=["nested", "outer"]):- print("Outer trace started")-- # outer_agent = ErrorProneAgent("OuterAgent") # Not used in this example-- try:- with agentops.start_trace("inner_operation", tags=["nested", "inner"]):- print("Inner trace started")-- inner_agent = ErrorProneAgent("InnerAgent")- # This will cause an error in the inner trace- inner_agent.risky_operation("value_error")-- except ValueError as e:- print(f"Inner trace caught ValueError: {e}")- # Re-raise to affect outer trace- raise RuntimeError("Inner operation failed") from e-- except RuntimeError as e:- print(f"Outer trace caught RuntimeError: {e}")-- print("Nested exception handling completed")---def retry_pattern():- """Example of retry pattern with context managers."""- print("\nRetry Pattern")-- agentops.init(- api_key=AGENTOPS_API_KEY,- trace_name="Context Manager Retry Pattern",- tags=["context-manager", "error-handling", "agentops-example"],- )-- max_retries = 3- for attempt in range(max_retries):- try:- with agentops.start_trace(f"retry_attempt_{attempt + 1}", tags=["retry", f"attempt-{attempt + 1}"]):- print(f"Retry attempt {attempt + 1} started")-- agent = ErrorProneAgent(f"RetryAgent_Attempt{attempt + 1}")-- # Simulate success on the last attempt- if attempt < max_retries - 1:- result = agent.risky_operation("runtime_error")- else:- result = agent.risky_operation("success")-- print(f"Retry attempt {attempt + 1} succeeded: {result}")- break-- except RuntimeError as e:- print(f"Retry attempt {attempt + 1} failed: {e}")- if attempt < max_retries - 1:- wait_time = 2**attempt # Exponential backoff- print(f"Waiting {wait_time}s before retry...")- time.sleep(wait_time * 0.01) # Shortened for demo- else:- print("All retry attempts exhausted")- raise-- print("Retry pattern completed")---def graceful_degradation():- """Example of graceful degradation pattern."""- print("\nGraceful Degradation")-- agentops.init(- api_key=AGENTOPS_API_KEY,- trace_name="Context Manager Graceful Degradation",- tags=["context-manager", "error-handling", "agentops-example"],- )-- try:- with agentops.start_trace("primary_service", tags=["degradation", "primary"]):- print("Primary service trace started")-- agent = ErrorProneAgent("PrimaryAgent")- result = agent.risky_operation("runtime_error")- print(f"Primary service result: {result}")-- except RuntimeError as e:- print(f"Primary service failed: {e}")- print("Falling back to secondary service...")-- with agentops.start_trace("fallback_service", tags=["degradation", "fallback"]):- print("Fallback service trace started")-- fallback_agent = ErrorProneAgent("FallbackAgent")- result = fallback_agent.risky_operation("success")- print(f"Fallback service result: {result}")-- print("Graceful degradation completed")---def partial_success_handling():- """Example of partial success handling."""- print("\nPartial Success Handling")-- agentops.init(- api_key=AGENTOPS_API_KEY,- trace_name="Context Manager Partial Success Handling",- tags=["context-manager", "error-handling", "agentops-example"],- )-- steps = ["step1", "step2", "fail", "step4"]-- with agentops.start_trace("partial_success", tags=["partial", "multi-step"]):- print("Partial success trace started")-- agent = ErrorProneAgent("PartialAgent")-- try:- result = agent.multi_step_operation(steps)- print(f"All steps completed: {result}")- except RuntimeError as e:- print(f"Operation partially failed: {e}")-- print("Partial success handling completed")---def custom_exception_handling():- """Example of handling custom exceptions."""- print("\nCustom Exception Handling")-- agentops.init(- api_key=AGENTOPS_API_KEY,- trace_name="Context Manager Custom Exception Handling",- tags=["context-manager", "error-handling", "agentops-example"],- )-- try:- with agentops.start_trace("custom_exception", tags=["custom", "exception"]):- print("Custom exception trace started")-- agent = ErrorProneAgent("CustomAgent")- result = agent.risky_operation("custom_error")- print(f"Result: {result}")-- except CustomError as e:- print(f"Caught custom exception: {e}")- except Exception as e:- print(f"Caught unexpected exception: {e}")-- print("Custom exception handling completed")---def finally_blocks_example():- """Example of exception handling with finally blocks."""- print("\nFinally Blocks Example")-- agentops.init(- api_key=AGENTOPS_API_KEY,- trace_name="Context Manager Finally Blocks",- tags=["context-manager", "error-handling", "agentops-example"],- )-- cleanup_actions = []-- try:- with agentops.start_trace("finally_example", tags=["finally", "cleanup"]):- print("Finally example trace started")-- agent = ErrorProneAgent("FinallyAgent")-- try:- result = agent.risky_operation("value_error")- print(f"Result: {result}")- finally:- cleanup_actions.append("Inner cleanup executed")- print("Inner finally block executed")-- except ValueError as e:- print(f"Caught exception: {e}")- finally:- cleanup_actions.append("Outer cleanup executed")- print("Outer finally block executed")-- print(f"Cleanup actions performed: {cleanup_actions}")- print("Finally block handling completed")---def exception_chaining_example():- """Example of exception chaining and context preservation."""- print("\nException Chaining Example")-- agentops.init(- api_key=AGENTOPS_API_KEY,- trace_name="Context Manager Exception Chaining",- tags=["context-manager", "error-handling", "agentops-example"],- )-- try:- with agentops.start_trace("exception_chaining", tags=["chaining", "context"]):- print("Exception chaining trace started")-- agent = ErrorProneAgent("ChainingAgent")-- try:- # First operation fails- agent.validate_input("invalid")- except ValueError as e:- print(f"Validation failed: {e}")- # Chain the exception with additional context- raise RuntimeError("Operation failed due to validation error") from e-- except RuntimeError as e:- print(f"Caught chained exception: {e}")- print(f"Original cause: {e.__cause__}")-- print("Exception chaining completed")---if __name__ == "__main__":- print("AgentOps Error Handling Examples")- print("=" * 40)-- # Basic exception handling- basic_exception_handling()-- # Nested exception handling- nested_exception_handling()-- # Retry pattern- retry_pattern()-- # Graceful degradation- graceful_degradation()-- # Partial success handling- partial_success_handling()-- # Custom exception handling- custom_exception_handling()-- # Finally blocks- finally_blocks_example()-- # Exception chaining- exception_chaining_example()-- print("\nAll error handling examples completed!")-- # Let's check programmatically that spans were recorded in AgentOps- print("\n" + "=" * 50)- print("Now let's verify that our LLM calls were tracked properly...")- try:- agentops.validate_trace_spans(trace_context=None)- print("\n✅ Success! All LLM spans were properly recorded in AgentOps.")- except agentops.ValidationError as e:- print(f"\n❌ Error validating spans: {e}")- raisediff --git a/examples/context_manager/parallel_traces.py b/examples/context_manager/parallel_traces.pydeleted file mode 100644index f8436a44..00000000--- a/examples/context_manager/parallel_traces.py+++ /dev/null@@ -1,263 +0,0 @@-"""-Parallel Traces Example--This example demonstrates how AgentOps context managers create independent-parallel traces rather than parent-child relationships, which is ideal for-concurrent operations and workflow management.-"""--import os-import time-import threading-import concurrent.futures-import agentops-from agentops import agent, task, tool-from dotenv import load_dotenv--load_dotenv()--# Get API key from environment-AGENTOPS_API_KEY = os.environ.get("AGENTOPS_API_KEY")---@agent-class WorkerAgent:- """A worker agent that can process tasks independently."""-- def __init__(self, worker_id: str):- self.worker_id = worker_id-- @task- def process_task(self, task_data: str) -> str:- """Process a task with some simulated work."""- # Simulate some work- time.sleep(0.1)-- result = self.analyze_data(task_data)- return self.finalize_result(result)-- @tool- def analyze_data(self, data: str) -> str:- """Analyze the input data."""- return f"Analyzed: {data}"-- @tool- def finalize_result(self, analyzed_data: str) -> str:- """Finalize the processing result."""- return f"Final: {analyzed_data.upper()}"---def sequential_parallel_traces():- """Example of sequential parallel traces - each trace is independent."""- print("Sequential Parallel Traces")-- agentops.init(- api_key=AGENTOPS_API_KEY,- trace_name="Context Manager Sequential Parallel Traces",- tags=["context-manager", "agentops-example"],- )-- tasks = ["task_1", "task_2", "task_3"]- results = []-- for i, task_name in enumerate(tasks):- # Each trace is completely independent- with agentops.start_trace(f"sequential_{task_name}", tags=["sequential", f"step-{i + 1}"]):- print(f"Started trace for {task_name}")-- worker = WorkerAgent(f"Worker{i + 1}")- result = worker.process_task(f"data_for_{task_name}")- results.append(result)-- print(f"{task_name} result: {result}")-- print("All sequential traces completed")- print(f"Final results: {results}")---def nested_parallel_traces():- """Example showing that nested context managers create parallel traces."""- print("\nNested Parallel Traces")-- agentops.init(- api_key=AGENTOPS_API_KEY,- trace_name="Context Manager Nested Parallel Traces",- tags=["context-manager", "agentops-example"],- )-- # Outer trace for the overall workflow- with agentops.start_trace("workflow_main", tags=["workflow", "main"]):- print("Main workflow trace started")-- # Inner trace for data preparation (parallel, not child)- with agentops.start_trace("data_preparation", tags=["workflow", "preparation"]):- print("Data preparation trace started (parallel to main)")-- prep_worker = WorkerAgent("PrepWorker")- prep_result = prep_worker.process_task("raw_data")- print(f"Preparation result: {prep_result}")-- print("Data preparation trace ended")-- # Another inner trace for data processing (also parallel)- with agentops.start_trace("data_processing", tags=["workflow", "processing"]):- print("Data processing trace started (parallel to main)")-- proc_worker = WorkerAgent("ProcessWorker")- proc_result = proc_worker.process_task("prepared_data")- print(f"Processing result: {proc_result}")-- print("Data processing trace ended")- print("Main workflow completed")-- print("Main workflow trace ended")- print("All traces were independent/parallel, not parent-child relationships")---def concurrent_traces_with_threads():- """Example of truly concurrent traces using threading."""- print("\nConcurrent Traces with Threading")-- agentops.init(- api_key=AGENTOPS_API_KEY,- trace_name="Context Manager Concurrent Threads",- tags=["context-manager", "agentops-example"],- )-- def worker_function(worker_id: int, task_data: str):- """Function to run in a separate thread with its own trace."""- trace_name = f"concurrent_worker_{worker_id}"-- with agentops.start_trace(trace_name, tags=["concurrent", f"worker-{worker_id}"]):- print(f"Thread {worker_id}: Trace started")-- worker = WorkerAgent(f"ConcurrentWorker{worker_id}")- result = worker.process_task(task_data)-- # Simulate varying work times- time.sleep(0.05 * worker_id)-- print(f"Thread {worker_id}: Result: {result}")- return result-- # Start multiple threads, each with their own trace- threads = []- results = []-- for i in range(3):- thread = threading.Thread(target=lambda i=i: results.append(worker_function(i, f"concurrent_data_{i}")))- threads.append(thread)- thread.start()-- # Wait for all threads to complete- for thread in threads:- thread.join()-- print("All concurrent traces completed")- print(f"Results from concurrent execution: {len(results)} completed")---def concurrent_traces_with_executor():- """Example using ThreadPoolExecutor for concurrent traces."""- print("\nConcurrent Traces with ThreadPoolExecutor")-- agentops.init(- api_key=AGENTOPS_API_KEY,- trace_name="Context Manager Concurrent Executor",- tags=["context-manager", "agentops-example"],- )-- def process_with_trace(task_id: int, data: str) -> str:- """Process data within its own trace context."""- trace_name = f"executor_task_{task_id}"-- with agentops.start_trace(trace_name, tags=["executor", f"task-{task_id}"]):- print(f"Executor task {task_id}: Trace started")-- worker = WorkerAgent(f"ExecutorWorker{task_id}")- result = worker.process_task(data)-- print(f"Executor task {task_id}: Result: {result}")- return result-- # Use ThreadPoolExecutor for concurrent execution- tasks_data = [- (1, "executor_data_1"),- (2, "executor_data_2"),- (3, "executor_data_3"),- (4, "executor_data_4"),- ]-- with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:- # Submit all tasks- future_to_task = {executor.submit(process_with_trace, task_id, data): task_id for task_id, data in tasks_data}-- # Collect results as they complete- results = []- for future in concurrent.futures.as_completed(future_to_task):- task_id = future_to_task[future]- try:- result = future.result()- results.append((task_id, result))- print(f"Task {task_id} completed successfully")- except Exception as e:- print(f"Task {task_id} failed: {e}")-- print("All executor-based traces completed")- print(f"Completed {len(results)} tasks concurrently")---def trace_with_different_tag_types():- """Example showing different ways to tag parallel traces."""- print("\nTraces with Different Tag Types")-- agentops.init(- api_key=AGENTOPS_API_KEY, trace_name="Context Manager Tag Types", tags=["context-manager", "agentops-example"]- )-- # Trace with list tags- with agentops.start_trace("list_tags_trace", tags=["list", "example", "demo"]):- print("Trace with list tags started")- worker1 = WorkerAgent("ListTagWorker")- result1 = worker1.process_task("list_tag_data")- print(f"List tags result: {result1}")-- # Trace with dictionary tags- with agentops.start_trace(- "dict_tags_trace", tags={"environment": "demo", "version": "1.0", "priority": "high", "team": "engineering"}- ):- print("Trace with dictionary tags started")- worker2 = WorkerAgent("DictTagWorker")- result2 = worker2.process_task("dict_tag_data")- print(f"Dictionary tags result: {result2}")-- # Trace with no tags- with agentops.start_trace("no_tags_trace"):- print("Trace with no tags started")- worker3 = WorkerAgent("NoTagWorker")- result3 = worker3.process_task("no_tag_data")- print(f"No tags result: {result3}")-- print("All differently tagged traces completed")---if __name__ == "__main__":- print("AgentOps Parallel Traces Examples")- print("=" * 40)-- # Sequential parallel traces- sequential_parallel_traces()-- # Nested parallel traces- nested_parallel_traces()-- # Concurrent traces with threading- concurrent_traces_with_threads()-- # Concurrent traces with executor- concurrent_traces_with_executor()-- # Different tag types- trace_with_different_tag_types()-- print("\nAll parallel trace examples completed!")diff --git a/examples/context_manager/production_patterns.py b/examples/context_manager/production_patterns.pydeleted file mode 100644index 8cb131f6..00000000--- a/examples/context_manager/production_patterns.py+++ /dev/null@@ -1,366 +0,0 @@-"""-Production Patterns with Context Managers--This example demonstrates real-world production patterns using AgentOps-context managers, including monitoring, logging, and performance tracking.-"""--import os-import time-import logging-from datetime import datetime-from typing import Dict, Any-import agentops-from agentops import agent, task, tool-from dotenv import load_dotenv--load_dotenv()--# Get API key from environment-AGENTOPS_API_KEY = os.environ.get("AGENTOPS_API_KEY")--# Configure logging-logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")-logger = logging.getLogger(__name__)---@agent-class ProductionAgent:- """A production-ready agent with monitoring."""-- def __init__(self, name: str, config: Dict[str, Any]):- self.name = name- self.config = config- self.metrics = {"tasks_completed": 0, "errors_encountered": 0, "total_processing_time": 0.0}- logger.info(f"Initialized production agent: {self.name}")-- @task- def process_request(self, request_data: Dict[str, Any]) -> Dict[str, Any]:- """Process a production request with monitoring."""- start_time = time.time()-- try:- logger.info(f"{self.name} processing request: {request_data.get('id', 'unknown')}")-- # Validate request- validated_data = self.validate_request(request_data)-- # Process data- processed_data = self.transform_data(validated_data)-- # Generate response- response = self.generate_response(processed_data)-- # Update metrics- processing_time = time.time() - start_time- self.metrics["tasks_completed"] += 1- self.metrics["total_processing_time"] += processing_time-- logger.info(f"{self.name} completed request in {processing_time:.3f}s")- return response-- except Exception as e:- self.metrics["errors_encountered"] += 1- logger.error(f"{self.name} failed to process request: {e}")- raise-- @tool- def validate_request(self, data: Dict[str, Any]) -> Dict[str, Any]:- """Validate incoming request data."""- required_fields = ["id", "type", "payload"]-- for field in required_fields:- if field not in data:- raise ValueError(f"Missing required field: {field}")-- logger.debug(f"{self.name} validated request: {data['id']}")- return data-- @tool- def transform_data(self, data: Dict[str, Any]) -> Dict[str, Any]:- """Transform and enrich the data."""- # Simulate data transformation- time.sleep(0.01) # Simulate processing time-- transformed = {- **data,- "processed_at": datetime.now().isoformat(),- "processed_by": self.name,- "version": self.config.get("version", "1.0"),- }-- logger.debug(f"{self.name} transformed data for: {data['id']}")- return transformed-- @tool- def generate_response(self, data: Dict[str, Any]) -> Dict[str, Any]:- """Generate the final response."""- response = {- "status": "success",- "request_id": data["id"],- "result": f"Processed {data['type']} successfully",- "metadata": {- "processed_at": data["processed_at"],- "processed_by": data["processed_by"],- "version": data["version"],- },- }-- logger.debug(f"{self.name} generated response for: {data['id']}")- return response-- def get_metrics(self) -> Dict[str, Any]:- """Get current agent metrics."""- avg_processing_time = self.metrics["total_processing_time"] / max(self.metrics["tasks_completed"], 1)-- return {- **self.metrics,- "average_processing_time": avg_processing_time,- "error_rate": self.metrics["errors_encountered"]- / max(self.metrics["tasks_completed"] + self.metrics["errors_encountered"], 1),- }---def api_endpoint_pattern():- """Example of using context managers in API endpoint pattern."""- print("API Endpoint Pattern")-- agentops.init(- api_key=AGENTOPS_API_KEY,- trace_name="Context Manager API Endpoint Pattern",- tags=["context-manager", "production", "agentops-example"],- )-- # Simulate API requests- requests = [- {"id": "req_001", "type": "user_query", "payload": {"query": "What is AI?"}},- {"id": "req_002", "type": "data_analysis", "payload": {"dataset": "sales_data"}},- {"id": "req_003", "type": "user_query", "payload": {"query": "Process this"}},- ]-- agent_config = {"version": "2.1.0", "environment": "production"}- api_agent = ProductionAgent("APIAgent", agent_config)-- for request in requests:- try:- with agentops.start_trace("api_request", tags=["api", request["type"]]):- response = api_agent.process_request(request)- logger.info(f"API Response: {response['status']} for {response['request_id']}")-- except Exception as e:- logger.error(f"API request failed: {e}")-- # Print final metrics- print(f"Agent Metrics: {api_agent.get_metrics()}")---def batch_processing_pattern():- """Example of batch processing with context managers."""- print("\nBatch Processing Pattern")-- agentops.init(- api_key=AGENTOPS_API_KEY,- trace_name="Context Manager Batch Processing Pattern",- tags=["context-manager", "production", "agentops-example"],- )-- # Simulate batch data- batch_data = [{"id": f"item_{i:03d}", "type": "data_record", "payload": {"value": i * 10}} for i in range(1, 6)]-- agent_config = {"version": "1.5.0", "batch_size": 5}- batch_agent = ProductionAgent("BatchAgent", agent_config)-- with agentops.start_trace("batch_processing", tags=["batch", "bulk"]):- logger.info(f"Starting batch processing of {len(batch_data)} items")-- successful_items = 0- failed_items = 0-- for item in batch_data:- try:- with agentops.start_trace("item_processing", tags=["batch", "item"]):- batch_agent.process_request(item)- successful_items += 1- logger.debug(f"Processed item: {item['id']}")-- except Exception as e:- failed_items += 1- logger.error(f"Failed to process item {item['id']}: {e}")-- logger.info(f"Batch completed: {successful_items} successful, {failed_items} failed")-- print(f"Batch Agent Metrics: {batch_agent.get_metrics()}")---def microservice_pattern():- """Example of microservice communication pattern."""- print("\nMicroservice Communication Pattern")-- agentops.init(- api_key=AGENTOPS_API_KEY,- trace_name="Context Manager Microservice Pattern",- tags=["context-manager", "production", "agentops-example"],- )-- def authenticate_user(user_id: str) -> bool:- """Simulate authentication service."""- with agentops.start_trace("authenticate", tags=["auth", "security"]):- logger.info(f"Authenticating user: {user_id}")- time.sleep(0.01) # Simulate auth check- return user_id != "invalid_user"-- def get_user_profile(user_id: str) -> Dict[str, Any]:- """Simulate user service."""- with agentops.start_trace("get_profile", tags=["user", "profile"]):- logger.info(f"Fetching profile for user: {user_id}")- time.sleep(0.02) # Simulate database query- return {"user_id": user_id, "name": f"User {user_id}", "email": f"{user_id}@example.com"}-- def send_notification(user_id: str, message: str) -> bool:- """Simulate notification service."""- with agentops.start_trace("send_notification", tags=["notification", "email"]):- logger.info(f"Sending notification to user: {user_id}")- time.sleep(0.01) # Simulate email sending- return True-- # Simulate a complete user workflow- user_requests = ["user_123", "user_456", "invalid_user"]-- for user_id in user_requests:- try:- with agentops.start_trace("user_workflow", tags=["workflow", "user"]):- logger.info(f"Processing workflow for user: {user_id}")-- # Step 1: Authenticate- if not authenticate_user(user_id):- raise ValueError(f"Authentication failed for user: {user_id}")-- # Step 2: Get profile- get_user_profile(user_id)-- # Step 3: Send welcome notification- send_notification(user_id, "Welcome to our service!")-- logger.info(f"Workflow completed for user: {user_id}")-- except Exception as e:- logger.error(f"Workflow failed for user {user_id}: {e}")---def monitoring_pattern():- """Example of monitoring with context managers."""- print("\nMonitoring Pattern")-- agentops.init(- api_key=AGENTOPS_API_KEY,- trace_name="Context Manager Monitoring Pattern",- tags=["context-manager", "production", "agentops-example"],- )-- class AlertManager:- """Simple alert manager for demonstration."""-- def __init__(self):- self.alerts = []-- def check_and_alert(self, trace_name: str, duration: float, success: bool):- """Check conditions and generate alerts."""- # Alert on slow operations- if duration > 0.1: # 100ms threshold- self.alerts.append(- {- "type": "SLOW_OPERATION",- "trace": trace_name,- "duration": duration,- "timestamp": datetime.now().isoformat(),- }- )- logger.warning(f"SLOW OPERATION ALERT: {trace_name} took {duration:.3f}s")-- # Alert on failures- if not success:- self.alerts.append(- {"type": "OPERATION_FAILURE", "trace": trace_name, "timestamp": datetime.now().isoformat()}- )- logger.warning(f"FAILURE ALERT: {trace_name} failed")-- alert_manager = AlertManager()-- class MonitoredOperation:- """Context manager with built-in monitoring and alerting."""-- def __init__(self, operation_name: str, tags: list = None):- self.operation_name = operation_name- self.tags = tags or []- self.start_time = None- self.trace_context = None-- def __enter__(self):- self.start_time = time.time()- self.trace_context = agentops.start_trace(self.operation_name, tags=self.tags)- return self.trace_context-- def __exit__(self, exc_type, exc_val, exc_tb):- duration = time.time() - self.start_time- success = exc_type is None-- # Check for alerts- alert_manager.check_and_alert(self.operation_name, duration, success)-- return False-- # Simulate operations with different performance characteristics- operations = [- ("fast_operation", 0.01, False), # Fast, successful- ("slow_operation", 0.15, False), # Slow, successful (will trigger alert)- ("failing_operation", 0.05, True), # Fast, but fails (will trigger alert)- ("normal_operation", 0.03, False), # Normal, successful- ]-- for op_name, sleep_time, should_fail in operations:- try:- with MonitoredOperation(op_name, tags=["monitoring", "demo"]):- logger.info(f"Executing {op_name}")- time.sleep(sleep_time)-- if should_fail:- raise RuntimeError(f"Simulated failure in {op_name}")-- logger.info(f"{op_name} completed successfully")-- except Exception as e:- logger.error(f"{op_name} failed: {e}")-- # Print alerts- print(f"Generated {len(alert_manager.alerts)} alerts:")- for alert in alert_manager.alerts:- print(f" {alert['type']}: {alert.get('trace', 'unknown')} at {alert['timestamp']}")---if __name__ == "__main__":- print("AgentOps Production Patterns Examples")- print("=" * 50)-- # API endpoint pattern- api_endpoint_pattern()-- # Batch processing pattern- batch_processing_pattern()-- # Microservice pattern- microservice_pattern()-- # Monitoring pattern- monitoring_pattern()-- print("\nAll production pattern examples completed!")-- # Let's check programmatically that spans were recorded in AgentOps- print("\n" + "=" * 50)- print("Now let's verify that our LLM calls were tracked properly...")- try:- agentops.validate_trace_spans(trace_context=None)- print("\n✅ Success! All LLM spans were properly recorded in AgentOps.")- except agentops.ValidationError as e:- print(f"\n❌ Error validating spans: {e}")- raisediff --git a/examples/context_manager/requirements.txt b/examples/context_manager/requirements.txtdeleted file mode 100644index 434f10ac..00000000--- a/examples/context_manager/requirements.txt+++ /dev/null@@ -1,2 +0,0 @@-# Context manager examples only need agentops-# python-dotenv and requests are installed as common deps\ No newline at end of filediff --git a/examples/crewai/requirements.txt b/examples/crewai/requirements.txtindex f2e17dad..1e8c11da 100644--- a/examples/crewai/requirements.txt+++ b/examples/crewai/requirements.txt@@ -1,3 +1,5 @@crewaicrewai-tools-pymarkdown\ No newline at end of file+pymarkdown==0.1.4+pymarkdownlnt==0.9.30+pymdown-extensions==10.15\ No newline at end of filediff --git a/examples/google_adk/human_approval.py b/examples/google_adk/human_approval.pyindex 7bf08844..fc556781 100644--- a/examples/google_adk/human_approval.py+++ b/examples/google_adk/human_approval.py@@ -1,12 +1,12 @@-# Google ADK Example: Human Approval Workflow with AgentOps-# This notebook demonstrates a complete human approval workflow using the Google ADK (Agent Development Kit), integrated with AgentOps for observability.+# Google ADK Example: Automated Approval Workflow with AgentOps+# This notebook demonstrates a complete automated approval workflow using the Google ADK (Agent Development Kit), integrated with AgentOps for observability.## **Key Features:**# - **Sequential Agent Processing:** The workflow uses multiple agents chained together to handle different stages of the approval process.-# - **External Tool Integration:** An agent interacts with an external tool that simulates (or in this version, directly prompts for) human approval.+# - **External Tool Integration:** An agent interacts with an external tool that provides automated approval decisions based on amount and reason analysis.# - **Session State Management:** Information is passed between agents and persisted using session state.# - **AgentOps Observability:** All agent actions, tool calls, and LLM interactions are traced and can be viewed in your AgentOps dashboard.-# - **Interactive Human Input:** The approval step now requires direct input from the user.+# - **Automated Decision Making:** The approval system automatically approves or rejects requests based on configurable business rules.# ## 1. Setup and Dependencies# First, let's install the necessary libraries if they are not already present and import them.# %pip install google-adk agentops python-dotenv nest_asyncio asyncio@@ -33,17 +33,17 @@ AGENTOPS_API_KEY = os.getenv("AGENTOPS_API_KEY") or "your_agentops_api_key_here"# Initialize AgentOps - Just 2 lines!agentops.init(AGENTOPS_API_KEY,- trace_name="adk-human-approval-notebook",+ trace_name="adk-automated-approval-notebook",auto_start_session=False,- tags=["google-adk", "human-approval", "agentops-example"],+ tags=["google-adk", "automated-approval", "agentops-example"],)# Define some constants for our application.-APP_NAME = "human_approval_app_notebook"+APP_NAME = "automated_approval_app_notebook"USER_ID = "test_user_notebook_123"-SESSION_ID = "approval_session_notebook_456"+SESSION_ID = "automated_approval_session_notebook_456"MODEL_NAME = "gemini-1.5-flash"-agentops.start_trace(trace_name=APP_NAME, tags=["google_adk", "notebook"])+tracer = agentops.start_trace(trace_name=APP_NAME, tags=["google_adk", "notebook"])# ## 3. Define Schemas@@ -58,23 +58,51 @@ class ApprovalDecision(BaseModel):comments: str = Field(description="Additional comments from the approver")-# ## 4. External Approval Tool (with Human Interaction)-# This tool now directly prompts the user for an approval decision. In a real-world scenario, this might involve sending a notification to an approver and waiting for their response through a UI or API.+# ## 4. External Approval Tool (Automated Decision Making)+# This tool automatically makes approval decisions based on configurable business rules. It analyzes the amount and reason to determine whether to approve or reject the request. In a real-world scenario, this could be integrated with more sophisticated rule engines or ML models for decision making.async def external_approval_tool(amount: float, reason: str) -> str:"""- Prompts for human approval and returns the decision as a JSON string.+ Automated approval system that returns approval decisions based on amount and reason analysis."""- print("🔔 HUMAN APPROVAL REQUIRED:")+ print("🤖 AUTOMATED APPROVAL SYSTEM:")print(f" Amount: ${amount:,.2f}")print(f" Reason: {reason}")++ # Automated decision logicdecision = ""- while decision.lower() not in ["approved", "rejected"]:- decision = input(" Enter decision (approved/rejected): ").strip().lower()- if decision.lower() not in ["approved", "rejected"]:- print(" Invalid input. Please enter 'approved' or 'rejected'.")- comments = input(" Enter comments (optional): ").strip()+ comments = ""++ reason_lower = reason.lower()+ high_priority_keywords = ["critical", "urgent", "emergency", "security", "compliance", "license"]+ business_keywords = ["conference", "training", "team", "software", "equipment", "travel"]++ # Check for high priority keywords first (overrides amount limits)+ if any(keyword in reason_lower for keyword in high_priority_keywords):+ decision = "approved"+ comments = "Auto-approved: High priority business need identified"++ # Auto-approve small amounts+ elif amount <= 1000:+ decision = "approved"+ comments = "Auto-approved: Amount under $1,000 threshold"++ # Auto-reject very large amounts without high priority justification+ elif amount > 10000:+ decision = "rejected"+ comments = "Auto-rejected: Amount exceeds $10,000 limit without high priority justification"++ # Medium amounts: analyze reason keywords+ else:+ if any(keyword in reason_lower for keyword in business_keywords):+ decision = "approved"+ comments = "Auto-approved: Standard business expense"+ else:+ # Default to approval for reasonable amounts with unclear reasons+ decision = "approved"+ comments = "Auto-approved: Amount within reasonable range"+print(f" Decision: {decision.upper()}")- print(f" Comments: {comments if comments else 'N/A'}")+ print(f" Comments: {comments}")return json.dumps({"decision": decision, "comments": comments, "amount": amount, "reason": reason})@@ -102,16 +130,16 @@ prepare_request = LlmAgent(output_key="request_prepared",)-# Agent 2: Request human approval using the tool+# Agent 2: Request automated approval using the toolrequest_approval = LlmAgent(model=MODEL_NAME,- name="RequestHumanApprovalAgent",- description="Calls the external approval system with prepared request details",- instruction="""You are a human approval request agent.+ name="RequestAutomatedApprovalAgent",+ description="Calls the external automated approval system with prepared request details",+ instruction="""You are an automated approval request agent.Your task:1. Get the 'approval_amount' and 'approval_reason' from the session state2. Use the external_approval_tool with these values- 3. Store the approval decision in session state with key 'human_decision'+ 3. Store the approval decision in session state with key 'automated_decision'4. Respond with the approval statusAlways use the exact values from the session state for the tool call.""",@@ -123,10 +151,10 @@ request_approval = LlmAgent(process_decision = LlmAgent(model=MODEL_NAME,name="ProcessDecisionAgent",- description="Processes the human approval decision and provides final response",+ description="Processes the automated approval decision and provides final response",instruction="""You are a decision processing agent.Your task:- 1. Check the 'human_decision' from session state+ 1. Check the 'automated_decision' from session state2. Parse the approval decision JSON3. If approved: congratulate and provide next steps4. If rejected: explain the rejection and suggest alternatives@@ -140,8 +168,8 @@ process_decision = LlmAgent(# ## 6. Create Sequential Workflow# Combine the agents into a sequential workflow. The `SequentialAgent` ensures that the sub-agents are executed in the specified order.approval_workflow = SequentialAgent(- name="HumanApprovalWorkflowNotebook",- description="Complete workflow for processing approval requests with human oversight",+ name="AutomatedApprovalWorkflowNotebook",+ description="Complete workflow for processing approval requests with automated decision making",sub_agents=[prepare_request, request_approval, process_decision],)@@ -196,12 +224,14 @@ async def run_approval_workflow_notebook(user_request: str, session_id: str):# This cell contains the main logic to run the workflow with a few test cases. Each test case will run in its own session.async def main_notebook():test_requests = [- "I need approval for $750 for team lunch and celebrations",- "Please approve $3,000 for a conference ticket and travel expenses",- "I need $12,000 approved for critical software licenses renewal",+ "I need approval for $750 for team lunch and celebrations", # Should auto-approve: under $1,000+ "Please approve $3,000 for a conference ticket and travel expenses", # Should auto-approve: business keywords+ "I need $12,000 approved for critical software licenses renewal", # Should auto-approve: high priority keywords despite high amount+ "Please approve $15,000 for office decoration and furniture", # Should auto-reject: over $10,000 without high priority keywords+ "I need $5,000 for urgent security system upgrade", # Should auto-approve: high priority keywords]for i, request in enumerate(test_requests, 1):- current_session_id = f"approval_session_notebook_{456 + i - 1}"+ current_session_id = f"automated_approval_session_notebook_{456 + i - 1}"# Create the session before running the workflowawait session_service.create_session(app_name=APP_NAME, user_id=USER_ID, session_id=current_session_id)print(f"Created session: {current_session_id}")@@ -219,7 +249,7 @@ except Exception as e:print("\n" + "=" * 50)print("Now let's verify that our LLM calls were tracked properly...")try:- agentops.validate_trace_spans(trace_context=None)+ agentops.validate_trace_spans(trace_context=tracer)print("\n✅ Success! All LLM spans were properly recorded in AgentOps.")except agentops.ValidationError as e:print(f"\n❌ Error validating spans: {e}")diff --git a/examples/google_adk/requirements.txt b/examples/google_adk/requirements.txtindex 476e7f45..a6941f20 100644--- a/examples/google_adk/requirements.txt+++ b/examples/google_adk/requirements.txt@@ -1,5 +1,6 @@-google-adk-google-genai-nest-asyncio+google-adk==1.0.0+google-genai==1.16.1+nest-asyncio==1.6.0agentops-python-dotenv\ No newline at end of file+python-dotenv+deprecated\ No newline at end of filediff --git a/examples/openai/multi_tool_orchestration.py b/examples/openai/multi_tool_orchestration.pyindex f3586a75..c8c8ea37 100644--- a/examples/openai/multi_tool_orchestration.py+++ b/examples/openai/multi_tool_orchestration.py@@ -15,12 +15,11 @@ import timefrom tqdm.auto import tqdmfrom pandas import DataFramefrom datasets import load_dataset-import random-import string++# Note: random and string imports removed as we're using an existing indexfrom openai import OpenAIimport agentopsfrom pinecone import Pinecone-from pinecone import ServerlessSpecload_dotenv()@@ -29,7 +28,7 @@ os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "your_openai_api_key_os.environ["PINECONE_API_KEY"] = os.getenv("PINECONE_API_KEY", "your_pinecone_api_key_here")agentops.init(- auto_start_session=True,+ auto_start_session=False,trace_name="OpenAI Multi-Tool Orchestration",tags=["openai", "multi-tool", "agentops-example"],)@@ -50,11 +49,12 @@ ds_dataframe["merged"] = ds_dataframe.apply()print("Example merged text:", ds_dataframe["merged"].iloc[0])-# ### Create a Pinecone Index Based on the Dataset-# Use the dataset itself to determine the embedding dimensionality. For example, compute one embedding from the merged column and then create the index accordingly.+# ### Connect to Existing Pinecone Index+# We'll connect to an existing Pinecone index instead of creating a new one. We still compute an embedding to ensure compatibility with the existing index dimensions.MODEL = "text-embedding-3-small" # Replace with your production embedding model if needed# Compute an embedding for the first document to obtain the embedding dimension.+# This helps ensure compatibility with the existing index dimensions.sample_embedding_resp = client.embeddings.create(input=[ds_dataframe["merged"].iloc[0]], model=MODEL)embed_dim = len(sample_embedding_resp.data[0].embedding)print(f"Embedding dimension: {embed_dim}")@@ -62,46 +62,47 @@ print(f"Embedding dimension: {embed_dim}")# Initialize Pinecone using your API key.pc = Pinecone(api_key=os.environ["PINECONE_API_KEY"])-# Define the Pinecone serverless specification.-AWS_REGION = "us-east-1"-spec = ServerlessSpec(cloud="aws", region=AWS_REGION)--# Create a random index name with lower case alphanumeric characters and '-'-index_name = "pinecone-index-" + "".join(random.choices(string.ascii_lowercase + string.digits, k=10))--# Create the index if it doesn't already exist.-if index_name not in pc.list_indexes().names():- pc.create_index(index_name, dimension=embed_dim, metric="dotproduct", spec=spec)+# Use the existing Pinecone index+index_name = "pinecone-index-btimakzhfe"-# Connect to the index.+# Connect to the existing index (no need to create it as it already exists)index = pc.Index(index_name)time.sleep(1)-print("Index stats:", index.describe_index_stats())+index_stats = index.describe_index_stats()+print("Index stats:", index_stats)-# #### Upsert the Dataset into Pinecone index+# #### Check if the index already has data, and optionally upsert new data#-# Process the dataset in batches, generate embeddings for each merged text, prepare metadata (including separate Question and Answer fields), and upsert each batch into the index. You may also update metadata for specific entries if needed.-batch_size = 32-for i in tqdm(range(0, len(ds_dataframe["merged"]), batch_size), desc="Upserting to Pinecone"):- i_end = min(i + batch_size, len(ds_dataframe["merged"]))- lines_batch = ds_dataframe["merged"][i:i_end]- ids_batch = [str(n) for n in range(i, i_end)]-- # Create embeddings for the current batch.- res = client.embeddings.create(input=[line for line in lines_batch], model=MODEL)- embeds = [record.embedding for record in res.data]-- # Prepare metadata by extracting original Question and Answer.- meta = []- for record in ds_dataframe.iloc[i:i_end].to_dict("records"):- q_text = record["Question"]- a_text = record["Response"]- # Optionally update metadata for specific entries.- meta.append({"Question": q_text, "Answer": a_text})-- # Upsert the batch into Pinecone.- vectors = list(zip(ids_batch, embeds, meta))- index.upsert(vectors=vectors)+# Check if the existing index already has sufficient data for our demo.+# If it has data, we'll use it as-is. If not, we'll add some sample data.+if index_stats.total_vector_count > 0:+ print(f"✅ Index already contains {index_stats.total_vector_count} vectors. Using existing data.")+else:+ print("📝 Index is empty. Adding sample data from the medical dataset...")+ # Process the dataset in batches, generate embeddings for each merged text, prepare metadata (including separate Question and Answer fields), and upsert each batch into the index. You may also update metadata for specific entries if needed.+ batch_size = 32+ for i in tqdm(range(0, len(ds_dataframe["merged"]), batch_size), desc="Upserting to Pinecone"):+ i_end = min(i + batch_size, len(ds_dataframe["merged"]))+ lines_batch = ds_dataframe["merged"][i:i_end]+ ids_batch = [str(n) for n in range(i, i_end)]++ # Create embeddings for the current batch.+ res = client.embeddings.create(input=[line for line in lines_batch], model=MODEL)+ embeds = [record.embedding for record in res.data]++ # Prepare metadata by extracting original Question and Answer.+ meta = []+ for record in ds_dataframe.iloc[i:i_end].to_dict("records"):+ q_text = record["Question"]+ a_text = record["Response"]+ # Optionally update metadata for specific entries.+ meta.append({"Question": q_text, "Answer": a_text})++ # Upsert the batch into Pinecone.+ vectors = list(zip(ids_batch, embeds, meta))+ index.upsert(vectors=vectors)++ print("✅ Data upserting completed.")# ### Query the Pinecone Indexdiff --git a/examples/openai/requirements.txt b/examples/openai/requirements.txtindex f066e4c3..d6d9bc5f 100644--- a/examples/openai/requirements.txt+++ b/examples/openai/requirements.txt@@ -1,3 +1,5 @@openaipandas # for multi_tool_orchestration.py-tavily-python # for web_search.py\ No newline at end of file+tavily-python # for web_search.py+datasets+pinecone\ No newline at end of filediff --git a/examples/openai/web_search.py b/examples/openai/web_search.pyindex 627d79ce..387b0a99 100644--- a/examples/openai/web_search.py+++ b/examples/openai/web_search.py@@ -27,7 +27,7 @@ os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "your_openai_api_key_os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY", "your_api_key_here")agentops.init(- auto_start_session=True, trace_name="OpenAI Web Search Example", tags=["openai", "web-search", "agentops-example"]+ auto_start_session=False, trace_name="OpenAI Web Search Example", tags=["openai", "web-search", "agentops-example"])tracer = agentops.start_trace(trace_name="OpenAI Responses Example", tags=["openai-responses-example", "openai", "agentops-example"]diff --git a/examples/openai_agents/agent_patterns.py b/examples/openai_agents/agent_patterns.pyindex 3e6af902..94a48dc0 100644--- a/examples/openai_agents/agent_patterns.py+++ b/examples/openai_agents/agent_patterns.py@@ -83,6 +83,10 @@ agentops.init(trace_name="OpenAI Agents Patterns",tags=["openai-agents", "patterns", "agentops-example"],)+tracer = agentops.start_trace(+ trace_name="OpenAI Agents Patterns",+ tags=["openai-agents", "patterns", "agentops-example"],+)# Note: tracer will be defined in each section's cell for clarity, using the specific tags for that pattern.# ## 1. Agents as Tools Pattern#@@ -91,8 +95,7 @@ agentops.init(# For example, you could model the translation task above as tool calls instead: rather than handing over to the language-specific agent, you could call the agent as a tool, and then use the result in the next step. This enables things like translating multiple languages at once.## This pattern demonstrates using agents as callable tools within other agents. The orchestrator agent receives a user message and then picks which specialized agents to call as tools.-# Start the AgentOps trace session-tracer = agentops.start_trace(trace_name="OpenAI Agents as Tools Pattern", tags=["agentops-example", "openai-agents"])+# Agents as Tools Pattern Example# Define specialized translation agentsspanish_agent = Agent(@@ -163,9 +166,6 @@ async def run_agents_as_tools_demo():# Run the demo# await run_agents_as_tools_demo()--# End the AgentOps trace session-agentops.end_trace(tracer, end_state="Success")# ## 2. Deterministic Flow Pattern## A common tactic is to break down a task into a series of smaller steps. Each task can be performed by an agent, and the output of one agent is used as input to the next. For example, if your task was to generate a story, you could break it down into the following steps:@@ -177,10 +177,7 @@ agentops.end_trace(tracer, end_state="Success")# Each of these steps can be performed by an agent. The output of one agent is used as input to the next.## This pattern demonstrates breaking down a complex task into a series of smaller, sequential steps. Each step is performed by an agent, and the output of one agent is used as input to the next.-# Start the AgentOps trace session-tracer = agentops.start_trace(- trace_name="OpenAI Agents Deterministic Flow Pattern", tags=["agentops-example", "openai-agents"]-)+# Deterministic Flow Pattern Example# Define the story generation workflowstory_outline_agent = Agent(@@ -238,9 +235,6 @@ async def run_deterministic_flow_demo():# Run the demo# await run_deterministic_flow_demo()--# End the AgentOps trace session-agentops.end_trace(tracer, end_state="Success")# ## 3. Forcing Tool Use Pattern## This pattern shows how to force an agent to use a tool using `ModelSettings(tool_choice=\"required\")`. This is useful when you want to ensure the agent always uses a specific tool rather than generating a response directly.@@ -251,10 +245,7 @@ agentops.end_trace(tracer, end_state="Success")# 3. `custom`: A custom tool use behavior function is used. The custom function receives all the tool results, and chooses to use the first tool result to generate the final output.## For this demo, we'll allow the user to choose which tool use behavior to test:-# Start the AgentOps trace session-tracer = agentops.start_trace(- trace_name="OpenAI Agents Forcing Tool Use Pattern", tags=["agentops-example", "openai-agents"]-)+# Forcing Tool Use Pattern Example# Define the weather tool and agent@@ -277,25 +268,6 @@ async def custom_tool_use_behavior(return ToolsToFinalOutputResult(is_final_output=True, final_output=f"{weather.city} is {weather.conditions}.")-# User can choose which behavior to test-print("Choose tool use behavior:")-print("1. default - Send tool output to LLM")-print("2. first_tool - Use first tool result as final output")-print("3. custom - Use custom tool behavior function")--choice = input("Enter choice (1, 2, or 3): ").strip()--if choice == "1":- tool_use_behavior = "default"-elif choice == "2":- tool_use_behavior = "first_tool"-elif choice == "3":- tool_use_behavior = "custom"-else:- tool_use_behavior = "default"- print("Invalid choice, using default")--async def run_forcing_tool_use_demo(tool_use_behavior: str):print(f"Testing {tool_use_behavior} behavior:")@@ -318,11 +290,22 @@ async def run_forcing_tool_use_demo(tool_use_behavior: str):print(f"Result: {result.final_output}")-# Run the demo-# await run_forcing_tool_use_demo(tool_use_behavior)+async def run_all_forcing_tool_use_demos():+ """Run all three tool use behavior demos automatically"""+ print("Running all tool use behavior demos:")+ print("1. default - Send tool output to LLM")+ print("2. first_tool - Use first tool result as final output")+ print("3. custom - Use custom tool behavior function")+ print()++ # Test all three behaviors+ for behavior in ["default", "first_tool", "custom"]:+ await run_forcing_tool_use_demo(behavior)+ print("-" * 50)-# End the AgentOps trace session-agentops.end_trace(tracer, end_state="Success")++# Run the demo+# await run_all_forcing_tool_use_demos()# ## 4. Input Guardrails Pattern## Related to parallelization, you often want to run input guardrails to make sure the inputs to your agents are valid. For example, if you have a customer support agent, you might want to make sure that the user isn't trying to ask for help with a math problem.@@ -332,10 +315,7 @@ agentops.end_trace(tracer, end_state="Success")# This is really useful for latency: for example, you might have a very fast model that runs the guardrail and a slow model that runs the actual agent. You wouldn't want to wait for the slow model to finish, so guardrails let you quickly reject invalid inputs.## This pattern demonstrates how to use input guardrails to validate user inputs before they reach the main agent. Guardrails can prevent inappropriate or off-topic requests from being processed.-# Start the AgentOps trace session-tracer = agentops.start_trace(- trace_name="OpenAI Agents Input Guardrails Pattern", tags=["agentops-example", "openai-agents"]-)+# Input Guardrails Pattern Example# Define the guardrail@@ -387,9 +367,6 @@ async def run_input_guardrails_demo():# Run the demo# await run_input_guardrails_demo()--# End the AgentOps trace session-agentops.end_trace(tracer, end_state="Success")# ## 5. LLM as a Judge Pattern## LLMs can often improve the quality of their output if given feedback. A common pattern is to generate a response using a model, and then use a second model to provide feedback. You can even use a small model for the initial generation and a larger model for the feedback, to optimize cost.@@ -397,10 +374,7 @@ agentops.end_trace(tracer, end_state="Success")# For example, you could use an LLM to generate an outline for a story, and then use a second LLM to evaluate the outline and provide feedback. You can then use the feedback to improve the outline, and repeat until the LLM is satisfied with the outline.## This pattern shows how to use one LLM to evaluate and improve the output of another. The first agent generates content, and the second agent judges the quality and provides feedback for improvement.-# Start the AgentOps trace session-tracer = agentops.start_trace(- trace_name="OpenAI Agents LLM as a Judge Pattern", tags=["agentops-example", "openai-agents"]-)+# LLM as a Judge Pattern Example# Define the story generation and evaluation agentsstory_outline_generator = Agent(@@ -469,9 +443,6 @@ async def run_llm_as_judge_demo():# Run the demo# await run_llm_as_judge_demo()--# End the AgentOps trace session-agentops.end_trace(tracer, end_state="Success")# ## 6. Output Guardrails Pattern## Related to parallelization, you often want to run output guardrails to make sure the outputs from your agents are valid. Guardrails can have a \"tripwire\" - if the tripwire is triggered, the agent execution will immediately stop and a `GuardrailTripwireTriggered` exception will be raised.@@ -479,10 +450,7 @@ agentops.end_trace(tracer, end_state="Success")# This is really useful for latency: for example, you might have a very fast model that runs the guardrail and a slow model that runs the actual agent. You wouldn't want to wait for the slow model to finish, so guardrails let you quickly reject invalid outputs.## This pattern demonstrates how to use output guardrails to validate agent outputs after they are generated. This can help prevent sensitive information from being shared or ensure outputs meet quality standards.-# Start the AgentOps trace session-tracer = agentops.start_trace(- trace_name="OpenAI Agents Output Guardrails Pattern", tags=["agentops-example", "openai-agents"]-)+# Output Guardrails Pattern Example# The agent's output type@@ -538,9 +506,6 @@ async def run_output_guardrails_demo():# Run the demo# await run_output_guardrails_demo()--# End the AgentOps trace session-agentops.end_trace(tracer, end_state="Success")# ## 7. Parallelization Pattern## Running multiple agents in parallel is a common pattern. This can be useful for both latency (e.g. if you have multiple steps that don't depend on each other) and also for other reasons e.g. generating multiple responses and picking the best one.@@ -548,10 +513,7 @@ agentops.end_trace(tracer, end_state="Success")# This example runs a translation agent multiple times in parallel, and then picks the best translation.## This pattern shows how to run multiple agents in parallel to improve latency or generate multiple options to choose from. In this example, we run translation agents multiple times and pick the best result.-# Start the AgentOps trace session-tracer = agentops.start_trace(- trace_name="OpenAI Agents Parallelization Pattern", tags=["agentops-example", "openai-agents"]-)+# Parallelization Pattern Example# Define agents for parallelizationspanish_translation_agent = Agent(@@ -597,9 +559,6 @@ async def run_parallelization_demo():# Run the demo# await run_parallelization_demo()--# End the AgentOps trace session-agentops.end_trace(tracer, end_state="Success")# ## 8. Routing Pattern## In many situations, you have specialized sub-agents that handle specific tasks. You can use handoffs to route the task to the right agent.@@ -607,8 +566,7 @@ agentops.end_trace(tracer, end_state="Success")# For example, you might have a frontline agent that receives a request, and then hands off to a specialized agent based on the language of the request.## This pattern demonstrates handoffs and routing between specialized agents. The triage agent receives the first message and hands off to the appropriate agent based on the language of the request.-# Start the AgentOps trace session-tracer = agentops.start_trace(trace_name="OpenAI Agents Routing Pattern", tags=["agentops-example", "openai-agents"])+# Routing Pattern Example# Define language-specific agentsfrench_routing_agent = Agent(@@ -650,9 +608,6 @@ async def run_routing_demo():# Run the demo# await run_routing_demo()--# End the AgentOps trace session-agentops.end_trace(tracer, end_state="Success")# ## 9. Streaming Guardrails Pattern## This example shows how to use guardrails as the model is streaming. Output guardrails run after the final output has been generated; this example runs guardrails every N tokens, allowing for early termination if bad output is detected.@@ -660,10 +615,7 @@ agentops.end_trace(tracer, end_state="Success")# The expected output is that you'll see a bunch of tokens stream in, then the guardrail will trigger and stop the streaming.## This pattern shows how to use guardrails during streaming to provide real-time validation. Unlike output guardrails that run after completion, streaming guardrails can interrupt the generation process early.-# Start the AgentOps trace session-tracer = agentops.start_trace(- trace_name="OpenAI Agents Streaming Guardrails Pattern", tags=["agentops-example", "openai-agents"]-)+# Streaming Guardrails Pattern Example# Define streaming guardrail agentstreaming_agent = Agent(@@ -738,9 +690,9 @@ async def run_streaming_guardrails_demo():if __name__ == "__main__":# Run the streaming guardrails demoasyncio.run(run_streaming_guardrails_demo())+ agentops.end_trace(tracer, end_state="Success")-# End the AgentOps trace session-agentops.end_trace(tracer, end_state="Success")+# Streaming Guardrails Pattern Example Complete# Let's check programmatically that spans were recorded in AgentOpsprint("\n" + "=" * 50)diff --git a/examples/openai_agents/agents_tools.py b/examples/openai_agents/agents_tools.pyindex ed01e752..35f19144 100644--- a/examples/openai_agents/agents_tools.py+++ b/examples/openai_agents/agents_tools.py@@ -32,6 +32,7 @@ import base64import subprocessimport sysimport tempfile+import asynciofrom agents import (Agent,@@ -53,6 +54,10 @@ agentops.init(trace_name="OpenAI Agents Tools Examples",tags=["openai-agents", "tools", "agentops-example"],)+tracer = agentops.start_trace(+ trace_name="OpenAI Agents Tools Examples",+ tags=["openai-agents", "tools", "agentops-example"],+)# ## 1. Code Interpreter Tool#@@ -63,10 +68,7 @@ agentops.init(# - Perform complex mathematical calculations# - Generate plots and visualizations# - Handle data processing tasks-# Start the AgentOps trace session-tracer = agentops.start_trace(- trace_name="OpenAI Agents Code Interpreter Tool Example", tags=["tools-demo", "openai-agents", "agentops-example"]-)+# Code Interpreter Tool Exampleasync def run_code_interpreter_demo():@@ -97,10 +99,7 @@ async def run_code_interpreter_demo():# Run the demo-# await run_code_interpreter_demo()--# End the AgentOps trace session-agentops.end_trace(tracer, end_state="Success")+asyncio.run(run_code_interpreter_demo())# ## 2. File Search Tool#@@ -113,10 +112,7 @@ agentops.end_trace(tracer, end_state="Success")# - Configurable result limits## **Note:** This example requires a pre-configured vector store ID.-# Start the AgentOps trace session-tracer = agentops.start_trace(- trace_name="OpenAI Agents File Search Tool Example", tags=["tools-demo", "openai-agents", "agentops-example"]-)+# File Search Tool Exampleasync def run_file_search_demo():@@ -145,10 +141,7 @@ async def run_file_search_demo():# Run the demo-# await run_file_search_demo()--# End the AgentOps trace session-agentops.end_trace(tracer, end_state="Success")+asyncio.run(run_file_search_demo())# ## 3. Image Generation Tool#@@ -159,10 +152,7 @@ agentops.end_trace(tracer, end_state="Success")# - Configurable quality settings# - Support for various image styles# - Automatic image saving and display-# Start the AgentOps trace session-tracer = agentops.start_trace(- trace_name="OpenAI Agents Image Generation Tool Example", tags=["tools-demo", "openai-agents", "agentops-example"]-)+# Image Generation Tool Exampledef open_file(path: str) -> None:@@ -201,19 +191,18 @@ async def run_image_generation_demo():tmp.write(base64.b64decode(img_result))temp_path = tmp.name- # Open the image+ # Open the image (optional - may not work in headless environments)print(f"Image saved to: {temp_path}")try:open_file(temp_path)+ print("Image opened successfully")except Exception as e:- print(f"Could not open image automatically: {e}")+ print(f"Could not open image automatically (this is normal in headless environments): {e}")+ print("You can manually open the image file if needed")# Run the demo-# await run_image_generation_demo()--# End the AgentOps trace session-agentops.end_trace(tracer, end_state="Success")+asyncio.run(run_image_generation_demo())# ## 4. Web Search Tool#@@ -224,10 +213,7 @@ agentops.end_trace(tracer, end_state="Success")# - Location-aware search results# - Real-time data access# - Configurable search parameters-# Start the AgentOps trace session-tracer = agentops.start_trace(- trace_name="OpenAI Agents Web Search Tool Example", tags=["tools-demo", "openai-agents", "agentops-example"]-)+# Web Search Tool Exampleasync def run_web_search_demo():@@ -247,9 +233,7 @@ async def run_web_search_demo():# Run the demo-# await run_web_search_demo()--# End the AgentOps trace session+asyncio.run(run_web_search_demo())agentops.end_trace(tracer, end_state="Success")# Let's check programmatically that spans were recorded in AgentOpsdiff --git a/examples/openai_agents/customer_service_agent.ipynb b/examples/openai_agents/customer_service_agent.ipynbindex 7cfd793d..c3a4025d 100644--- a/examples/openai_agents/customer_service_agent.ipynb+++ b/examples/openai_agents/customer_service_agent.ipynb@@ -262,7 +262,7 @@"outputs": [],"source": ["await main()\n",- "agentops.end_trace(tracer, status=\"Success\")"+ "agentops.end_trace(tracer, end_state=\"Success\")"]},{diff --git a/examples/openai_agents/customer_service_agent.py b/examples/openai_agents/customer_service_agent.pyindex dffad335..464d1526 100644--- a/examples/openai_agents/customer_service_agent.py+++ b/examples/openai_agents/customer_service_agent.py@@ -28,6 +28,7 @@ import osfrom dotenv import load_dotenvimport randomimport uuid+import asynciofrom pydantic import BaseModelimport agentops@@ -166,8 +167,24 @@ async def main():# Here, we'll just use a random UUID for the conversation IDconversation_id = uuid.uuid4().hex[:16]- while True:- user_input = input("Enter your message: ")+ # Predefined test messages to demonstrate the customer service agent+ test_messages = [+ "Hello, I need help with my flight",+ "I want to change my seat",+ "My confirmation number is ABC123",+ "I'd like seat 12A please",+ "What's the baggage policy?",+ "How many seats are on the plane?",+ "Is there wifi on the flight?",+ "Thank you for your help",+ ]++ print("🤖 Starting Customer Service Agent Demo")+ print("=" * 50)++ for user_input in test_messages:+ print(f"\n👤 User: {user_input}")+with trace("Customer service", group_id=conversation_id):input_items.append({"content": user_input, "role": "user"})result = await Runner.run(current_agent, input_items, context=context)@@ -175,21 +192,24 @@ async def main():for new_item in result.new_items:agent_name = new_item.agent.nameif isinstance(new_item, MessageOutputItem):- print(f"{agent_name}: {ItemHelpers.text_message_output(new_item)}")+ print(f"🤖 {agent_name}: {ItemHelpers.text_message_output(new_item)}")elif isinstance(new_item, HandoffOutputItem):- print(f"Handed off from {new_item.source_agent.name} to {new_item.target_agent.name}")+ print(f"🔄 Handed off from {new_item.source_agent.name} to {new_item.target_agent.name}")elif isinstance(new_item, ToolCallItem):- print(f"{agent_name}: Calling a tool")+ print(f"🔧 {agent_name}: Calling a tool")elif isinstance(new_item, ToolCallOutputItem):- print(f"{agent_name}: Tool call output: {new_item.output}")+ print(f"🔧 {agent_name}: Tool call output: {new_item.output}")else:- print(f"{agent_name}: Skipping item: {new_item.__class__.__name__}")+ print(f"ℹ️ {agent_name}: Skipping item: {new_item.__class__.__name__}")input_items = result.to_input_list()current_agent = result.last_agent+ print("\n" + "=" * 50)+ print("🎉 Customer Service Agent Demo Complete!")+-# await main()-agentops.end_trace(tracer, status="Success")+if __name__ == "__main__":+ asyncio.run(main())# Let's check programmatically that spans were recorded in AgentOpsprint("\n" + "=" * 50)diff --git a/examples/smolagents/multi_smolagents_system.py b/examples/smolagents/multi_smolagents_system.pyindex acbb6965..0ba04999 100644--- a/examples/smolagents/multi_smolagents_system.py+++ b/examples/smolagents/multi_smolagents_system.py@@ -96,6 +96,8 @@ print(visit_webpage("https://en.wikipedia.org/wiki/Hugging_Face")[:500])web_agent = ToolCallingAgent(tools=[DuckDuckGoSearchTool(), visit_webpage],model=model,+ name="web_research",+ description="Runs web searches for you. Give it your query as an argument. Please NOTE that the argument name is `task`.",)manager_agent = CodeAgent(diff --git a/examples/smolagents/requirements.txt b/examples/smolagents/requirements.txtindex 9a3d792c..2a36e82f 100644--- a/examples/smolagents/requirements.txt+++ b/examples/smolagents/requirements.txt@@ -1,4 +1,6 @@smolagents+smolagents[litellm]pandasduckduckgo-search-sqlalchemy\ No newline at end of file+sqlalchemy+markdownify\ No newline at end of file