Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/openlayer/lib/integrations/async_openai_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ async def handle_async_streaming_create(
num_of_completion_tokens = i + 1
i += 1

choices = getattr(chunk, "choices", None)
if not choices:
yield chunk
continue

delta = chunk.choices[0].delta

if delta.content:
Expand Down Expand Up @@ -235,7 +240,13 @@ async def handle_async_streaming_create(
if collected_output_data:
output_data = "".join(collected_output_data)
else:
collected_function_call["arguments"] = json.loads(collected_function_call["arguments"])
if collected_function_call["arguments"]:
try:
collected_function_call["arguments"] = json.loads(
collected_function_call["arguments"]
)
except json.JSONDecodeError:
pass
output_data = collected_function_call

trace_args = create_trace_args(
Expand Down Expand Up @@ -543,6 +554,12 @@ async def handle_async_streaming_parse(
num_of_completion_tokens = i + 1
i += 1

# Skip chunks with empty choices (e.g., Azure OpenAI heartbeat chunks)
choices = getattr(chunk, "choices", None)
if not choices:
yield chunk
continue

delta = chunk.choices[0].delta

if delta.content:
Expand Down Expand Up @@ -578,9 +595,13 @@ async def handle_async_streaming_parse(
if collected_output_data:
output_data = "".join(collected_output_data)
else:
collected_function_call["arguments"] = json.loads(
collected_function_call["arguments"]
)
if collected_function_call["arguments"]:
try:
collected_function_call["arguments"] = json.loads(
collected_function_call["arguments"]
)
except json.JSONDecodeError:
pass
output_data = collected_function_call

trace_args = create_trace_args(
Expand Down
30 changes: 26 additions & 4 deletions src/openlayer/lib/integrations/groq_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ def stream_chunks(
if i > 0:
num_of_completion_tokens = i + 1

choices = getattr(chunk, "choices", None)
if not choices:
yield chunk
continue

delta = chunk.choices[0].delta

if delta.content:
Expand Down Expand Up @@ -161,7 +166,13 @@ def stream_chunks(
if collected_output_data:
output_data = "".join(collected_output_data)
else:
collected_function_call["arguments"] = json.loads(collected_function_call["arguments"])
if collected_function_call["arguments"]:
try:
collected_function_call["arguments"] = json.loads(
collected_function_call["arguments"]
)
except json.JSONDecodeError:
pass
output_data = collected_function_call

# Get usage data from the last chunk
Expand Down Expand Up @@ -321,14 +332,25 @@ def parse_non_streaming_output_data(
output_data = output_content.strip()
elif output_function_call or output_tool_calls:
if output_function_call:
args_str = getattr(output_function_call, "arguments", "") or ""
try:
arguments = json.loads(args_str) if args_str.strip() else {}
except json.JSONDecodeError:
arguments = args_str
function_call = {
"name": output_function_call.name,
"arguments": json.loads(output_function_call.arguments),
"arguments": arguments,
}
else:
func = output_tool_calls[0].function
args_str = getattr(func, "arguments", "") or ""
try:
arguments = json.loads(args_str) if args_str.strip() else {}
except json.JSONDecodeError:
arguments = args_str
function_call = {
"name": output_tool_calls[0].function.name,
"arguments": json.loads(output_tool_calls[0].function.arguments),
"name": func.name,
"arguments": arguments,
}
output_data = function_call
else:
Expand Down
23 changes: 18 additions & 5 deletions src/openlayer/lib/integrations/openai_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ def stream_chunks(
num_of_completion_tokens = i + 1

# Skip chunks with empty choices (e.g., Azure OpenAI heartbeat chunks)
if not chunk.choices:
choices = getattr(chunk, "choices", None)
if not choices:
yield chunk
continue

Expand Down Expand Up @@ -1344,14 +1345,25 @@ def parse_non_streaming_output_data(
# Function/tool call response
if output_function_call or output_tool_calls:
if output_function_call:
args_str = getattr(output_function_call, "arguments", "") or ""
try:
arguments = json.loads(args_str) if args_str.strip() else {}
except json.JSONDecodeError:
arguments = args_str
return {
"name": output_function_call.name,
"arguments": json.loads(output_function_call.arguments),
"arguments": arguments,
}
else:
func = output_tool_calls[0].function
args_str = getattr(func, "arguments", "") or ""
try:
arguments = json.loads(args_str) if args_str.strip() else {}
except json.JSONDecodeError:
arguments = args_str
return {
"name": output_tool_calls[0].function.name,
"arguments": json.loads(output_tool_calls[0].function.arguments),
"name": func.name,
"arguments": arguments,
}

return None
Expand Down Expand Up @@ -1417,7 +1429,8 @@ def stream_parse_chunks(
num_of_completion_tokens = i + 1

# Skip chunks with empty choices (e.g., Azure OpenAI heartbeat chunks)
if not chunk.choices:
choices = getattr(chunk, "choices", None)
if not choices:
yield chunk
continue

Expand Down