Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/langtrace_python_sdk/instrumentation/openai/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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 "",
Expand Down Expand Up @@ -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 "",
Expand Down
12 changes: 12 additions & 0 deletions src/langtrace_python_sdk/utils/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/langtrace_python_sdk/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.1.26"
__version__ = "2.1.27"