agentrun-sdk 0.1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of agentrun-sdk might be problematic. Click here for more details.

Files changed (115) hide show
  1. agentrun_operation_sdk/cli/__init__.py +1 -0
  2. agentrun_operation_sdk/cli/cli.py +19 -0
  3. agentrun_operation_sdk/cli/common.py +21 -0
  4. agentrun_operation_sdk/cli/runtime/__init__.py +1 -0
  5. agentrun_operation_sdk/cli/runtime/commands.py +203 -0
  6. agentrun_operation_sdk/client/client.py +75 -0
  7. agentrun_operation_sdk/operations/runtime/__init__.py +8 -0
  8. agentrun_operation_sdk/operations/runtime/configure.py +101 -0
  9. agentrun_operation_sdk/operations/runtime/launch.py +82 -0
  10. agentrun_operation_sdk/operations/runtime/models.py +31 -0
  11. agentrun_operation_sdk/services/runtime.py +152 -0
  12. agentrun_operation_sdk/utils/logging_config.py +72 -0
  13. agentrun_operation_sdk/utils/runtime/config.py +94 -0
  14. agentrun_operation_sdk/utils/runtime/container.py +280 -0
  15. agentrun_operation_sdk/utils/runtime/entrypoint.py +203 -0
  16. agentrun_operation_sdk/utils/runtime/schema.py +56 -0
  17. agentrun_sdk/__init__.py +7 -0
  18. agentrun_sdk/agent/__init__.py +25 -0
  19. agentrun_sdk/agent/agent.py +696 -0
  20. agentrun_sdk/agent/agent_result.py +46 -0
  21. agentrun_sdk/agent/conversation_manager/__init__.py +26 -0
  22. agentrun_sdk/agent/conversation_manager/conversation_manager.py +88 -0
  23. agentrun_sdk/agent/conversation_manager/null_conversation_manager.py +46 -0
  24. agentrun_sdk/agent/conversation_manager/sliding_window_conversation_manager.py +179 -0
  25. agentrun_sdk/agent/conversation_manager/summarizing_conversation_manager.py +252 -0
  26. agentrun_sdk/agent/state.py +97 -0
  27. agentrun_sdk/event_loop/__init__.py +9 -0
  28. agentrun_sdk/event_loop/event_loop.py +499 -0
  29. agentrun_sdk/event_loop/streaming.py +319 -0
  30. agentrun_sdk/experimental/__init__.py +4 -0
  31. agentrun_sdk/experimental/hooks/__init__.py +15 -0
  32. agentrun_sdk/experimental/hooks/events.py +123 -0
  33. agentrun_sdk/handlers/__init__.py +10 -0
  34. agentrun_sdk/handlers/callback_handler.py +70 -0
  35. agentrun_sdk/hooks/__init__.py +49 -0
  36. agentrun_sdk/hooks/events.py +80 -0
  37. agentrun_sdk/hooks/registry.py +247 -0
  38. agentrun_sdk/models/__init__.py +10 -0
  39. agentrun_sdk/models/anthropic.py +432 -0
  40. agentrun_sdk/models/bedrock.py +649 -0
  41. agentrun_sdk/models/litellm.py +225 -0
  42. agentrun_sdk/models/llamaapi.py +438 -0
  43. agentrun_sdk/models/mistral.py +539 -0
  44. agentrun_sdk/models/model.py +95 -0
  45. agentrun_sdk/models/ollama.py +357 -0
  46. agentrun_sdk/models/openai.py +436 -0
  47. agentrun_sdk/models/sagemaker.py +598 -0
  48. agentrun_sdk/models/writer.py +449 -0
  49. agentrun_sdk/multiagent/__init__.py +22 -0
  50. agentrun_sdk/multiagent/a2a/__init__.py +15 -0
  51. agentrun_sdk/multiagent/a2a/executor.py +148 -0
  52. agentrun_sdk/multiagent/a2a/server.py +252 -0
  53. agentrun_sdk/multiagent/base.py +92 -0
  54. agentrun_sdk/multiagent/graph.py +555 -0
  55. agentrun_sdk/multiagent/swarm.py +656 -0
  56. agentrun_sdk/py.typed +1 -0
  57. agentrun_sdk/session/__init__.py +18 -0
  58. agentrun_sdk/session/file_session_manager.py +216 -0
  59. agentrun_sdk/session/repository_session_manager.py +152 -0
  60. agentrun_sdk/session/s3_session_manager.py +272 -0
  61. agentrun_sdk/session/session_manager.py +73 -0
  62. agentrun_sdk/session/session_repository.py +51 -0
  63. agentrun_sdk/telemetry/__init__.py +21 -0
  64. agentrun_sdk/telemetry/config.py +194 -0
  65. agentrun_sdk/telemetry/metrics.py +476 -0
  66. agentrun_sdk/telemetry/metrics_constants.py +15 -0
  67. agentrun_sdk/telemetry/tracer.py +563 -0
  68. agentrun_sdk/tools/__init__.py +17 -0
  69. agentrun_sdk/tools/decorator.py +569 -0
  70. agentrun_sdk/tools/executor.py +137 -0
  71. agentrun_sdk/tools/loader.py +152 -0
  72. agentrun_sdk/tools/mcp/__init__.py +13 -0
  73. agentrun_sdk/tools/mcp/mcp_agent_tool.py +99 -0
  74. agentrun_sdk/tools/mcp/mcp_client.py +423 -0
  75. agentrun_sdk/tools/mcp/mcp_instrumentation.py +322 -0
  76. agentrun_sdk/tools/mcp/mcp_types.py +63 -0
  77. agentrun_sdk/tools/registry.py +607 -0
  78. agentrun_sdk/tools/structured_output.py +421 -0
  79. agentrun_sdk/tools/tools.py +217 -0
  80. agentrun_sdk/tools/watcher.py +136 -0
  81. agentrun_sdk/types/__init__.py +5 -0
  82. agentrun_sdk/types/collections.py +23 -0
  83. agentrun_sdk/types/content.py +188 -0
  84. agentrun_sdk/types/event_loop.py +48 -0
  85. agentrun_sdk/types/exceptions.py +81 -0
  86. agentrun_sdk/types/guardrails.py +254 -0
  87. agentrun_sdk/types/media.py +89 -0
  88. agentrun_sdk/types/session.py +152 -0
  89. agentrun_sdk/types/streaming.py +201 -0
  90. agentrun_sdk/types/tools.py +258 -0
  91. agentrun_sdk/types/traces.py +5 -0
  92. agentrun_sdk-0.1.2.dist-info/METADATA +51 -0
  93. agentrun_sdk-0.1.2.dist-info/RECORD +115 -0
  94. agentrun_sdk-0.1.2.dist-info/WHEEL +5 -0
  95. agentrun_sdk-0.1.2.dist-info/entry_points.txt +2 -0
  96. agentrun_sdk-0.1.2.dist-info/top_level.txt +3 -0
  97. agentrun_wrapper/__init__.py +11 -0
  98. agentrun_wrapper/_utils/__init__.py +6 -0
  99. agentrun_wrapper/_utils/endpoints.py +16 -0
  100. agentrun_wrapper/identity/__init__.py +5 -0
  101. agentrun_wrapper/identity/auth.py +211 -0
  102. agentrun_wrapper/memory/__init__.py +6 -0
  103. agentrun_wrapper/memory/client.py +1697 -0
  104. agentrun_wrapper/memory/constants.py +103 -0
  105. agentrun_wrapper/memory/controlplane.py +626 -0
  106. agentrun_wrapper/py.typed +1 -0
  107. agentrun_wrapper/runtime/__init__.py +13 -0
  108. agentrun_wrapper/runtime/app.py +473 -0
  109. agentrun_wrapper/runtime/context.py +34 -0
  110. agentrun_wrapper/runtime/models.py +25 -0
  111. agentrun_wrapper/services/__init__.py +1 -0
  112. agentrun_wrapper/services/identity.py +192 -0
  113. agentrun_wrapper/tools/__init__.py +6 -0
  114. agentrun_wrapper/tools/browser_client.py +325 -0
  115. agentrun_wrapper/tools/code_interpreter_client.py +186 -0
