Skip to content

Commit 47e61e1

Browse files
authored
Merge pull request #231 from Scale3-Labs/ali/hotfix-fastapi
2 parents 33c63a0 + 3194522 commit 47e61e1

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/langtrace_python_sdk/instrumentation/openai/patch.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828
SERVICE_PROVIDERS,
2929
)
3030
from langtrace_python_sdk.constants.instrumentation.openai import APIS
31-
from langtrace_python_sdk.utils.llm import calculate_prompt_tokens, estimate_tokens
31+
from langtrace_python_sdk.utils.llm import (
32+
calculate_prompt_tokens,
33+
estimate_tokens,
34+
get_tool_calls,
35+
)
3236
from openai._types import NOT_GIVEN
3337

3438

@@ -430,9 +434,10 @@ def traced_method(wrapped, instance, args, kwargs):
430434
# handle tool calls in the kwargs
431435
llm_prompts = []
432436
for item in kwargs.get("messages", []):
433-
if hasattr(item, "tool_calls") and item.tool_calls is not None:
437+
tools = get_tool_calls(item)
438+
if tools is not None:
434439
tool_calls = []
435-
for tool_call in item.tool_calls:
440+
for tool_call in tools:
436441
tool_call_dict = {
437442
"id": tool_call.id if hasattr(tool_call, "id") else "",
438443
"type": tool_call.type if hasattr(tool_call, "type") else "",
@@ -611,9 +616,10 @@ async def traced_method(wrapped, instance, args, kwargs):
611616
# handle tool calls in the kwargs
612617
llm_prompts = []
613618
for item in kwargs.get("messages", []):
614-
if hasattr(item, "tool_calls") and item.tool_calls is not None:
619+
tools = get_tool_calls(item)
620+
if tools is not None:
615621
tool_calls = []
616-
for tool_call in item.tool_calls:
622+
for tool_call in tools:
617623
tool_call_dict = {
618624
"id": tool_call.id if hasattr(tool_call, "id") else "",
619625
"type": tool_call.type if hasattr(tool_call, "type") else "",

src/langtrace_python_sdk/utils/llm.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,15 @@ def set_span_attributes(span, name, value):
6565
if value != "":
6666
span.set_attribute(name, value)
6767
return
68+
69+
70+
def get_tool_calls(item):
71+
if isinstance(item, dict):
72+
if "tool_calls" in item and item["tool_calls"] is not None:
73+
return item["tool_calls"]
74+
return None
75+
76+
else:
77+
if hasattr(item, "tool_calls") and item.tool_calls is not None:
78+
return item.tool_calls
79+
return None
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "2.1.26"
1+
__version__ = "2.1.27"

0 commit comments

Comments
 (0)