Skip to content

Commit ed287f8

Browse files
authored
fix(langchain): report token counts when trace content is enabled (#2899)
1 parent 116d86b commit ed287f8

File tree

1 file changed

+50
-52
lines changed
  • packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain

1 file changed

+50
-52
lines changed

packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,6 @@ def _set_chat_request(
163163

164164

165165
def _set_chat_response(span: Span, response: LLMResult) -> None:
166-
if not should_send_prompts():
167-
return
168-
169166
input_tokens = 0
170167
output_tokens = 0
171168
total_tokens = 0
@@ -195,67 +192,68 @@ def _set_chat_response(span: Span, response: LLMResult) -> None:
195192
input_token_details = generation.message.usage_metadata.get("input_token_details", {})
196193
cache_read_tokens += input_token_details.get("cache_read", 0)
197194

198-
prefix = f"{SpanAttributes.LLM_COMPLETIONS}.{i}"
199-
if hasattr(generation, "text") and generation.text != "":
200-
span.set_attribute(
201-
f"{prefix}.content",
202-
generation.text,
203-
)
204-
span.set_attribute(f"{prefix}.role", "assistant")
205-
else:
206-
span.set_attribute(
207-
f"{prefix}.role",
208-
_message_type_to_role(generation.type),
209-
)
210-
if generation.message.content is str:
195+
if should_send_prompts():
196+
prefix = f"{SpanAttributes.LLM_COMPLETIONS}.{i}"
197+
if hasattr(generation, "text") and generation.text != "":
211198
span.set_attribute(
212199
f"{prefix}.content",
213-
generation.message.content,
200+
generation.text,
214201
)
202+
span.set_attribute(f"{prefix}.role", "assistant")
215203
else:
216204
span.set_attribute(
217-
f"{prefix}.content",
218-
json.dumps(
219-
generation.message.content, cls=CallbackFilteredJSONEncoder
220-
),
221-
)
222-
if generation.generation_info.get("finish_reason"):
223-
span.set_attribute(
224-
f"{prefix}.finish_reason",
225-
generation.generation_info.get("finish_reason"),
226-
)
227-
228-
if generation.message.additional_kwargs.get("function_call"):
229-
span.set_attribute(
230-
f"{prefix}.tool_calls.0.name",
231-
generation.message.additional_kwargs.get("function_call").get(
232-
"name"
233-
),
205+
f"{prefix}.role",
206+
_message_type_to_role(generation.type),
234207
)
235-
span.set_attribute(
236-
f"{prefix}.tool_calls.0.arguments",
237-
generation.message.additional_kwargs.get("function_call").get(
238-
"arguments"
239-
),
240-
)
241-
242-
if generation.message.additional_kwargs.get("tool_calls"):
243-
for idx, tool_call in enumerate(
244-
generation.message.additional_kwargs.get("tool_calls")
245-
):
246-
tool_call_prefix = f"{prefix}.tool_calls.{idx}"
247-
208+
if generation.message.content is str:
209+
span.set_attribute(
210+
f"{prefix}.content",
211+
generation.message.content,
212+
)
213+
else:
248214
span.set_attribute(
249-
f"{tool_call_prefix}.id", tool_call.get("id")
215+
f"{prefix}.content",
216+
json.dumps(
217+
generation.message.content, cls=CallbackFilteredJSONEncoder
218+
),
250219
)
220+
if generation.generation_info.get("finish_reason"):
251221
span.set_attribute(
252-
f"{tool_call_prefix}.name",
253-
tool_call.get("function").get("name"),
222+
f"{prefix}.finish_reason",
223+
generation.generation_info.get("finish_reason"),
254224
)
225+
226+
if generation.message.additional_kwargs.get("function_call"):
255227
span.set_attribute(
256-
f"{tool_call_prefix}.arguments",
257-
tool_call.get("function").get("arguments"),
228+
f"{prefix}.tool_calls.0.name",
229+
generation.message.additional_kwargs.get("function_call").get(
230+
"name"
231+
),
258232
)
233+
span.set_attribute(
234+
f"{prefix}.tool_calls.0.arguments",
235+
generation.message.additional_kwargs.get("function_call").get(
236+
"arguments"
237+
),
238+
)
239+
240+
if generation.message.additional_kwargs.get("tool_calls"):
241+
for idx, tool_call in enumerate(
242+
generation.message.additional_kwargs.get("tool_calls")
243+
):
244+
tool_call_prefix = f"{prefix}.tool_calls.{idx}"
245+
246+
span.set_attribute(
247+
f"{tool_call_prefix}.id", tool_call.get("id")
248+
)
249+
span.set_attribute(
250+
f"{tool_call_prefix}.name",
251+
tool_call.get("function").get("name"),
252+
)
253+
span.set_attribute(
254+
f"{tool_call_prefix}.arguments",
255+
tool_call.get("function").get("arguments"),
256+
)
259257
i += 1
260258

261259
if input_tokens > 0 or output_tokens > 0 or total_tokens > 0 or cache_read_tokens > 0:

0 commit comments

Comments
 (0)