diff --git a/newrelic/hooks/mlmodel_langchain.py b/newrelic/hooks/mlmodel_langchain.py index f4f338bfc..67454e383 100644 --- a/newrelic/hooks/mlmodel_langchain.py +++ b/newrelic/hooks/mlmodel_langchain.py @@ -700,16 +700,19 @@ def _create_successful_chain_run_events( trace_id = linking_metadata.get("trace.id") input_message_list = [_input] output_message_list = [] - try: - output_message_list = [response[0]] if response else [] - except: + if isinstance(response, str): + output_message_list = [response] + else: try: - output_message_list = [str(response)] - except Exception as e: - _logger.warning( - "Unable to capture response inside langchain chain instrumentation. No response message event will be captured. Report this issue to New Relic Support.\n%s", - traceback.format_exception(*sys.exc_info()), - ) + output_message_list = [response[0]] if response else [] + except: + try: + output_message_list = [str(response)] + except Exception as e: + _logger.warning( + "Unable to capture response inside langchain chain instrumentation. No response message event will be captured. Report this issue to New Relic Support.\n%s", + traceback.format_exception(*sys.exc_info()), + ) # Make sure the builtin attributes take precedence over metadata attributes. full_chat_completion_summary_dict = {f"metadata.{key}": value for key, value in metadata.items()} diff --git a/tests/mlmodel_langchain/_mock_external_openai_server.py b/tests/mlmodel_langchain/_mock_external_openai_server.py index f56269b46..a951d499b 100644 --- a/tests/mlmodel_langchain/_mock_external_openai_server.py +++ b/tests/mlmodel_langchain/_mock_external_openai_server.py @@ -381,6 +381,42 @@ "system_fingerprint": None, }, ], + "You are a helpful assistant who generates a random first name. A user will pass in a first letter, and you should generate a name that starts with that first letter.": [ + { + "Content-Type": "application/json", + "openai-model": "gpt-3.5-turbo-0613", + "openai-organization": "foobar-jtbczk", + "openai-processing-ms": "488", + "openai-version": "2020-10-01", + "x-ratelimit-limit-requests": "200", + "x-ratelimit-limit-tokens": "40000", + "x-ratelimit-limit-tokens_usage_based": "40000", + "x-ratelimit-remaining-requests": "199", + "x-ratelimit-remaining-tokens": "39921", + "x-ratelimit-remaining-tokens_usage_based": "39921", + "x-ratelimit-reset-requests": "7m12s", + "x-ratelimit-reset-tokens": "118ms", + "x-ratelimit-reset-tokens_usage_based": "118ms", + "x-request-id": "f3de99e17ccc360430cffa243b74dcbd", + }, + 200, + { + "id": "chatcmpl-8XEjOPNHth7yS2jt1You3fEwB6w9i", + "object": "chat.completion", + "created": 1702932142, + "model": "gpt-3.5-turbo-0613", + "choices": [ + { + "index": 0, + "message": {"role": "assistant", "content": "Milo"}, + "logprobs": None, + "finish_reason": "stop", + } + ], + "usage": {"prompt_tokens": 60, "completion_tokens": 9, "total_tokens": 69}, + "system_fingerprint": None, + }, + ], "9906": [ { "content-type": "application/json", diff --git a/tests/mlmodel_langchain/test_chain.py b/tests/mlmodel_langchain/test_chain.py index 4c5811113..1a3cbbfd7 100644 --- a/tests/mlmodel_langchain/test_chain.py +++ b/tests/mlmodel_langchain/test_chain.py @@ -532,7 +532,7 @@ "ingest_source": "Python", "is_response": True, "virtual_llm": True, - "content": "`", + "content": "```html\n\n\n
\n