Skip to content

Commit e64a0d7

Browse files
authored
Merge pull request #323 from Scale3-Labs/refactor-hasttr-logic
refactor: updating logic for get and has attr
2 parents 38ba1c5 + 9a5559e commit e64a0d7

File tree

7 files changed

+59
-115
lines changed

7 files changed

+59
-115
lines changed

src/langtrace_python_sdk/instrumentation/cohere/patch.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,7 @@ def traced_method(wrapped, instance, args, kwargs):
367367
}
368368
for item in chat_history
369369
]
370-
if len(history) > 0:
371-
prompts = history + prompts
372-
if len(system_prompts) > 0:
373-
prompts = system_prompts + prompts
370+
prompts = system_prompts + history + prompts
374371

375372
span_attributes = {
376373
**get_langtrace_attributes(version, service_provider),

src/langtrace_python_sdk/instrumentation/crewai/patch.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
from langtrace_python_sdk.constants import LANGTRACE_SDK_NAME
1010
from langtrace_python_sdk.constants.instrumentation.common import (
11-
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY, SERVICE_PROVIDERS)
11+
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY,
12+
SERVICE_PROVIDERS,
13+
)
1214
from langtrace_python_sdk.utils import set_span_attribute
1315
from langtrace_python_sdk.utils.llm import get_span_name, set_span_attributes
1416
from langtrace_python_sdk.utils.misc import serialize_args, serialize_kwargs
@@ -44,7 +46,9 @@ def traced_method(wrapped, instance, args, kwargs):
4446
set_span_attributes(span, attributes)
4547
result = wrapped(*args, **kwargs)
4648
if result is not None and len(result) > 0:
47-
set_span_attribute(span, "crewai.memory.storage.rag_storage.outputs", str(result))
49+
set_span_attribute(
50+
span, "crewai.memory.storage.rag_storage.outputs", str(result)
51+
)
4852
if result:
4953
span.set_status(Status(StatusCode.OK))
5054
span.end()
@@ -87,20 +91,17 @@ def traced_method(wrapped, instance, args, kwargs):
8791
CrewAISpanAttributes(span=span, instance=instance)
8892
result = wrapped(*args, **kwargs)
8993
if result:
94+
class_name = instance.__class__.__name__
95+
span.set_attribute(
96+
f"crewai.{class_name.lower()}.result", str(result)
97+
)
9098
span.set_status(Status(StatusCode.OK))
91-
if instance.__class__.__name__ == "Crew":
92-
span.set_attribute("crewai.crew.result", str(result))
93-
if hasattr(result, "tasks_output") and result.tasks_output is not None:
94-
span.set_attribute("crewai.crew.tasks_output", str((result.tasks_output)))
95-
if hasattr(result, "token_usage") and result.token_usage is not None:
96-
span.set_attribute("crewai.crew.token_usage", str((result.token_usage)))
97-
if hasattr(result, "usage_metrics") and result.usage_metrics is not None:
98-
span.set_attribute("crewai.crew.usage_metrics", str((result.usage_metrics)))
99-
elif instance.__class__.__name__ == "Agent":
100-
span.set_attribute("crewai.agent.result", str(result))
101-
elif instance.__class__.__name__ == "Task":
102-
span.set_attribute("crewai.task.result", str(result))
103-
99+
if class_name == "Crew":
100+
for attr in ["tasks_output", "token_usage", "usage_metrics"]:
101+
if hasattr(result, attr):
102+
span.set_attribute(
103+
f"crewai.crew.{attr}", str(getattr(result, attr))
104+
)
104105
span.end()
105106
return result
106107

src/langtrace_python_sdk/instrumentation/gemini/patch.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,9 @@ async def traced_method(wrapped, instance, args, kwargs):
100100

101101

