@@ -79,11 +79,16 @@ class Tracer:
7979
8080 When the OTEL_EXPORTER_OTLP_ENDPOINT environment variable is set, traces
8181 are sent to the OTLP endpoint.
82+
83+ Attributes:
84+ use_latest_genai_conventions: If True, uses the latest experimental GenAI semantic conventions.
85+ include_tool_definitions: If True, includes detailed tool definitions in the agent trace span.
86+
87+ Both attributes are controlled by including "gen_ai_latest_experimental" or "gen_ai_tool_definitions",
88+ respectively, in the OTEL_SEMCONV_STABILITY_OPT_IN environment variable.
8289 """
8390
84- def __init__ (
85- self ,
86- ) -> None :
91+ def __init__ (self ) -> None :
8792 """Initialize the tracer."""
8893 self .service_name = __name__
8994 self .tracer_provider : Optional [trace_api .TracerProvider ] = None
@@ -92,17 +97,18 @@ def __init__(
9297 ThreadingInstrumentor ().instrument ()
9398
9499 # Read OTEL_SEMCONV_STABILITY_OPT_IN environment variable
95- self .use_latest_genai_conventions = self ._parse_semconv_opt_in ()
100+ opt_in_values = self ._parse_semconv_opt_in ()
101+ self .use_latest_genai_conventions = "gen_ai_latest_experimental" in opt_in_values
102+ self .include_tool_definitions = "gen_ai_tool_definitions" in opt_in_values
96103
97- def _parse_semconv_opt_in (self ) -> bool :
104+ def _parse_semconv_opt_in (self ) -> set [ str ] :
98105 """Parse the OTEL_SEMCONV_STABILITY_OPT_IN environment variable.
99106
100107 Returns:
101- Set of opt-in values from the environment variable
108+ A set of opt-in values from the environment variable.
102109 """
103110 opt_in_env = os .getenv ("OTEL_SEMCONV_STABILITY_OPT_IN" , "" )
104-
105- return "gen_ai_latest_experimental" in opt_in_env
111+ return {value .strip () for value in opt_in_env .split ("," )}
106112
107113 def _start_span (
108114 self ,
@@ -551,6 +557,7 @@ def start_agent_span(
551557 model_id : Optional [str ] = None ,
552558 tools : Optional [list ] = None ,
553559 custom_trace_attributes : Optional [Mapping [str , AttributeValue ]] = None ,
560+ tools_config : Optional [dict ] = None ,
554561 ** kwargs : Any ,
555562 ) -> Span :
556563 """Start a new span for an agent invocation.
@@ -561,6 +568,7 @@ def start_agent_span(
561568 model_id: Optional model identifier.
562569 tools: Optional list of tools being used.
563570 custom_trace_attributes: Optional mapping of custom trace attributes to include in the span.
571+ tools_config: Optional dictionary of tool configurations.
564572 **kwargs: Additional attributes to add to the span.
565573
566574 Returns:
@@ -577,8 +585,15 @@ def start_agent_span(
577585 attributes ["gen_ai.request.model" ] = model_id
578586
579587 if tools :
580- tools_json = serialize (tools )
581- attributes ["gen_ai.agent.tools" ] = tools_json
588+ attributes ["gen_ai.agent.tools" ] = serialize (tools )
589+
590+ if self .include_tool_definitions and tools_config :
591+ try :
592+ tool_definitions = self ._construct_tool_definitions (tools_config )
593+ attributes ["gen_ai.tool.definitions" ] = serialize (tool_definitions )
594+ except Exception :
595+ # A failure in telemetry should not crash the agent
596+ logger .warning ("failed to attach tool metadata to agent span" , exc_info = True )
582597
583598 # Add custom trace attributes if provided
584599 if custom_trace_attributes :
@@ -649,6 +664,18 @@ def end_agent_span(
649664
650665 self ._end_span (span , attributes , error )
651666
667+ def _construct_tool_definitions (self , tools_config : dict ) -> list [dict [str , Any ]]:
668+ """Constructs a list of tool definitions from the provided tools_config."""
669+ return [
670+ {
671+ "name" : name ,
672+ "description" : spec .get ("description" ),
673+ "inputSchema" : spec .get ("inputSchema" ),
674+ "outputSchema" : spec .get ("outputSchema" ),
675+ }
676+ for name , spec in tools_config .items ()
677+ ]
678+
652679 def start_multiagent_span (
653680 self ,
654681 task : str | list [ContentBlock ],
0 commit comments