nat.plugins.strands.strands_callback_handler#
Attributes#
Classes#
Hook callbacks for instrumenting Strands tool invocations. |
|
Helper class that provides a standard way to create an ABC using |
Module Contents#
- logger#
- class StrandsToolInstrumentationHook(handler: StrandsProfilerHandler)#
Hook callbacks for instrumenting Strands tool invocations.
This class provides callbacks for Strands’ hooks API to capture tool execution events and emit proper TOOL_START/END spans.
Initialize the hook with a reference to the profiler handler.
- Args:
handler: StrandsProfilerHandler instance that manages this hook
- handler#
- _step_manager#
- on_before_tool_invocation(event: Any) None#
Handle tool invocation start.
Called by Strands before a tool is executed. Emits a TOOL_START span.
- Args:
event: BeforeToolInvocationEvent from Strands
- class StrandsProfilerHandler#
Bases:
nat.profiler.callbacks.base_callback_class.BaseProfilerCallbackHelper class that provides a standard way to create an ABC using inheritance.
- last_call_ts#
- instrument() None#
Instrument Strands for telemetry capture.
This patches: 1. Model streaming methods (OpenAI/Bedrock) for LLM spans 2. Agent.__init__ to auto-register tool hooks on Agent creation
Tool instrumentation uses Strands’ hooks API, which is automatically registered when an Agent is instantiated.
- _instrument_agent_init() None#
Patch Agent.__init__ to auto-register hooks on instantiation.
This ensures that whenever a Strands Agent is created, our tool instrumentation hooks are automatically registered without requiring any user code changes.
- _extract_model_info(model_instance: Any) tuple[str, dict[str, Any]]#
Extract model name from Strands model instance.
- _wrap_stream_method(
- original: collections.abc.Callable[Ellipsis, Any],
- _extract_text_from_event(ev: dict) str#
Extract text content from a Strands event.
- Args:
ev: Event dictionary from Strands stream
- Returns:
Extracted text content or empty string
- _finalize_tool_call(tool_call: dict[str, Any]) None#
Parse the accumulated input_str JSON and store in the input field.
- Args:
tool_call: Tool call dictionary with input_str to parse