Skip to content

Commit c9d0fe8

Browse files
committed
Save linking metadata on generator proxy.
1 parent 371e8ec commit c9d0fe8

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

newrelic/hooks/mlmodel_openai.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ def _handle_completion_success(transaction, linking_metadata, completion_id, kwa
442442
# The function trace will be exited when in the final iteration of the response
443443
# generator.
444444
return_val._nr_ft = ft
445+
return_val._nr_metadata = linking_metadata
445446
return_val._nr_openai_attrs = getattr(return_val, "_nr_openai_attrs", {})
446447
return_val._nr_openai_attrs["messages"] = kwargs.get("messages", [])
447448
return_val._nr_openai_attrs["temperature"] = kwargs.get("temperature")
@@ -492,10 +493,11 @@ def _record_completion_success(transaction, linking_metadata, completion_id, kwa
492493
response_model = kwargs.get("response.model")
493494
response_id = kwargs.get("id")
494495
output_message_list = []
495-
finish_reason = None
496+
finish_reason = kwargs.get("finish_reason")
496497
if "content" in kwargs:
497498
output_message_list = [{"content": kwargs.get("content"), "role": kwargs.get("role")}]
498-
finish_reason = kwargs.get("finish_reason")
499+
if "tool_call" in finish_reason and not kwargs.get("content"):
500+
output_message_list = []
499501
request_model = kwargs.get("model") or kwargs.get("engine")
500502

501503
request_id = response_headers.get("x-request-id")
@@ -765,7 +767,7 @@ def _record_stream_chunk(self, return_val):
765767

766768
def _record_events_on_stop_iteration(self, transaction):
767769
if hasattr(self, "_nr_ft"):
768-
linking_metadata = get_trace_linking_metadata()
770+
linking_metadata = self._nr_metadata or get_trace_linking_metadata()
769771
self._nr_ft.__exit__(None, None, None)
770772
try:
771773
openai_attrs = getattr(self, "_nr_openai_attrs", {})
@@ -872,6 +874,8 @@ def set_attrs_on_generator_proxy(proxy, instance):
872874
proxy._nr_response_headers = instance._nr_response_headers
873875
if hasattr(instance, "_nr_openai_attrs"):
874876
proxy._nr_openai_attrs = instance._nr_openai_attrs
877+
if hasattr(instance, "_nr_metadata"):
878+
proxy._nr_metadata = instance._nr_metadata
875879

876880

877881
def wrap_engine_api_resource_create_sync(wrapped, instance, args, kwargs):

0 commit comments

Comments
 (0)