Skip to content

Commit bbec741

Browse files
committed
refactor: enhance error handling and logging for agent and model wrapping
1 parent 369df8b commit bbec741

1 file changed

Lines changed: 78 additions & 28 deletions

File tree

agentops/instrumentation/agentic/smolagents/instrumentor.py

Lines changed: 78 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,49 @@ 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
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-
)
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}")
94112

95113
# Model operations with proper model name extraction
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))
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}")
98129

99130
logger.info("SmoLAgents instrumentation enabled")
100131

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

292+
# Try to import modules before attempting unwrap
261293
try:
262-
unwrap("smolagents.agents", "CodeAgent.run")
263-
except Exception as e:
264-
logger.debug(f"Failed to unwrap CodeAgent.run: {e}")
294+
import smolagents.agents
265295

266-
try:
267-
unwrap("smolagents.agents", "ToolCallingAgent.run")
268-
except Exception as e:
269-
logger.debug(f"Failed to unwrap ToolCallingAgent.run: {e}")
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}")
270302

271-
try:
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}")
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}")
275309

276-
try:
277-
unwrap("smolagents.models", "LiteLLMModel.generate")
278-
except Exception as e:
279-
logger.debug(f"Failed to unwrap LiteLLMModel.generate: {e}")
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}")
280317

281318
try:
282-
unwrap("smolagents.models", "LiteLLMModel.generate_stream")
283-
except Exception as e:
284-
logger.debug(f"Failed to unwrap LiteLLMModel.generate_stream: {e}")
319+
import smolagents.models
320+
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}")
327+
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}")
285335

286336
logger.info("SmoLAgents instrumentation disabled")

0 commit comments

Comments
 (0)