@@ -72,11 +72,11 @@ def _get_genai_attributes(
7272 request_model : Optional [str ],
7373 response_model : Optional [str ],
7474 operation_name : Optional [str ],
75- system : Optional [str ],
75+ provider : Optional [str ],
7676) -> Dict [str , AttributeValue ]:
7777 attributes : Dict [str , AttributeValue ] = {}
78- if system :
79- attributes [GenAI .GEN_AI_PROVIDER_NAME ] = system
78+ if provider :
79+ attributes [GenAI .GEN_AI_PROVIDER_NAME ] = provider
8080 if operation_name :
8181 attributes [GenAI .GEN_AI_OPERATION_NAME ] = operation_name
8282 if request_model :
@@ -90,16 +90,16 @@ def _get_genai_attributes(
9090def _set_initial_span_attributes (
9191 span : Span ,
9292 request_model : Optional [str ],
93- system : Optional [str ],
93+ provider : Optional [str ],
9494) -> None :
9595 span .set_attribute (
9696 GenAI .GEN_AI_OPERATION_NAME , GenAI .GenAiOperationNameValues .CHAT .value
9797 )
9898 if request_model :
9999 span .set_attribute (GenAI .GEN_AI_REQUEST_MODEL , request_model )
100- if system is not None :
101- # TODO: clean system name to match GenAiProviderNameValues?
102- span .set_attribute (GenAI .GEN_AI_PROVIDER_NAME , system )
100+ if provider is not None :
101+ # TODO: clean provider name to match GenAiProviderNameValues?
102+ span .set_attribute (GenAI .GEN_AI_PROVIDER_NAME , provider )
103103
104104
105105def _set_response_and_usage_attributes (
@@ -126,31 +126,24 @@ def _collect_finish_reasons(generations: List[OutputMessage]) -> List[str]:
126126 return finish_reasons
127127
128128
129- def _maybe_set_span_input_messages (
130- span : Span , messages : List [InputMessage ]
129+ def _maybe_set_span_messages (
130+ span : Span ,
131+ input_messages : List [InputMessage ],
132+ output_messages : List [OutputMessage ],
131133) -> None :
132134 if not is_experimental_mode () or get_content_capturing_mode () not in (
133135 ContentCapturingMode .SPAN_ONLY ,
134136 ContentCapturingMode .SPAN_AND_EVENT ,
135137 ):
136138 return
137139 message_parts : List [Dict [str , Any ]] = [
138- asdict (message ) for message in messages
140+ asdict (message ) for message in input_messages
139141 ]
140142 if message_parts :
141143 span .set_attribute ("gen_ai.input.messages" , json .dumps (message_parts ))
142144
143-
144- def _maybe_set_span_output_messages (
145- span : Span , generations : List [OutputMessage ]
146- ) -> None :
147- if not is_experimental_mode () or get_content_capturing_mode () not in (
148- ContentCapturingMode .SPAN_ONLY ,
149- ContentCapturingMode .SPAN_AND_EVENT ,
150- ):
151- return
152145 generation_parts : List [Dict [str , Any ]] = [
153- asdict (generation ) for generation in generations
146+ asdict (generation ) for generation in output_messages
154147 ]
155148 if generation_parts :
156149 span .set_attribute (
@@ -184,7 +177,7 @@ def __init__(
184177 ):
185178 self ._tracer : Tracer = tracer or trace .get_tracer (__name__ )
186179
187- # Map from run_id -> _SpanState, to keep track of spans and parent/child relationships
180+ # TODO: Map from run_id -> _SpanState, to keep track of spans and parent/child relationships
188181 self .spans : Dict [UUID , _SpanState ] = {}
189182
190183 def _start_span (
@@ -200,6 +193,7 @@ def _start_span(
200193 else :
201194 # top-level or missing parent
202195 span = self ._tracer .start_span (name = name , kind = kind )
196+ set_span_in_context (span )
203197
204198 return span
205199
@@ -210,6 +204,7 @@ def _end_span(self, run_id: UUID):
210204 if child_state :
211205 child_state .span .end ()
212206 state .span .end ()
207+ del self .spans [run_id ]
213208
214209 def start (self , invocation : LLMInvocation ):
215210 if (
@@ -245,12 +240,12 @@ def _apply_common_span_attributes(
245240 ) -> Tuple [Dict [str , AttributeValue ]]:
246241 """Apply attributes shared by finish() and error() and compute metrics.
247242
248- Returns (genai_attributes).
243+ Returns (genai_attributes) for use with metrics .
249244 """
250245 request_model = invocation .attributes .get ("request_model" )
251- system = invocation .attributes .get ("system " )
246+ provider = invocation .attributes .get ("provider " )
252247
253- _set_initial_span_attributes (span , request_model , system )
248+ _set_initial_span_attributes (span , request_model , provider )
254249
255250 finish_reasons = _collect_finish_reasons (invocation .chat_generations )
256251 if finish_reasons :
@@ -273,7 +268,7 @@ def _apply_common_span_attributes(
273268 request_model ,
274269 response_model ,
275270 GenAI .GenAiOperationNameValues .CHAT .value ,
276- system ,
271+ provider ,
277272 )
278273 return (genai_attributes ,)
279274
@@ -286,8 +281,9 @@ def finish(self, invocation: LLMInvocation):
286281 _ = self ._apply_common_span_attributes (
287282 span , invocation
288283 ) # return value to be used with metrics
289- _maybe_set_span_input_messages (span , invocation .messages )
290- _maybe_set_span_output_messages (span , invocation .chat_generations )
284+ _maybe_set_span_messages (
285+ span , invocation .messages , invocation .chat_generations
286+ )
291287 self ._finalize_invocation (invocation )
292288
293289 def error (self , error : Error , invocation : LLMInvocation ):
0 commit comments