@@ -0,0 +1,563 @@
1
+ """OpenTelemetry integration.
2
+
3
+ This module provides tracing capabilities using OpenTelemetry,
4
+ enabling trace data to be sent to OTLP endpoints.
5
+ """
6
+
7
+ import json
8
+ import logging
9
+ from datetime import date, datetime, timezone
10
+ from typing import Any, Dict, Mapping, Optional
11
+
12
+ import opentelemetry.trace as trace_api
13
+ from opentelemetry.instrumentation.threading import ThreadingInstrumentor
14
+ from opentelemetry.trace import Span, StatusCode
15
+
16
+ from ..agent.agent_result import AgentResult
17
+ from ..types.content import ContentBlock, Message, Messages
18
+ from ..types.streaming import StopReason, Usage
19
+ from ..types.tools import ToolResult, ToolUse
20
+ from ..types.traces import AttributeValue
21
+
22
+ logger = logging.getLogger(__name__)
23
+
24
+
25
+ class JSONEncoder(json.JSONEncoder):
26
+ """Custom JSON encoder that handles non-serializable types."""
27
+
28
+ def encode(self, obj: Any) -> str:
29
+ """Recursively encode objects, preserving structure and only replacing unserializable values.
30
+
31
+ Args:
32
+ obj: The object to encode
33
+
34
+ Returns:
35
+ JSON string representation of the object
36
+ """
37
+ # Process the object to handle non-serializable values
38
+ processed_obj = self._process_value(obj)
39
+ # Use the parent class to encode the processed object
40
+ return super().encode(processed_obj)
41
+
42
+ def _process_value(self, value: Any) -> Any:
43
+ """Process any value, handling containers recursively.
44
+
45
+ Args:
46
+ value: The value to process
47
+
48
+ Returns:
49
+ Processed value with unserializable parts replaced
50
+ """
51
+ # Handle datetime objects directly
52
+ if isinstance(value, (datetime, date)):
53
+ return value.isoformat()
54
+
55
+ # Handle dictionaries
56
+ elif isinstance(value, dict):
57
+ return {k: self._process_value(v) for k, v in value.items()}
58
+
59
+ # Handle lists
60
+ elif isinstance(value, list):
61
+ return [self._process_value(item) for item in value]
62
+
63
+ # Handle all other values
64
+ else:
65
+ try:
66
+ # Test if the value is JSON serializable
67
+ json.dumps(value)
68
+ return value
69
+ except (TypeError, OverflowError, ValueError):
70
+ return "<replaced>"
71
+
72
+
73
+ class Tracer:
74
+ """Handles OpenTelemetry tracing.
75
+
76
+ This class provides a simple interface for creating and managing traces,
77
+ with support for sending to OTLP endpoints.
78
+
79
+ When the OTEL_EXPORTER_OTLP_ENDPOINT environment variable is set, traces
80
+ are sent to the OTLP endpoint.
81
+ """
82
+
83
+ def __init__(
84
+ self,
85
+ ) -> None:
86
+ """Initialize the tracer."""
87
+ self.service_name = __name__
88
+ self.tracer_provider: Optional[trace_api.TracerProvider] = None
89
+ self.tracer_provider = trace_api.get_tracer_provider()
90
+ self.tracer = self.tracer_provider.get_tracer(self.service_name)
91
+ ThreadingInstrumentor().instrument()
92
+
93
+ def _start_span(
94
+ self,
95
+ span_name: str,
96
+ parent_span: Optional[Span] = None,
97
+ attributes: Optional[Dict[str, AttributeValue]] = None,
98
+ span_kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL,
99
+ ) -> Span:
100
+ """Generic helper method to start a span with common attributes.
101
+
102
+ Args:
103
+ span_name: Name of the span to create
104
+ parent_span: Optional parent span to link this span to
105
+ attributes: Dictionary of attributes to set on the span
106
+ span_kind: enum of OptenTelemetry SpanKind
107
+
108
+ Returns:
109
+ The created span, or None if tracing is not enabled
110
+ """
111
+ if not parent_span:
112
+ parent_span = trace_api.get_current_span()
113
+
114
+ context = None
115
+ if parent_span and parent_span.is_recording() and parent_span != trace_api.INVALID_SPAN:
116
+ context = trace_api.set_span_in_context(parent_span)
117
+
118
+ span = self.tracer.start_span(name=span_name, context=context, kind=span_kind)
119
+
120
+ # Set start time as a common attribute
121
+ span.set_attribute("gen_ai.event.start_time", datetime.now(timezone.utc).isoformat())
122
+
123
+ # Add all provided attributes
124
+ if attributes:
125
+ self._set_attributes(span, attributes)
126
+
127
+ return span
128
+
129
+ def _set_attributes(self, span: Span, attributes: Dict[str, AttributeValue]) -> None:
130
+ """Set attributes on a span, handling different value types appropriately.
131
+
132
+ Args:
133
+ span: The span to set attributes on
134
+ attributes: Dictionary of attributes to set
135
+ """
136
+ if not span:
137
+ return
138
+
139
+ for key, value in attributes.items():
140
+ span.set_attribute(key, value)
141
+
142
+ def _end_span(
143
+ self,
144
+ span: Span,
145
+ attributes: Optional[Dict[str, AttributeValue]] = None,
146
+ error: Optional[Exception] = None,
147
+ ) -> None:
148
+ """Generic helper method to end a span.
149
+
150
+ Args:
151
+ span: The span to end
152
+ attributes: Optional attributes to set before ending the span
153
+ error: Optional exception if an error occurred
154
+ """
155
+ if not span:
156
+ return
157
+
158
+ try:
159
+ # Set end time as a common attribute
160
+ span.set_attribute("gen_ai.event.end_time", datetime.now(timezone.utc).isoformat())
161
+
162
+ # Add any additional attributes
163
+ if attributes:
164
+ self._set_attributes(span, attributes)
165
+
166
+ # Handle error if present
167
+ if error:
168
+ span.set_status(StatusCode.ERROR, str(error))
169
+ span.record_exception(error)
170
+ else:
171
+ span.set_status(StatusCode.OK)
172
+ except Exception as e:
173
+ logger.warning("error=<%s> | error while ending span", e, exc_info=True)
174
+ finally:
175
+ span.end()
176
+ # Force flush to ensure spans are exported
177
+ if self.tracer_provider and hasattr(self.tracer_provider, "force_flush"):
178
+ try:
179
+ self.tracer_provider.force_flush()
180
+ except Exception as e:
181
+ logger.warning("error=<%s> | failed to force flush tracer provider", e)
182
+
183
+ def end_span_with_error(self, span: Span, error_message: str, exception: Optional[Exception] = None) -> None:
184
+ """End a span with error status.
185
+
186
+ Args:
187
+ span: The span to end.
188
+ error_message: Error message to set in the span status.
189
+ exception: Optional exception to record in the span.
190
+ """
191
+ if not span:
192
+ return
193
+
194
+ error = exception or Exception(error_message)
195
+ self._end_span(span, error=error)
196
+
197
+ def _add_event(self, span: Optional[Span], event_name: str, event_attributes: Dict[str, AttributeValue]) -> None:
198
+ """Add an event with attributes to a span.
199
+
200
+ Args:
201
+ span: The span to add the event to
202
+ event_name: Name of the event
203
+ event_attributes: Dictionary of attributes to set on the event
204
+ """
205
+ if not span:
206
+ return
207
+
208
+ span.add_event(event_name, attributes=event_attributes)
209
+
210
+ def start_model_invoke_span(
211
+ self,
212
+ messages: Messages,
213
+ parent_span: Optional[Span] = None,
214
+ model_id: Optional[str] = None,
215
+ **kwargs: Any,
216
+ ) -> Span:
217
+ """Start a new span for a model invocation.
218
+
219
+ Args:
220
+ messages: Messages being sent to the model.
221
+ parent_span: Optional parent span to link this span to.
222
+ model_id: Optional identifier for the model being invoked.
223
+ **kwargs: Additional attributes to add to the span.
224
+
225
+ Returns:
226
+ The created span, or None if tracing is not enabled.
227
+ """
228
+ attributes: Dict[str, AttributeValue] = {
229
+ "gen_ai.system": "strands-agents",
230
+ "gen_ai.operation.name": "chat",
231
+ }
232
+
233
+ if model_id:
234
+ attributes["gen_ai.request.model"] = model_id
235
+
236
+ # Add additional kwargs as attributes
237
+ attributes.update({k: v for k, v in kwargs.items() if isinstance(v, (str, int, float, bool))})
238
+
239
+ span = self._start_span("chat", parent_span, attributes=attributes, span_kind=trace_api.SpanKind.CLIENT)
240
+ for message in messages:
241
+ self._add_event(
242
+ span,
243
+ f"gen_ai.{message['role']}.message",
244
+ {"content": serialize(message["content"])},
245
+ )
246
+ return span
247
+
248
+ def end_model_invoke_span(
249
+ self, span: Span, message: Message, usage: Usage, stop_reason: StopReason, error: Optional[Exception] = None
250
+ ) -> None:
251
+ """End a model invocation span with results and metrics.
252
+
253
+ Args:
254
+ span: The span to end.
255
+ message: The message response from the model.
256
+ usage: Token usage information from the model call.
257
+ stop_reason (StopReason): The reason the model stopped generating.
258
+ error: Optional exception if the model call failed.
259
+ """
260
+ attributes: Dict[str, AttributeValue] = {
261
+ "gen_ai.usage.prompt_tokens": usage["inputTokens"],
262
+ "gen_ai.usage.input_tokens": usage["inputTokens"],
263
+ "gen_ai.usage.completion_tokens": usage["outputTokens"],
264
+ "gen_ai.usage.output_tokens": usage["outputTokens"],
265
+ "gen_ai.usage.total_tokens": usage["totalTokens"],
266
+ }
267
+
268
+ self._add_event(
269
+ span,
270
+ "gen_ai.choice",
271
+ event_attributes={"finish_reason": str(stop_reason), "message": serialize(message["content"])},
272
+ )
273
+
274
+ self._end_span(span, attributes, error)
275
+
276
+ def start_tool_call_span(self, tool: ToolUse, parent_span: Optional[Span] = None, **kwargs: Any) -> Span:
277
+ """Start a new span for a tool call.
278
+
279
+ Args:
280
+ tool: The tool being used.
281
+ parent_span: Optional parent span to link this span to.
282
+ **kwargs: Additional attributes to add to the span.
283
+
284
+ Returns:
285
+ The created span, or None if tracing is not enabled.
286
+ """
287
+ attributes: Dict[str, AttributeValue] = {
288
+ "gen_ai.operation.name": "execute_tool",
289
+ "gen_ai.system": "strands-agents",
290
+ "gen_ai.tool.name": tool["name"],
291
+ "gen_ai.tool.call.id": tool["toolUseId"],
292
+ }
293
+
294
+ # Add additional kwargs as attributes
295
+ attributes.update(kwargs)
296
+
297
+ span_name = f"execute_tool {tool['name']}"
298
+ span = self._start_span(span_name, parent_span, attributes=attributes, span_kind=trace_api.SpanKind.INTERNAL)
299
+
300
+ self._add_event(
301
+ span,
302
+ "gen_ai.tool.message",
303
+ event_attributes={
304
+ "role": "tool",
305
+ "content": serialize(tool["input"]),
306
+ "id": tool["toolUseId"],
307
+ },
308
+ )
309
+
310
+ return span
311
+
312
+ def end_tool_call_span(
313
+ self, span: Span, tool_result: Optional[ToolResult], error: Optional[Exception] = None
314
+ ) -> None:
315
+ """End a tool call span with results.
316
+
317
+ Args:
318
+ span: The span to end.
319
+ tool_result: The result from the tool execution.
320
+ error: Optional exception if the tool call failed.
321
+ """
322
+ attributes: Dict[str, AttributeValue] = {}
323
+ if tool_result is not None:
324
+ status = tool_result.get("status")
325
+ status_str = str(status) if status is not None else ""
326
+
327
+ attributes.update(
328
+ {
329
+ "tool.status": status_str,
330
+ }
331
+ )
332
+
333
+ self._add_event(
334
+ span,
335
+ "gen_ai.choice",
336
+ event_attributes={
337
+ "message": serialize(tool_result.get("content")),
338
+ "id": tool_result.get("toolUseId", ""),
339
+ },
340
+ )
341
+
342
+ self._end_span(span, attributes, error)
343
+
344
+ def start_event_loop_cycle_span(
345
+ self,
346
+ invocation_state: Any,
347
+ messages: Messages,
348
+ parent_span: Optional[Span] = None,
349
+ **kwargs: Any,
350
+ ) -> Optional[Span]:
351
+ """Start a new span for an event loop cycle.
352
+
353
+ Args:
354
+ invocation_state: Arguments for the event loop cycle.
355
+ parent_span: Optional parent span to link this span to.
356
+ messages: Messages being processed in this cycle.
357
+ **kwargs: Additional attributes to add to the span.
358
+
359
+ Returns:
360
+ The created span, or None if tracing is not enabled.
361
+ """
362
+ event_loop_cycle_id = str(invocation_state.get("event_loop_cycle_id"))
363
+ parent_span = parent_span if parent_span else invocation_state.get("event_loop_parent_span")
364
+
365
+ attributes: Dict[str, AttributeValue] = {
366
+ "event_loop.cycle_id": event_loop_cycle_id,
367
+ }
368
+
369
+ if "event_loop_parent_cycle_id" in invocation_state:
370
+ attributes["event_loop.parent_cycle_id"] = str(invocation_state["event_loop_parent_cycle_id"])
371
+
372
+ # Add additional kwargs as attributes
373
+ attributes.update({k: v for k, v in kwargs.items() if isinstance(v, (str, int, float, bool))})
374
+
375
+ span_name = "execute_event_loop_cycle"
376
+ span = self._start_span(span_name, parent_span, attributes)
377
+ for message in messages or []:
378
+ self._add_event(
379
+ span,
380
+ f"gen_ai.{message['role']}.message",
381
+ {"content": serialize(message["content"])},
382
+ )
383
+
384
+ return span
385
+
386
+ def end_event_loop_cycle_span(
387
+ self,
388
+ span: Span,
389
+ message: Message,
390
+ tool_result_message: Optional[Message] = None,
391
+ error: Optional[Exception] = None,
392
+ ) -> None:
393
+ """End an event loop cycle span with results.
394
+
395
+ Args:
396
+ span: The span to end.
397
+ message: The message response from this cycle.
398
+ tool_result_message: Optional tool result message if a tool was called.
399
+ error: Optional exception if the cycle failed.
400
+ """
401
+ attributes: Dict[str, AttributeValue] = {}
402
+ event_attributes: Dict[str, AttributeValue] = {"message": serialize(message["content"])}
403
+
404
+ if tool_result_message:
405
+ event_attributes["tool.result"] = serialize(tool_result_message["content"])
406
+ self._add_event(span, "gen_ai.choice", event_attributes=event_attributes)
407
+ self._end_span(span, attributes, error)
408
+
409
+ def start_agent_span(
410
+ self,
411
+ message: Message,
412
+ agent_name: str,
413
+ model_id: Optional[str] = None,
414
+ tools: Optional[list] = None,
415
+ custom_trace_attributes: Optional[Mapping[str, AttributeValue]] = None,
416
+ **kwargs: Any,
417
+ ) -> Span:
418
+ """Start a new span for an agent invocation.
419
+
420
+ Args:
421
+ message: The user message being sent to the agent.
422
+ agent_name: Name of the agent.
423
+ model_id: Optional model identifier.
424
+ tools: Optional list of tools being used.
425
+ custom_trace_attributes: Optional mapping of custom trace attributes to include in the span.
426
+ **kwargs: Additional attributes to add to the span.
427
+
428
+ Returns:
429
+ The created span, or None if tracing is not enabled.
430
+ """
431
+ attributes: Dict[str, AttributeValue] = {
432
+ "gen_ai.system": "strands-agents",
433
+ "gen_ai.agent.name": agent_name,
434
+ "gen_ai.operation.name": "invoke_agent",
435
+ }
436
+
437
+ if model_id:
438
+ attributes["gen_ai.request.model"] = model_id
439
+
440
+ if tools:
441
+ tools_json = serialize(tools)
442
+ attributes["gen_ai.agent.tools"] = tools_json
443
+
444
+ # Add custom trace attributes if provided
445
+ if custom_trace_attributes:
446
+ attributes.update(custom_trace_attributes)
447
+
448
+ # Add additional kwargs as attributes
449
+ attributes.update({k: v for k, v in kwargs.items() if isinstance(v, (str, int, float, bool))})
450
+
451
+ span = self._start_span(
452
+ f"invoke_agent {agent_name}", attributes=attributes, span_kind=trace_api.SpanKind.CLIENT
453
+ )
454
+ self._add_event(
455
+ span,
456
+ "gen_ai.user.message",
457
+ event_attributes={
458
+ "content": serialize(message["content"]),
459
+ },
460
+ )
461
+
462
+ return span
463
+
464
+ def end_agent_span(
465
+ self,
466
+ span: Span,
467
+ response: Optional[AgentResult] = None,
468
+ error: Optional[Exception] = None,
469
+ ) -> None:
470
+ """End an agent span with results and metrics.
471
+
472
+ Args:
473
+ span: The span to end.
474
+ response: The response from the agent.
475
+ error: Any error that occurred.
476
+ """
477
+ attributes: Dict[str, AttributeValue] = {}
478
+
479
+ if response:
480
+ self._add_event(
481
+ span,
482
+ "gen_ai.choice",
483
+ event_attributes={"message": str(response), "finish_reason": str(response.stop_reason)},
484
+ )
485
+
486
+ if hasattr(response, "metrics") and hasattr(response.metrics, "accumulated_usage"):
487
+ accumulated_usage = response.metrics.accumulated_usage
488
+ attributes.update(
489
+ {
490
+ "gen_ai.usage.prompt_tokens": accumulated_usage["inputTokens"],
491
+ "gen_ai.usage.completion_tokens": accumulated_usage["outputTokens"],
492
+ "gen_ai.usage.input_tokens": accumulated_usage["inputTokens"],
493
+ "gen_ai.usage.output_tokens": accumulated_usage["outputTokens"],
494
+ "gen_ai.usage.total_tokens": accumulated_usage["totalTokens"],
495
+ }
496
+ )
497
+
498
+ self._end_span(span, attributes, error)
499
+
500
+ def start_multiagent_span(
501
+ self,
502
+ task: str | list[ContentBlock],
503
+ instance: str,
504
+ ) -> Span:
505
+ """Start a new span for swarm invocation."""
506
+ attributes: Dict[str, AttributeValue] = {
507
+ "gen_ai.system": "strands-agents",
508
+ "gen_ai.agent.name": instance,
509
+ "gen_ai.operation.name": f"invoke_{instance}",
510
+ }
511
+
512
+ span = self._start_span(f"invoke_{instance}", attributes=attributes, span_kind=trace_api.SpanKind.CLIENT)
513
+ content = serialize(task) if isinstance(task, list) else task
514
+ self._add_event(
515
+ span,
516
+ "gen_ai.user.message",
517
+ event_attributes={"content": content},
518
+ )
519
+
520
+ return span
521
+
522
+ def end_swarm_span(
523
+ self,
524
+ span: Span,
525
+ result: Optional[str] = None,
526
+ ) -> None:
527
+ """End a swarm span with results."""
528
+ if result:
529
+ self._add_event(
530
+ span,
531
+ "gen_ai.choice",
532
+ event_attributes={"message": result},
533
+ )
534
+
535
+
536
+ # Singleton instance for global access
537
+ _tracer_instance = None
538
+
539
+
540
+ def get_tracer() -> Tracer:
541
+ """Get or create the global tracer.
542
+
543
+ Returns:
544
+ The global tracer instance.
545
+ """
546
+ global _tracer_instance
547
+
548
+ if not _tracer_instance:
549
+ _tracer_instance = Tracer()
550
+
551
+ return _tracer_instance
552
+
553
+
554
+ def serialize(obj: Any) -> str:
555
+ """Serialize an object to JSON with consistent settings.
556
+
557
+ Args:
558
+ obj: The object to serialize
559
+
560
+ Returns:
561
+ JSON string representation of the object
562
+ """
563
+ return json.dumps(obj, ensure_ascii=False, cls=JSONEncoder)
@@ -0,0 +1,17 @@
1
+ """Agent tool interfaces and utilities.
2
+
3
+ This module provides the core functionality for creating, managing, and executing tools through agents.
4
+ """
5
+
6
+ from .decorator import tool
7
+ from .structured_output import convert_pydantic_to_tool_spec
8
+ from .tools import InvalidToolUseNameException, PythonAgentTool, normalize_schema, normalize_tool_spec
9
+
10
+ __all__ = [
11
+ "tool",
12
+ "PythonAgentTool",
13
+ "InvalidToolUseNameException",
14
+ "normalize_schema",
15
+ "normalize_tool_spec",
16
+ "convert_pydantic_to_tool_spec",
17
+ ]