102102
def get_llm_model(instance):
103-
llm_model = "unknown"
104-
if hasattr(instance, "_model_id"):
105-
llm_model = instance._model_id
106103
if hasattr(instance, "_model_name"):
107-
llm_model = instance._model_name.replace("models/", "")
108-
return llm_model
104+
return instance._model_name.replace("models/", "")
105+
return getattr(instance, "_model_id", "unknown")
109106

110107

111108
def serialize_prompts(args, kwargs, instance):

src/langtrace_python_sdk/instrumentation/groq/patch.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def traced_method(wrapped, instance, args, kwargs):
158158
usage = result.usage
159159
set_usage_attributes(span, dict(usage))
160160

161-
span.set_status(StatusCode.OK)
161+
span.set_status(Status(StatusCode.OK))
162162
span.end()
163163
return result
164164
else:
@@ -257,7 +257,7 @@ def handle_streaming_response(
257257
span, [{"role": "assistant", "content": "".join(result_content)}]
258258
)
259259

260-
span.set_status(StatusCode.OK)
260+
span.set_status(Status(StatusCode.OK))
261261
span.end()
262262

263263
# return the wrapped method
@@ -282,21 +282,13 @@ async def traced_method(wrapped, instance, args, kwargs):
282282
tool_calls = []
283283
for tool_call in item.tool_calls:
284284
tool_call_dict = {
285-
"id": tool_call.id if hasattr(tool_call, "id") else "",
286-
"type": tool_call.type if hasattr(tool_call, "type") else "",
285+
"id": getattr(tool_call, "id", ""),
286+
"type": getattr(tool_call, "type", ""),
287287
}
288288
if hasattr(tool_call, "function"):
289289
tool_call_dict["function"] = {
290-
"name": (
291-
tool_call.function.name
292-
if hasattr(tool_call.function, "name")
293-
else ""
294-
),
295-
"arguments": (
296-
tool_call.function.arguments
297-
if hasattr(tool_call.function, "arguments")
298-
else ""
299-
),
290+
"name": getattr(tool_call.function, "name", ""),
291+
"arguments": getattr(tool_call.function, "arguments", ""),
300292
}
301293
tool_calls.append(tool_call_dict)
302294
llm_prompts.append(tool_calls)
@@ -459,11 +451,7 @@ async def ahandle_streaming_response(
459451
tool_call.function.arguments
460452
)
461453
completion_tokens += token_counts
462-
content = content + [
463-
tool_call.function.arguments
464-
]
465-
else:
466-
content = content + []
454+
content += [tool_call.function.arguments]
467455
else:
468456
content = []
469457
span.add_event(

src/langtrace_python_sdk/instrumentation/openai/patch.py

Lines changed: 28 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,16 @@ def traced_method(
7474
if hasattr(result, "data") and len(result.data) > 0
7575
else None
7676
)
77-
if data is not None:
78-
response = [
79-
{
80-
"role": "assistant",
81-
"content": {
82-
"url": data.url if hasattr(data, "url") else "",
83-
"revised_prompt": (
84-
data.revised_prompt
85-
if hasattr(data, "revised_prompt")
86-
else ""
87-
),
88-
},
89-
}
90-
]
91-
set_event_completion(span, response)
77+
response = [
78+
{
79+
"role": "assistant",
80+
"content": {
81+
"url": getattr(data, "url", ""),
82+
"revised_prompt": getattr(data, "revised_prompt", ""),
83+
},
84+
}
85+
]
86+
set_event_completion(span, response)
9287

9388
span.set_status(StatusCode.OK)
9489
return result
@@ -140,21 +135,16 @@ async def traced_method(
140135
if hasattr(result, "data") and len(result.data) > 0
141136
else None
142137
)
143-
if data is not None:
144-
response = [
145-
{
146-
"role": "assistant",
147-
"content": {
148-
"url": data.url if hasattr(data, "url") else "",
149-
"revised_prompt": (
150-
data.revised_prompt
151-
if hasattr(data, "revised_prompt")
152-
else ""
153-
),
154-
},
155-
}
156-
]
157-
set_event_completion(span, response)
138+
response = [
139+
{
140+
"role": "assistant",
141+
"content": {
142+
"url": getattr(data, "url", ""),
143+
"revised_prompt": getattr(data, "revised_prompt", ""),
144+
},
145+
}
146+
]
147+
set_event_completion(span, response)
158148

159149
span.set_status(StatusCode.OK)
160150
return result
@@ -257,21 +247,13 @@ def traced_method(
257247
tool_calls = []
258248
for tool_call in tools:
259249
tool_call_dict = {
260-
"id": tool_call.id if hasattr(tool_call, "id") else "",
261-
"type": tool_call.type if hasattr(tool_call, "type") else "",
250+
"id": getattr(tool_call, "id", ""),
251+
"type": getattr(tool_call, "type", ""),
262252
}
263253
if hasattr(tool_call, "function"):
264254
tool_call_dict["function"] = {
265-
"name": (
266-
tool_call.function.name
267-
if hasattr(tool_call.function, "name")
268-
else ""
269-
),
270-
"arguments": (
271-
tool_call.function.arguments
272-
if hasattr(tool_call.function, "arguments")
273-
else ""
274-
),
255+
"name": getattr(tool_call.function, "name", ""),
256+
"arguments": getattr(tool_call.function, "arguments", ""),
275257
}
276258
tool_calls.append(tool_call_dict)
277259
llm_prompts.append(tool_calls)
@@ -353,21 +335,13 @@ async def traced_method(
353335
tool_calls = []
354336
for tool_call in tools:
355337
tool_call_dict = {
356-
"id": tool_call.id if hasattr(tool_call, "id") else "",
357-
"type": tool_call.type if hasattr(tool_call, "type") else "",
338+
"id": getattr(tool_call, "id", ""),
339+
"type": getattr(tool_call, "type", ""),
358340
}
359341
if hasattr(tool_call, "function"):
360342
tool_call_dict["function"] = {
361-
"name": (
362-
tool_call.function.name
363-
if hasattr(tool_call.function, "name")
364-
else ""
365-
),
366-
"arguments": (
367-
tool_call.function.arguments
368-
if hasattr(tool_call.function, "arguments")
369-
else ""
370-
),
343+
"name": getattr(tool_call.function, "name", ""),
344+
"arguments": getattr(tool_call.function, "arguments", ""),
371345
}
372346
tool_calls.append(json.dumps(tool_call_dict))
373347
llm_prompts.append(tool_calls)

src/langtrace_python_sdk/instrumentation/vertexai/patch.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,9 @@ def is_streaming_response(response):
102102

103103

104104
def get_llm_model(instance):
105-
llm_model = "unknown"
106-
if hasattr(instance, "_model_id"):
107-
llm_model = instance._model_id
108105
if hasattr(instance, "_model_name"):
109-
llm_model = instance._model_name.replace("publishers/google/models/", "")
110-
return llm_model
106+
return instance._model_name.replace("models/", "")
107+
return getattr(instance, "_model_id", "unknown")
111108

112109

113110
def serialize_prompts(args, kwargs):

src/langtrace_python_sdk/instrumentation/weaviate/patch.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,9 @@ def get_response_object_attributes(response_object):
9696
response_attributes = {
9797
**response_object.properties,
9898
"uuid": str(response_object.uuid) if hasattr(response_object, "uuid") else None,
99-
"collection": (
100-
response_object.collection
101-
if hasattr(response_object, "collection")
102-
else None
103-
),
104-
"vector": (
105-
response_object.vector if hasattr(response_object, "vector") else None
106-
),
107-
"references": (
108-
response_object.references
109-
if hasattr(response_object, "references")
110-
else None
111-
),
99+
"collection": getattr(response_object, "collection", None),
100+
"vector": getattr(response_object, "vector", None),
101+
"references": getattr(response_object, "references", None),
112102
"metadata": (
113103
extract_metadata(response_object.metadata)
114104
if hasattr(response_object, "metadata")

0 commit comments

Comments
 (0)