Skip to content

Commit 2529ced

Browse files
karthikscale3obinnaokaforobinnascale3
authored
Release (#520)
* remove logs * remove requirements * Bump version * Squash * minor * switch to http exporter * add instrumentation for neo4j * update version * update trace attributes version * update imports * add neo4j result span attributes * add neo4j result span attributes * bump bersion * fix db attributes error and add examples * handle result transformer * fix genai and pinecone instrumentations (#512) * fix genai and pinecone instrumentations * bump version * fix pinecone Index attribute --------- Co-authored-by: Obinna Okafor <obinna.okafor01@gmail.com> * Improve Agno instrumentation (#513) * cast token count to int * bump version * only redirect stdout if logging is disabled * bump version * fix openai agents attribute error * bump version * fix openai responses api instrumentation (#519) Co-authored-by: Obinna Okafor <obinna.okafor01@gmail.com> * bump version * bump version --------- Co-authored-by: Obinna Okafor <obinna.okafor01@gmail.com> Co-authored-by: obinnascale3 <109410793+obinnascale3@users.noreply.github.com>
1 parent 8ae6146 commit 2529ced

File tree

5 files changed

+72
-65
lines changed

5 files changed

+72
-65
lines changed

src/langtrace_python_sdk/instrumentation/cohere/patch.py

Lines changed: 62 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -87,25 +87,25 @@ def traced_method(wrapped, instance, args, kwargs):
8787
and result.meta.billed_units is not None
8888
):
8989
usage = result.meta.billed_units
90-
if usage is not None:
91-
span.set_attribute(
92-
SpanAttributes.LLM_USAGE_PROMPT_TOKENS,
93-
usage.input_tokens or 0,
94-
)
95-
span.set_attribute(
96-
SpanAttributes.LLM_USAGE_COMPLETION_TOKENS,
97-
usage.output_tokens or 0,
98-
)
99-
100-
span.set_attribute(
101-
SpanAttributes.LLM_USAGE_TOTAL_TOKENS,
102-
(usage.input_tokens or 0) + (usage.output_tokens or 0),
103-
)
104-
105-
span.set_attribute(
106-
"search_units",
107-
usage.search_units or 0,
108-
)
90+
input_tokens = int(usage.input_tokens) if usage.input_tokens else 0
91+
output_tokens = int(usage.output_tokens) if usage.output_tokens else 0
92+
span.set_attribute(
93+
SpanAttributes.LLM_USAGE_PROMPT_TOKENS,
94+
input_tokens,
95+
)
96+
span.set_attribute(
97+
SpanAttributes.LLM_USAGE_COMPLETION_TOKENS,
98+
output_tokens,
99+
)
100+
span.set_attribute(
101+
SpanAttributes.LLM_USAGE_TOTAL_TOKENS,
102+
input_tokens + output_tokens,
103+
)
104+
span.set_attribute(
105+
"search_units",
106+
int(usage.search_units) if usage.search_units else 0,
107+
)
108+
109109

110110
span.set_status(StatusCode.OK)
111111
span.end()
@@ -309,25 +309,25 @@ def traced_method(wrapped, instance, args, kwargs):
309309
and result.meta.billed_units is not None
310310
):
311311
usage = result.meta.billed_units
312-
if usage is not None:
313-
span.set_attribute(
314-
SpanAttributes.LLM_USAGE_PROMPT_TOKENS,
315-
usage.input_tokens or 0,
316-
)
317-
span.set_attribute(
318-
SpanAttributes.LLM_USAGE_COMPLETION_TOKENS,
319-
usage.output_tokens or 0,
320-
)
321-
322-
span.set_attribute(
323-
SpanAttributes.LLM_USAGE_TOTAL_TOKENS,
324-
(usage.input_tokens or 0) + (usage.output_tokens or 0),
325-
)
326-
327-
span.set_attribute(
328-
"search_units",
329-
usage.search_units or 0,
330-
)
312+
input_tokens = int(usage.input_tokens) if usage.input_tokens else 0
313+
output_tokens = int(usage.output_tokens) if usage.output_tokens else 0
314+
span.set_attribute(
315+
SpanAttributes.LLM_USAGE_PROMPT_TOKENS,
316+
input_tokens,
317+
)
318+
span.set_attribute(
319+
SpanAttributes.LLM_USAGE_COMPLETION_TOKENS,
320+
output_tokens,
321+
)
322+
span.set_attribute(
323+
SpanAttributes.LLM_USAGE_TOTAL_TOKENS,
324+
input_tokens + output_tokens,
325+
)
326+
span.set_attribute(
327+
"search_units",
328+
int(usage.search_units) if usage.search_units else 0,
329+
)
330+
331331
span.set_status(StatusCode.OK)
332332
span.end()
333333
return result
@@ -419,10 +419,12 @@ def traced_method(wrapped, instance, args, kwargs):
419419
if (hasattr(result.usage, "billed_units") and
420420
result.usage.billed_units is not None):
421421
usage = result.usage.billed_units
422+
input_tokens = int(usage.input_tokens) if usage.input_tokens else 0
423+
output_tokens = int(usage.output_tokens) if usage.output_tokens else 0
422424
for metric, value in {
423-
"input": usage.input_tokens or 0,
424-
"output": usage.output_tokens or 0,
425-
"total": (usage.input_tokens or 0) + (usage.output_tokens or 0),
425+
"input": input_tokens,
426+
"output": output_tokens,
427+
"total": input_tokens + output_tokens,
426428
}.items():
427429
span.set_attribute(
428430
f"gen_ai.usage.{metric}_tokens",
@@ -571,26 +573,27 @@ def traced_method(wrapped, instance, args, kwargs):
571573
and response.meta.billed_units is not None
572574
):
573575
usage = response.meta.billed_units
574-
if usage is not None:
575-
span.set_attribute(
576-
SpanAttributes.LLM_USAGE_PROMPT_TOKENS,
577-
usage.input_tokens or 0,
578-
)
579-
span.set_attribute(
580-
SpanAttributes.LLM_USAGE_COMPLETION_TOKENS,
581-
usage.output_tokens or 0,
582-
)
583-
576+
input_tokens = int(usage.input_tokens) if usage.input_tokens else 0
577+
output_tokens = int(usage.output_tokens) if usage.output_tokens else 0
578+
span.set_attribute(
579+
SpanAttributes.LLM_USAGE_PROMPT_TOKENS,
580+
input_tokens,
581+
)
582+
span.set_attribute(
583+
SpanAttributes.LLM_USAGE_COMPLETION_TOKENS,
584+
output_tokens,
585+
)
586+
span.set_attribute(
587+
SpanAttributes.LLM_USAGE_TOTAL_TOKENS,
588+
input_tokens + output_tokens,
589+
)
590+
591+
if usage.search_units is not None:
584592
span.set_attribute(
585-
SpanAttributes.LLM_USAGE_TOTAL_TOKENS,
586-
(usage.input_tokens or 0)
587-
+ (usage.output_tokens or 0),
593+
"search_units",
594+
int(usage.search_units) if usage.search_units else 0,
588595
)
589-
if usage.search_units is not None:
590-
span.set_attribute(
591-
"search_units",
592-
usage.search_units or 0,
593-
)
596+
594597

