Skip to content

Commit 12d9bdb

Browse files
committed
refactor: updating logic for get and has attr
1 parent 4b0b071 commit 12d9bdb

File tree

7 files changed

+33
-95
lines changed

7 files changed

+33
-95
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: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,13 @@ def traced_method(wrapped, instance, args, kwargs):
8787
CrewAISpanAttributes(span=span, instance=instance)
8888
result = wrapped(*args, **kwargs)
8989
if result:
90+
class_name = instance.__class__.__name__
91+
span.set_attribute(f"crewai.{class_name.lower()}.result", str(result))
9092
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"):
94-
span.set_attribute("crewai.crew.tasks_output", str((result.tasks_output)))
95-
if hasattr(result, "token_usage"):
96-
span.set_attribute("crewai.crew.token_usage", str((result.token_usage)))
97-
if hasattr(result, "usage_metrics"):
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-
93+
if class_name == "Crew":
94+
for attr in ["tasks_output", "token_usage", "usage_metrics"]:
95+
if hasattr(result, attr):
96+
span.set_attribute(f"crewai.crew.{attr}", str(getattr(result, attr)))
10497
span.end()
10598
return result
10699

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: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,8 @@ def traced_method(wrapped, instance, args, kwargs):
8383
{
8484
"role": "assistant",
8585
"content": {
86-
"url": data.url if hasattr(data, "url") else "",
87-
"revised_prompt": (
88-
data.revised_prompt
89-
if hasattr(data, "revised_prompt")
90-
else ""
91-
),
86+
"url": getattr(data, "url", ""),
87+
"revised_prompt": getattr(data, "revised_prompt", ""),
9288
},
9389
}
9490
]
@@ -146,12 +142,8 @@ async def traced_method(wrapped, instance, args, kwargs):
146142
{
147143
"role": "assistant",
148144
"content": {
149-
"url": data.url if hasattr(data, "url") else "",
150-
"revised_prompt": (
151-
data.revised_prompt
152-
if hasattr(data, "revised_prompt")
153-
else ""
154-
),
145+
"url": getattr(data, "url", ""),
146+
"revised_prompt": getattr(data, "revised_prompt", ""),
155147
},
156148
}
157149
]
@@ -251,21 +243,13 @@ def traced_method(wrapped, instance, args, kwargs):
251243
tool_calls = []
252244
for tool_call in tools:
253245
tool_call_dict = {
254-
"id": tool_call.id if hasattr(tool_call, "id") else "",
255-
"type": tool_call.type if hasattr(tool_call, "type") else "",
246+
"id": getattr(tool_call, "id", ""),
247+
"type": getattr(tool_call, "type", ""),
256248
}
257249
if hasattr(tool_call, "function"):
258250
tool_call_dict["function"] = {
259-
"name": (
260-
tool_call.function.name
261-
if hasattr(tool_call.function, "name")
262-
else ""
263-
),
264-
"arguments": (
265-
tool_call.function.arguments
266-
if hasattr(tool_call.function, "arguments")
267-
else ""
268-
),
251+
"name": getattr(tool_call.function, "name", ""),
252+
"arguments": getattr(tool_call.function, "arguments", ""),
269253
}
270254
tool_calls.append(tool_call_dict)
271255
llm_prompts.append(tool_calls)
@@ -345,21 +329,13 @@ async def traced_method(wrapped, instance, args, kwargs):
345329
tool_calls = []
346330
for tool_call in tools:
347331
tool_call_dict = {
348-
"id": tool_call.id if hasattr(tool_call, "id") else "",
349-
"type": tool_call.type if hasattr(tool_call, "type") else "",
332+
"id": getattr(tool_call, "id", ""),
333+
"type": getattr(tool_call, "type", ""),
350334
}
351335
if hasattr(tool_call, "function"):
352336
tool_call_dict["function"] = {
353-
"name": (
354-
tool_call.function.name
355-
if hasattr(tool_call.function, "name")
356-
else ""
357-
),
358-
"arguments": (
359-
tool_call.function.arguments
360-
if hasattr(tool_call.function, "arguments")
361-
else ""
362-
),
337+
"name": getattr(tool_call.function, "name", ""),
338+
"arguments": getattr(tool_call.function, "arguments", ""),
363339
}
364340
tool_calls.append(json.dumps(tool_call_dict))
365341
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)