Skip to content

Commit 3e92060

Browse files
committed
fix: allow reasoning traces when embeddings_only is True
Refactors the dialog rails validation logic to properly respect the embeddings_only flag, allowing reasoning traces to be enabled when this flag is set to True. Adds tests to verify this behavior.
1 parent d240625 commit 3e92060

File tree

2 files changed

+97
-31
lines changed

2 files changed

+97
-31
lines changed

nemoguardrails/rails/llm/config.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,13 +1207,22 @@ def check_reasoning_traces_with_dialog_rails(cls, values):
12071207
Task.GENERATE_INTENT_STEPS_MESSAGE,
12081208
]
12091209

1210-
# dialog rails are activated (explicitly or implicitly)
1211-
has_dialog_rails = bool(dialog_rails) or (
1210+
embeddings_only = dialog_rails.get("user_messages", {}).get(
1211+
"embeddings_only", False
1212+
)
1213+
1214+
has_dialog_rail_configs = (
12121215
bool(values.get("user_messages"))
1213-
and bool(values.get("bot_messages"))
1214-
# and bool(values.get("flows"))
1216+
or bool(values.get("flows"))
1217+
or bool(values.get("bot_messages"))
12151218
)
12161219

1220+
# dialog rails are activated (explicitly or implicitly) and require validation
1221+
# skip validation when embeddings_only is True
1222+
has_dialog_rails = (
1223+
bool(dialog_rails) or has_dialog_rail_configs
1224+
) and not embeddings_only
1225+
12171226
if has_dialog_rails:
12181227
main_model = next(
12191228
(model for model in models if model.get("type") == "main"), None

tests/test_config_validation.py

Lines changed: 84 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -317,39 +317,49 @@ def test_reasoning_traces_with_implicit_dialog_rails_flows_only():
317317
def test_reasoning_traces_with_implicit_dialog_rails_user_messages_only():
318318
"""Test that reasoning traces cannot be enabled when dialog rails are implicitly enabled through user messages (user canonical forms) only."""
319319

320-
_ = RailsConfig.from_content(
321-
yaml_content="""
322-
models:
323-
- type: main
324-
engine: openai
325-
model: gpt-3.5-turbo-instruct
326-
reasoning_config:
327-
remove_thinking_traces: false
328-
""",
329-
colang_content="""
320+
with pytest.raises(ValueError) as exc_info:
321+
_ = RailsConfig.from_content(
322+
yaml_content="""
323+
models:
324+
- type: main
325+
engine: openai
326+
model: gpt-3.5-turbo-instruct
327+
reasoning_config:
328+
remove_thinking_traces: false
329+
""",
330+
colang_content="""
330331
define user express greeting
331-
"hello"
332-
"hi"
333-
""",
332+
"hello"
333+
"hi"
334+
""",
335+
)
336+
337+
assert "Reasoning traces must be disabled when dialog rails are present" in str(
338+
exc_info.value
334339
)
335340

336341

337342
def test_reasoning_traces_with_bot_messages_only():
338-
"""Test that reasoning traces can exist with bot messages only."""
343+
"""Test that reasoning traces cannot be enabled when bot messages are present."""
339344

340-
_ = RailsConfig.from_content(
341-
yaml_content="""
342-
models:
343-
- type: main
344-
engine: openai
345-
model: gpt-3.5-turbo-instruct
346-
reasoning_config:
347-
remove_thinking_traces: False
348-
""",
349-
colang_content="""
350-
define bot express greeting
351-
"Hello there!"
352-
""",
345+
with pytest.raises(ValueError) as exc_info:
346+
_ = RailsConfig.from_content(
347+
yaml_content="""
348+
models:
349+
- type: main
350+
engine: openai
351+
model: gpt-3.5-turbo-instruct
352+
reasoning_config:
353+
remove_thinking_traces: False
354+
""",
355+
colang_content="""
356+
define bot express greeting
357+
"Hello there!"
358+
""",
359+
)
360+
361+
assert "Reasoning traces must be disabled when dialog rails are present" in str(
362+
exc_info.value
353363
)
354364

355365

@@ -580,3 +590,50 @@ def test_reasoning_traces_with_partial_dedicated_models():
580590
"Please update your config.yml to set 'remove_thinking_traces: true' under reasoning_config"
581591
in str(exc_info.value)
582592
)
593+
594+
595+
def test_reasoning_traces_with_implicit_dialog_rails_embeddings_only():
596+
"""Test that reasoning traces can be enabled when embeddings_only is True, even with user messages."""
597+
598+
_ = RailsConfig.from_content(
599+
yaml_content="""
600+
models:
601+
- type: main
602+
engine: openai
603+
model: gpt-3.5-turbo-instruct
604+
reasoning_config:
605+
remove_thinking_traces: False
606+
rails:
607+
dialog:
608+
user_messages:
609+
embeddings_only: True
610+
""",
611+
colang_content="""
612+
define user express greeting
613+
"hello"
614+
"hi"
615+
""",
616+
)
617+
618+
619+
def test_reasoning_traces_with_bot_messages_embeddings_only():
620+
"""Test that reasoning traces can be enabled when embeddings_only is True, even with bot messages."""
621+
622+
_ = RailsConfig.from_content(
623+
yaml_content="""
624+
models:
625+
- type: main
626+
engine: openai
627+
model: gpt-3.5-turbo-instruct
628+
reasoning_config:
629+
remove_thinking_traces: False
630+
rails:
631+
dialog:
632+
user_messages:
633+
embeddings_only: True
634+
""",
635+
colang_content="""
636+
define bot express greeting
637+
"Hello there!"
638+
""",
639+
)

0 commit comments

Comments
 (0)