From 093041bf9bab22b77641ea65d1a15e7a085303a0 Mon Sep 17 00:00:00 2001 From: Giovanni Liva Date: Tue, 8 Apr 2025 14:41:54 +0200 Subject: [PATCH 1/3] fix: error in RunnableRails with tracing enabled Signed-off-by: Giovanni Liva --- nemoguardrails/rails/llm/llmrails.py | 1 - 1 file changed, 1 deletion(-) diff --git a/nemoguardrails/rails/llm/llmrails.py b/nemoguardrails/rails/llm/llmrails.py index 320695006..850d2b48b 100644 --- a/nemoguardrails/rails/llm/llmrails.py +++ b/nemoguardrails/rails/llm/llmrails.py @@ -938,7 +938,6 @@ async def generate_async( input=messages, response=res, adapters=self._log_adapters ) await tracer.export_async() - res = res.response[0] return res else: # If a prompt is used, we only return the content of the message. From 201e97c2c00fc8b036e7cd4377905c30943944ee Mon Sep 17 00:00:00 2001 From: Giovanni Liva Date: Tue, 8 Apr 2025 15:29:10 +0200 Subject: [PATCH 2/3] add tests Signed-off-by: Giovanni Liva --- tests/test_tracing.py | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/tests/test_tracing.py b/tests/test_tracing.py index 2e51e8f48..02de5cd06 100644 --- a/tests/test_tracing.py +++ b/tests/test_tracing.py @@ -14,11 +14,15 @@ # limitations under the License. import asyncio +import os + +import pytest import unittest -from unittest.mock import AsyncMock, MagicMock +from unittest.mock import AsyncMock, MagicMock, patch +from nemoguardrails import LLMRails from nemoguardrails.logging.explain import LLMCallInfo -from nemoguardrails.rails.llm.config import TracingConfig +from nemoguardrails.rails.llm.config import TracingConfig, RailsConfig from nemoguardrails.rails.llm.options import ( ActivatedRail, ExecutedAction, @@ -200,6 +204,39 @@ def test_export_async(self): asyncio.run(tracer_non_empty.export_async()) adapter_non_empty.transform_async.assert_called_once() +@patch.object(Tracer, "export_async", return_value="") +@pytest.mark.asyncio +async def test_tracing_enable_no_crash_issue_1093(mockTracer): + config = RailsConfig.from_content( + colang_content=""" + define user express greeting + "hello" + + define flow + user express greeting + bot express greeting + + define bot express greeting + "Hello World!\\n NewLine World!" + """, + config={ + "models": [], + "rails": {"dialog": {"user_messages": {"embeddings_only": True}}}, + }, + ) + # Force Tracing to be enabled + config.tracing.enabled = True + rails = LLMRails(config) + res = await rails.generate_async( + messages=[ + {"role": "user", "content": "hi!"}, + {"role": "assistant", "content": "hi!"}, + {"role": "user", "content": "hi!"}, + ] + ) + assert mockTracer.called == True + assert res.response != None + if __name__ == "__main__": unittest.main() From d937e3e1aac1e2d9eb9096f58d0102e82966b4f5 Mon Sep 17 00:00:00 2001 From: Giovanni Liva Date: Wed, 9 Apr 2025 08:56:13 +0200 Subject: [PATCH 3/3] format Signed-off-by: Giovanni Liva --- tests/test_tracing.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_tracing.py b/tests/test_tracing.py index 02de5cd06..d376ab4e7 100644 --- a/tests/test_tracing.py +++ b/tests/test_tracing.py @@ -204,6 +204,7 @@ def test_export_async(self): asyncio.run(tracer_non_empty.export_async()) adapter_non_empty.transform_async.assert_called_once() + @patch.object(Tracer, "export_async", return_value="") @pytest.mark.asyncio async def test_tracing_enable_no_crash_issue_1093(mockTracer):