-
Notifications
You must be signed in to change notification settings - Fork 563
Description
Did you check docs and existing issues?
- I have read all the NeMo-Guardrails docs
- I have updated the package to the latest version before submitting this issue
- (optional) I have used the develop branch
- I have searched the existing issues of NeMo-Guardrails
Python version (python --version)
Python 3.10.13
Operating system/version
Ubuntu 22.04
NeMo-Guardrails version (if you must use a specific version and not the latest
Develop
Describe the bug
A TypeError occurs when running the hello_world example after commit df51265 from #1380. The error message is:
TypeError: nemoguardrails.colang.v2_x.runtime.flows.State() argument after ** must be a mapping, not StateError Location:
Guardrails/nemoguardrails/cli/chat.py
Line 543 in d2bfaea
| output_state_typed: State = cast(State, State(**output_state)) |
Full Stack Trace:
File "nemoguardrails/cli/chat.py", line 543, in _process_input_events
output_state_typed: State = cast(State, State(**output_state))
TypeError: nemoguardrails.colang.v2_x.runtime.flows.State() argument after ** must be a mapping, not StateRoot Cause:
In commit df51265, the code in nemoguardrails/cli/chat.py was refactored to assume process_events_async returns a dictionary for the state parameter. However, process_events_async actually returns a State object (not a dict), causing the code to attempt State(**output_state) where output_state is already a State instance.
Type Mismatch Chain:
-
RuntimeV2_x.process_events():- Returns:
Tuple[List[Dict[str, Any]], State] - Returns a
Stateobject, not a dict ) -> Tuple[List[Dict[str, Any]], State]:
- Returns:
-
LLMRails.process_events_async():- Calls
self.runtime.process_events()and returns the result directly - Type annotation is incorrect:
Guardrails/nemoguardrails/rails/llm/llmrails.py
Line 1433 in d2bfaea
) -> Tuple[List[dict], dict]: ) -> Tuple[List[Dict[str, Any]], State]:
- Calls
-
chat.py:- Assumes
output_stateis a dict based on the incorrect type annotation - Attempts to reconstruct:
State(**output_state) - Fails because
output_stateis already aStateinstance Guardrails/nemoguardrails/cli/chat.py
Line 543 in d2bfaea
output_state_typed: State = cast(State, State(**output_state))
- Assumes
Affected Files:
-
nemoguardrails/cli/chat.py:- Line 507:
chat_state.output_state = cast(State, State(**output_state)) - Line 543:
output_state_typed: State = cast(State, State(**output_state)) - Both lines incorrectly assume
output_stateis a dict Guardrails/nemoguardrails/cli/chat.py
Line 507 in d2bfaea
chat_state.output_state = cast(State, State(**output_state)) Guardrails/nemoguardrails/cli/chat.py
Line 543 in d2bfaea
output_state_typed: State = cast(State, State(**output_state))
- Line 507:
-
nemoguardrails/rails/llm/llmrails.py:- Type annotation incorrectly states:
-> Tuple[List[dict], dict] - Should be:
-> Tuple[List[dict], State] Guardrails/nemoguardrails/rails/llm/llmrails.py
Line 1433 in d2bfaea
) -> Tuple[List[dict], dict]:
- Type annotation incorrectly states:
Related Commit:
- Commit: df51265
- PR: chore(types): Type-clean /cli (37 errors) #1380
- Title: "chore(types): Type-clean /cli (37 errors)"
Steps To Reproduce
-
Use the hello_world example:
-
Run the chat interface:
nemoguardrails chat --config examples/v2_x/tutorial/hello_world_1/config.yml
-
The error occurs immediately when processing input events during initialization or when processing user input.
Expected Behavior
The chat interface should start successfully and be able to process user input events without errors. The process_events_async method should return a State object, and the code should handle it correctly without attempting to unpack it as a dictionary.
Actual Behavior
The chat interface crashes with a TypeError when attempting to process events. The error occurs because the code tries to unpack a State object as if it were a dictionary using State(**output_state), but output_state is already a State instance, not a mapping.
Impact:
- Severity: High - Breaks the chat interface for v2.x runtime
- Scope: Affects all users of the v2.x runtime with the chat CLI
- Workaround: None - the error occurs during normal operation