diff --git a/src/langtrace_python_sdk/instrumentation/langchain_core/patch.py b/src/langtrace_python_sdk/instrumentation/langchain_core/patch.py index 71441666..765c738e 100644 --- a/src/langtrace_python_sdk/instrumentation/langchain_core/patch.py +++ b/src/langtrace_python_sdk/instrumentation/langchain_core/patch.py @@ -87,13 +87,11 @@ def traced_method(wrapped, instance, args, kwargs): completion_tokens = result.usage.completion_tokens span.set_attribute(SpanAttributes.LLM_USAGE_PROMPT_TOKENS, prompt_tokens) span.set_attribute(SpanAttributes.LLM_USAGE_COMPLETION_TOKENS, completion_tokens) - - elif result.generations[0][0].text: + elif hasattr(result, 'generations') and len(result.generations) > 0 and len(result.generations[0]) > 0 and hasattr(result.generations[0][0], 'text') and isinstance(result.generations[0][0].text, str): span.set_attribute(SpanAttributes.LLM_USAGE_COMPLETION_TOKENS, instance.get_num_tokens(result.generations[0][0].text)) - elif isinstance(args[0][0], str): + elif len(args) > 0 and len(args[0]) > 0 and not hasattr(args[0][0], 'text') and hasattr(instance, 'get_num_tokens'): span.set_attribute(SpanAttributes.LLM_USAGE_PROMPT_TOKENS, instance.get_num_tokens(args[0][0])) - - else: + elif len(args) > 0 and len(args[0]) > 0 and hasattr(args[0][0], 'text') and isinstance(args[0][0].text, str) and hasattr(instance, 'get_num_tokens'): span.set_attribute(SpanAttributes.LLM_USAGE_PROMPT_TOKENS, instance.get_num_tokens(args[0][0].text)) span.set_status(StatusCode.OK) diff --git a/src/langtrace_python_sdk/version.py b/src/langtrace_python_sdk/version.py index a0b06b86..e121c1f3 100644 --- a/src/langtrace_python_sdk/version.py +++ b/src/langtrace_python_sdk/version.py @@ -1 +1 @@ -__version__ = "2.3.7" +__version__ = "2.3.8"