Skip to content

Commit 220cbd8

Browse files
committed
refactor: introduce StreamingNotSupportedError exception
Replace InvalidRailsConfigurationError with a new, more specific StreamingNotSupportedError for cases where streaming is requested but not supported by the configuration. Update all relevant imports, usages, and tests to use the new exception. This improves error clarity and guidance for users encountering streaming configuration issues.
1 parent dd54b5f commit 220cbd8

File tree

6 files changed

+30
-25
lines changed

6 files changed

+30
-25
lines changed

nemoguardrails/cli/chat.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from nemoguardrails.colang.v2_x.runtime.eval import eval_expression
2929
from nemoguardrails.colang.v2_x.runtime.flows import State
3030
from nemoguardrails.colang.v2_x.runtime.runtime import RuntimeV2_x
31-
from nemoguardrails.exceptions import InvalidRailsConfigurationError
31+
from nemoguardrails.exceptions import StreamingNotSupportedError
3232
from nemoguardrails.logging import verbose
3333
from nemoguardrails.logging.verbose import console
3434
from nemoguardrails.rails.llm.options import (
@@ -93,22 +93,19 @@ async def _run_chat_v1_0(
9393

9494
bot_message_text = "".join(bot_message_list)
9595
bot_message = {"role": "assistant", "content": bot_message_text}
96-
except InvalidRailsConfigurationError as e:
97-
error_msg = str(e)
98-
if "stream_async()" in error_msg and "output rails" in error_msg:
99-
raise InvalidRailsConfigurationError(
100-
f"Cannot use --streaming with config `{config_path}` because output rails "
101-
"are configured but streaming is not enabled for them.\n\n"
102-
"To fix this, either:\n"
103-
" 1. Enable streaming for output rails by adding to your config.yml:\n"
104-
" rails:\n"
105-
" output:\n"
106-
" streaming:\n"
107-
" enabled: True\n\n"
108-
" 2. Or run without the --streaming flag:\n"
109-
f" nemoguardrails chat {config_path}"
110-
) from e
111-
raise
96+
except StreamingNotSupportedError as e:
97+
raise StreamingNotSupportedError(
98+
f"Cannot use --streaming with config `{config_path}` because output rails "
99+
"are configured but streaming is not enabled for them.\n\n"
100+
"To fix this, either:\n"
101+
" 1. Enable streaming for output rails by adding to your config.yml:\n"
102+
" rails:\n"
103+
" output:\n"
104+
" streaming:\n"
105+
" enabled: True\n\n"
106+
" 2. Or run without the --streaming flag:\n"
107+
f" nemoguardrails chat {config_path}"
108+
) from e
112109

113110
else:
114111
if rails_app is None:

nemoguardrails/exceptions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"InvalidModelConfigurationError",
2020
"InvalidRailsConfigurationError",
2121
"LLMCallException",
22+
"StreamingNotSupportedError",
2223
]
2324

2425

@@ -49,6 +50,12 @@ class InvalidRailsConfigurationError(ConfigurationError):
4950
pass
5051

5152

53+
class StreamingNotSupportedError(InvalidRailsConfigurationError):
54+
"""Raised when streaming is requested but not supported by the configuration."""
55+
56+
pass
57+
58+
5259
class LLMCallException(Exception):
5360
"""A wrapper around the LLM call invocation exception.
5461

nemoguardrails/rails/llm/llmrails.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
from nemoguardrails.exceptions import (
7474
InvalidModelConfigurationError,
7575
InvalidRailsConfigurationError,
76+
StreamingNotSupportedError,
7677
)
7778
from nemoguardrails.kb.kb import KnowledgeBase
7879
from nemoguardrails.llm.cache import CacheInterface, LFUCache
@@ -1167,7 +1168,7 @@ def _validate_streaming_with_output_rails(self) -> None:
11671168
if len(self.config.rails.output.flows) > 0 and (
11681169
not self.config.rails.output.streaming or not self.config.rails.output.streaming.enabled
11691170
):
1170-
raise InvalidRailsConfigurationError(
1171+
raise StreamingNotSupportedError(
11711172
"stream_async() cannot be used when output rails are configured but "
11721173
"rails.output.streaming.enabled is False. Either set "
11731174
"rails.output.streaming.enabled to True in your configuration, or use "

tests/test_parallel_streaming_output_rails.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
from nemoguardrails import RailsConfig
2626
from nemoguardrails.actions import action
27-
from nemoguardrails.exceptions import InvalidRailsConfigurationError
27+
from nemoguardrails.exceptions import StreamingNotSupportedError
2828
from tests.utils import TestChat
2929

3030

@@ -586,7 +586,7 @@ async def test_parallel_streaming_output_rails_default_config_behavior(
586586

587587
llmrails = LLMRails(parallel_output_rails_default_config)
588588

589-
with pytest.raises(InvalidRailsConfigurationError) as exc_info:
589+
with pytest.raises(StreamingNotSupportedError) as exc_info:
590590
async for _ in llmrails.stream_async(messages=[{"role": "user", "content": "Hi!"}]):
591591
pass
592592

tests/test_streaming.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from nemoguardrails import RailsConfig
2323
from nemoguardrails.actions import action
24-
from nemoguardrails.exceptions import InvalidRailsConfigurationError
24+
from nemoguardrails.exceptions import StreamingNotSupportedError
2525
from nemoguardrails.streaming import StreamingHandler
2626
from tests.utils import TestChat
2727

@@ -494,7 +494,7 @@ async def test_streaming_with_output_rails_disabled_raises_error():
494494
streaming=True,
495495
)
496496

497-
with pytest.raises(InvalidRailsConfigurationError) as exc_info:
497+
with pytest.raises(StreamingNotSupportedError) as exc_info:
498498
async for chunk in chat.app.stream_async(
499499
messages=[{"role": "user", "content": "Hi!"}],
500500
):
@@ -536,7 +536,7 @@ async def test_streaming_with_output_rails_no_streaming_config_raises_error():
536536
streaming=True,
537537
)
538538

539-
with pytest.raises(InvalidRailsConfigurationError) as exc_info:
539+
with pytest.raises(StreamingNotSupportedError) as exc_info:
540540
async for chunk in chat.app.stream_async(
541541
messages=[{"role": "user", "content": "Hi!"}],
542542
):

tests/test_streaming_output_rails.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from nemoguardrails import RailsConfig
2525
from nemoguardrails.actions import action
26-
from nemoguardrails.exceptions import InvalidRailsConfigurationError
26+
from nemoguardrails.exceptions import StreamingNotSupportedError
2727
from nemoguardrails.rails.llm.llmrails import LLMRails
2828
from nemoguardrails.streaming import StreamingHandler
2929
from tests.utils import TestChat
@@ -165,7 +165,7 @@ async def test_streaming_output_rails_blocked_default_config(
165165

166166
llmrails = LLMRails(output_rails_streaming_config_default)
167167

168-
with pytest.raises(InvalidRailsConfigurationError) as exc_info:
168+
with pytest.raises(StreamingNotSupportedError) as exc_info:
169169
async for chunk in llmrails.stream_async(messages=[{"role": "user", "content": "Hi!"}]):
170170
pass
171171

0 commit comments

Comments
 (0)