595598
yield event
596599
finally:

src/langtrace_python_sdk/instrumentation/openai/patch.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ def traced_method(
114114
return StreamWrapper(response, span)
115115
else:
116116
_set_openai_agentic_response_attributes(span, response)
117+
118+
span.set_status(StatusCode.OK)
119+
span.end()
117120
return response
118121
except Exception as err:
119122
span.record_exception(err)
@@ -738,7 +741,7 @@ def _set_openai_agentic_response_attributes(span: Span, response) -> None:
738741
"input_tokens": response.usage.input_tokens,
739742
"output_tokens": response.usage.output_tokens,
740743
"total_tokens": response.usage.total_tokens,
741-
"cached_tokens": response.usage.input_tokens_details["cached_tokens"],
744+
"cached_tokens": response.usage.input_tokens_details.cached_tokens,
742745
},
743746
)
744747

src/langtrace_python_sdk/langtrace.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ def init(
299299
init_instrumentations(config.disable_instrumentations, all_instrumentations)
300300
add_span_processor(provider, config, exporter)
301301

302-
sys.stdout = sys.__stdout__
302+
if config.disable_logging:
303+
sys.stdout = sys.__stdout__
303304
init_sentry(config, host)
304305

305306

src/langtrace_python_sdk/utils/llm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,19 +347,19 @@ def set_usage_attributes(span, usage):
347347
set_span_attribute(
348348
span,
349349
SpanAttributes.LLM_USAGE_PROMPT_TOKENS,
350-
input_tokens,
350+
int(input_tokens),
351351
)
352352

353353
set_span_attribute(
354354
span,
355355
SpanAttributes.LLM_USAGE_COMPLETION_TOKENS,
356-
output_tokens,
356+
int(output_tokens),
357357
)
358358

359359
set_span_attribute(
360360
span,
361361
SpanAttributes.LLM_USAGE_TOTAL_TOKENS,
362-
input_tokens + output_tokens,
362+
int(input_tokens) + int(output_tokens),
363363
)
364364

365365
if "search_units" in usage:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "3.8.10"
1+
__version__ = "3.8.11"

0 commit comments

Comments
 (0)