Skip to content

Commit d527184

Browse files
committed
fixes again argh
1 parent 4075775 commit d527184

1 file changed

Lines changed: 34 additions & 28 deletions

File tree

  • agentops/instrumentation/providers/openai/wrappers

agentops/instrumentation/providers/openai/wrappers/chat.py

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,41 @@
2020
from agentops.semconv.tool import ToolAttributes
2121
from agentops.semconv.span_kinds import AgentOpsSpanKindValues
2222

23-
from opentelemetry.trace import SpanKind, get_tracer, set_span_in_context, get_current
23+
from opentelemetry.trace import SpanKind, get_tracer, set_span_in_context
2424

2525
logger = logging.getLogger(__name__)
2626

2727
LLM_REQUEST_TYPE = LLMRequestTypeValues.CHAT
2828

2929

30-
def _create_tool_span(parent_span: Span, tool_call_data: Dict[str, Any]):
30+
def _create_tool_span(parent_span: Span, tool_call_data: Dict[str, Any]) -> Span:
3131
"""
32-
Create a distinct span for each tool call.
33-
32+
Create a distinct span for each tool call and return it.
3433
Args:
3534
parent_span: The parent LLM span
3635
tool_call_data: The tool call data dictionary
36+
Returns:
37+
The created tool span.
3738
"""
3839
# Get the tracer for this module
3940
tracer = get_tracer(__name__)
4041

4142
# Create a child span for the tool call
42-
with tracer.start_as_current_span(
43+
tool_span = tracer.start_span(
4344
name=f"tool_call.{tool_call_data['function']['name']}",
4445
kind=SpanKind.INTERNAL,
45-
context=set_span_in_context(parent_span, get_current()),
46-
) as tool_span:
47-
# Set the span kind to TOOL
48-
tool_span.set_attribute("agentops.span.kind", AgentOpsSpanKindValues.TOOL)
46+
context=set_span_in_context(parent_span),
47+
)
48+
49+
# Set the span kind to TOOL
50+
tool_span.set_attribute("agentops.span.kind", AgentOpsSpanKindValues.TOOL)
4951

50-
# Set tool-specific attributes
51-
tool_span.set_attribute(ToolAttributes.TOOL_NAME, tool_call_data["function"]["name"])
52-
tool_span.set_attribute(ToolAttributes.TOOL_PARAMETERS, tool_call_data["function"]["arguments"])
53-
tool_span.set_attribute("tool.call.id", tool_call_data["id"])
54-
tool_span.set_attribute("tool.call.type", tool_call_data["type"])
52+
# Set tool-specific attributes
53+
tool_span.set_attribute(ToolAttributes.TOOL_NAME, tool_call_data["function"]["name"])
54+
tool_span.set_attribute(ToolAttributes.TOOL_PARAMETERS, tool_call_data["function"]["arguments"])
55+
tool_span.set_attribute("tool.call.id", tool_call_data["id"])
56+
tool_span.set_attribute("tool.call.type", tool_call_data["type"])
57+
return tool_span
5558

5659

5760
def handle_chat_attributes(
@@ -231,20 +234,23 @@ def handle_chat_attributes(
231234
# Tool calls
232235
if "tool_calls" in message:
233236
tool_calls = message["tool_calls"]
234-
if tool_calls and span is not None:
235-
for i, tool_call in enumerate(tool_calls):
236-
# Convert tool_call to the format expected by _create_tool_span
237-
function = tool_call.get("function", {})
238-
tool_call_data = {
239-
"id": tool_call.get("id", ""),
240-
"type": tool_call.get("type", "function"),
241-
"function": {
242-
"name": function.get("name", ""),
243-
"arguments": function.get("arguments", ""),
244-
},
245-
}
246-
# Create a child span for this tool call
247-
_create_tool_span(span, tool_call_data)
237+
if tool_calls:
238+
if span is None:
239+
logger.debug("Skipping tool span creation: no parent span provided")
240+
else:
241+
for i, tool_call in enumerate(tool_calls):
242+
# Convert tool_call to the format expected by _create_tool_span
243+
function = tool_call.get("function", {})
244+
tool_call_data = {
245+
"id": tool_call.get("id", ""),
246+
"type": tool_call.get("type", "function"),
247+
"function": {
248+
"name": function.get("name", ""),
249+
"arguments": function.get("arguments", ""),
250+
},
251+
}
252+
# Create a child span for this tool call
253+
_create_tool_span(span, tool_call_data)
248254

249255
# Prompt filter results
250256
if "prompt_filter_results" in response_dict:

0 commit comments

Comments
 (0)