diff --git a/src/langtrace_python_sdk/instrumentation/openai/patch.py b/src/langtrace_python_sdk/instrumentation/openai/patch.py index c3bf718a..a9e261ec 100644 --- a/src/langtrace_python_sdk/instrumentation/openai/patch.py +++ b/src/langtrace_python_sdk/instrumentation/openai/patch.py @@ -28,7 +28,11 @@ SERVICE_PROVIDERS, ) from langtrace_python_sdk.constants.instrumentation.openai import APIS -from langtrace_python_sdk.utils.llm import calculate_prompt_tokens, estimate_tokens +from langtrace_python_sdk.utils.llm import ( + calculate_prompt_tokens, + estimate_tokens, + get_tool_calls, +) from openai._types import NOT_GIVEN @@ -430,9 +434,10 @@ def traced_method(wrapped, instance, args, kwargs): # handle tool calls in the kwargs llm_prompts = [] for item in kwargs.get("messages", []): - if hasattr(item, "tool_calls") and item.tool_calls is not None: + tools = get_tool_calls(item) + if tools is not None: tool_calls = [] - for tool_call in item.tool_calls: + for tool_call in tools: tool_call_dict = { "id": tool_call.id if hasattr(tool_call, "id") else "", "type": tool_call.type if hasattr(tool_call, "type") else "", @@ -611,9 +616,10 @@ async def traced_method(wrapped, instance, args, kwargs): # handle tool calls in the kwargs llm_prompts = [] for item in kwargs.get("messages", []): - if hasattr(item, "tool_calls") and item.tool_calls is not None: + tools = get_tool_calls(item) + if tools is not None: tool_calls = [] - for tool_call in item.tool_calls: + for tool_call in tools: tool_call_dict = { "id": tool_call.id if hasattr(tool_call, "id") else "", "type": tool_call.type if hasattr(tool_call, "type") else "", diff --git a/src/langtrace_python_sdk/utils/llm.py b/src/langtrace_python_sdk/utils/llm.py index 17f1a43f..56d6c460 100644 --- a/src/langtrace_python_sdk/utils/llm.py +++ b/src/langtrace_python_sdk/utils/llm.py @@ -65,3 +65,15 @@ def set_span_attributes(span, name, value): if value != "": span.set_attribute(name, value) return + + +def get_tool_calls(item): + if isinstance(item, dict): + if "tool_calls" in item and item["tool_calls"] is not None: + return item["tool_calls"] + return None + + else: + if hasattr(item, "tool_calls") and item.tool_calls is not None: + return item.tool_calls + return None diff --git a/src/langtrace_python_sdk/version.py b/src/langtrace_python_sdk/version.py index 5c809192..b9303606 100644 --- a/src/langtrace_python_sdk/version.py +++ b/src/langtrace_python_sdk/version.py @@ -1 +1 @@ -__version__ = "2.1.26" +__version__ = "2.1.27"