Skip to content

Commit c4005ec

Browse files
committed
refactor: streamline agent and model wrapping with improved error handling
1 parent bbec741 commit c4005ec

1 file changed

Lines changed: 28 additions & 78 deletions

File tree

agentops/instrumentation/agentic/smolagents/instrumentor.py

Lines changed: 28 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -83,49 +83,18 @@ def _custom_wrap(self, **kwargs):
8383
This is called after normal wrapping, but we use it for all wrapping
8484
since we don't have normal wrapped methods.
8585
"""
86-
# Core agent operations - wrap with error handling
87-
try:
88-
# Import the module first to ensure it's loaded
89-
import smolagents.agents
90-
91-
# Check if classes exist before wrapping
92-
if hasattr(smolagents.agents, "CodeAgent"):
93-
wrap_function_wrapper("smolagents.agents", "CodeAgent.run", self._agent_run_wrapper(self._tracer))
94-
logger.debug("Successfully wrapped CodeAgent.run")
95-
else:
96-
logger.debug("CodeAgent not found in smolagents.agents")
97-
98-
if hasattr(smolagents.agents, "ToolCallingAgent"):
99-
wrap_function_wrapper(
100-
"smolagents.agents", "ToolCallingAgent.run", self._agent_run_wrapper(self._tracer)
101-
)
102-
wrap_function_wrapper(
103-
"smolagents.agents",
104-
"ToolCallingAgent.execute_tool_call",
105-
self._tool_execution_wrapper(self._tracer),
106-
)
107-
logger.debug("Successfully wrapped ToolCallingAgent methods")
108-
else:
109-
logger.debug("ToolCallingAgent not found in smolagents.agents")
110-
except (ImportError, AttributeError) as e:
111-
logger.debug(f"Failed to wrap agent operations: {e}")
86+
# Core agent operations
87+
wrap_function_wrapper("smolagents.agents", "CodeAgent.run", self._agent_run_wrapper(self._tracer))
88+
wrap_function_wrapper("smolagents.agents", "ToolCallingAgent.run", self._agent_run_wrapper(self._tracer))
89+
90+
# Tool calling operations
91+
wrap_function_wrapper(
92+
"smolagents.agents", "ToolCallingAgent.execute_tool_call", self._tool_execution_wrapper(self._tracer)
93+
)
11294

11395
# Model operations with proper model name extraction
114-
try:
115-
# Import the module first to ensure it's loaded
116-
import smolagents.models
117-
118-
# Check if class exists before wrapping
119-
if hasattr(smolagents.models, "LiteLLMModel"):
120-
wrap_function_wrapper("smolagents.models", "LiteLLMModel.generate", self._llm_wrapper(self._tracer))
121-
wrap_function_wrapper(
122-
"smolagents.models", "LiteLLMModel.generate_stream", self._llm_wrapper(self._tracer)
123-
)
124-
logger.debug("Successfully wrapped LiteLLMModel methods")
125-
else:
126-
logger.debug("LiteLLMModel not found in smolagents.models")
127-
except (ImportError, AttributeError) as e:
128-
logger.debug(f"Failed to wrap model operations: {e}")
96+
wrap_function_wrapper("smolagents.models", "LiteLLMModel.generate", self._llm_wrapper(self._tracer))
97+
wrap_function_wrapper("smolagents.models", "LiteLLMModel.generate_stream", self._llm_wrapper(self._tracer))
12998

13099
logger.info("SmoLAgents instrumentation enabled")
131100

@@ -289,48 +258,29 @@ def _custom_unwrap(self, **kwargs):
289258
# Unwrap all instrumented methods
290259
from opentelemetry.instrumentation.utils import unwrap
291260

292-
# Try to import modules before attempting unwrap
293261
try:
294-
import smolagents.agents
295-
296-
if hasattr(smolagents.agents, "CodeAgent"):
297-
try:
298-
unwrap("smolagents.agents", "CodeAgent.run")
299-
logger.debug("Successfully unwrapped CodeAgent.run")
300-
except Exception as e:
301-
logger.debug(f"Failed to unwrap CodeAgent.run: {e}")
302-
303-
if hasattr(smolagents.agents, "ToolCallingAgent"):
304-
try:
305-
unwrap("smolagents.agents", "ToolCallingAgent.run")
306-
logger.debug("Successfully unwrapped ToolCallingAgent.run")
307-
except Exception as e:
308-
logger.debug(f"Failed to unwrap ToolCallingAgent.run: {e}")
262+
unwrap("smolagents.agents", "CodeAgent.run")
263+
except Exception as e:
264+
logger.debug(f"Failed to unwrap CodeAgent.run: {e}")
309265

310-
try:
311-
unwrap("smolagents.agents", "ToolCallingAgent.execute_tool_call")
312-
logger.debug("Successfully unwrapped ToolCallingAgent.execute_tool_call")
313-
except Exception as e:
314-
logger.debug(f"Failed to unwrap ToolCallingAgent.execute_tool_call: {e}")
315-
except ImportError as e:
316-
logger.debug(f"smolagents.agents module not available for unwrapping: {e}")
266+
try:
267+
unwrap("smolagents.agents", "ToolCallingAgent.run")
268+
except Exception as e:
269+
logger.debug(f"Failed to unwrap ToolCallingAgent.run: {e}")
317270

318271
try:
319-
import smolagents.models
272+
unwrap("smolagents.agents", "ToolCallingAgent.execute_tool_call")
273+
except Exception as e:
274+
logger.debug(f"Failed to unwrap ToolCallingAgent.execute_tool_call: {e}")
320275

321-
if hasattr(smolagents.models, "LiteLLMModel"):
322-
try:
323-
unwrap("smolagents.models", "LiteLLMModel.generate")
324-
logger.debug("Successfully unwrapped LiteLLMModel.generate")
325-
except Exception as e:
326-
logger.debug(f"Failed to unwrap LiteLLMModel.generate: {e}")
276+
try:
277+
unwrap("smolagents.models", "LiteLLMModel.generate")
278+
except Exception as e:
279+
logger.debug(f"Failed to unwrap LiteLLMModel.generate: {e}")
327280

328-
try:
329-
unwrap("smolagents.models", "LiteLLMModel.generate_stream")
330-
logger.debug("Successfully unwrapped LiteLLMModel.generate_stream")
331-
except Exception as e:
332-
logger.debug(f"Failed to unwrap LiteLLMModel.generate_stream: {e}")
333-
except ImportError as e:
334-
logger.debug(f"smolagents.models module not available for unwrapping: {e}")
281+
try:
282+
unwrap("smolagents.models", "LiteLLMModel.generate_stream")
283+
except Exception as e:
284+
logger.debug(f"Failed to unwrap LiteLLMModel.generate_stream: {e}")
335285

336286
logger.info("SmoLAgents instrumentation disabled")

0 commit comments

Comments
 (0)