Skip to content

Commit 234a911

Browse files
committed
fix tool calls
1 parent a1a24c6 commit 234a911

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/langtrace_python_sdk/instrumentation/openai/patch.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
get_langtrace_attributes,
4141
get_llm_request_attributes,
4242
get_llm_url,
43+
get_tool_calls,
4344
is_streaming,
4445
)
4546
from openai._types import NOT_GIVEN
@@ -407,12 +408,13 @@ def traced_method(wrapped, instance, args, kwargs):
407408
service_provider = SERVICE_PROVIDERS["PPLX"]
408409
elif "azure" in get_base_url(instance):
409410
service_provider = SERVICE_PROVIDERS["AZURE"]
410-
411411
llm_prompts = []
412412
for item in kwargs.get("messages", []):
413-
if hasattr(item, "tool_calls") and item.tool_calls is not None:
413+
tools = get_tool_calls(item)
414+
if tools is not None:
414415
tool_calls = []
415-
for tool_call in item.tool_calls:
416+
417+
for tool_call in tools:
416418
tool_call_dict = {
417419
"id": tool_call.id if hasattr(tool_call, "id") else "",
418420
"type": tool_call.type if hasattr(tool_call, "type") else "",
@@ -501,12 +503,12 @@ async def traced_method(wrapped, instance, args, kwargs):
501503
service_provider = SERVICE_PROVIDERS["PPLX"]
502504
elif "azure" in get_base_url(instance):
503505
service_provider = SERVICE_PROVIDERS["AZURE"]
504-
505506
llm_prompts = []
506507
for item in kwargs.get("messages", []):
507-
if hasattr(item, "tool_calls") and item.tool_calls is not None:
508+
tools = get_tool_calls(item)
509+
if tools is not None:
508510
tool_calls = []
509-
for tool_call in item.tool_calls:
511+
for tool_call in tools:
510512
tool_call_dict = {
511513
"id": tool_call.id if hasattr(tool_call, "id") else "",
512514
"type": tool_call.type if hasattr(tool_call, "type") else "",
@@ -524,7 +526,7 @@ async def traced_method(wrapped, instance, args, kwargs):
524526
else ""
525527
),
526528
}
527-
tool_calls.append(tool_call_dict)
529+
tool_calls.append(json.dumps(tool_call_dict))
528530
llm_prompts.append(tool_calls)
529531
else:
530532
llm_prompts.append(item)

src/langtrace_python_sdk/utils/llm.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,15 @@ def set_usage_attributes(span, usage):
173173
set_span_attributes(
174174
span, SpanAttributes.LLM_USAGE_SEARCH_UNITS.value, usage["search_units"]
175175
)
176+
177+
178+
def get_tool_calls(item):
179+
if isinstance(item, dict):
180+
if "tool_calls" in item and item["tool_calls"] is not None:
181+
return item["tool_calls"]
182+
return None
183+
184+
else:
185+
if hasattr(item, "tool_calls") and item.tool_calls is not None:
186+
return item.tool_calls
187+
return None

0 commit comments

Comments
 (0)