fix(cli): Fix TypeError in v2.x chat due to incorrect State/dict conv… - #1509
Merged
Conversation
…ersion Fixes incorrect type assumptions in PR #1380 that caused TypeError when using v2.x chat CLI. The bug incorrectly assumed process_events_async returns dict and tried to convert State objects with State(**output_state). Changes: - Fix type annotations in LLMRails.process_events_async to return Union[dict, State] - Remove incorrect asdict() conversion and State(**) reconstruction in chat.py - Add integration tests to prevent regression
Contributor
Greptile OverviewGreptile SummaryThis PR correctly fixes a Root Cause Analysis:
The Fix:
Key Changes:
The fix is minimal, targeted, and addresses the exact root cause without over-engineering. Confidence Score: 5/5
Important Files ChangedFile Analysis
Sequence DiagramsequenceDiagram
participant User
participant ChatCLI as chat.py (_run_chat_v2_x)
participant LLMRails as llmrails.py (LLMRails)
participant RuntimeV2 as runtime.py (RuntimeV2_x)
Note over User,RuntimeV2: Before Fix (PR #1380 - BROKEN)
User->>ChatCLI: User input
ChatCLI->>ChatCLI: asdict(chat_state.state)
Note right of ChatCLI: Incorrectly converts State to dict
ChatCLI->>LLMRails: process_events_async(events, dict_state)
LLMRails->>RuntimeV2: process_events(events, dict_state)
RuntimeV2-->>LLMRails: (output_events, State)
Note right of RuntimeV2: Returns State object
LLMRails-->>ChatCLI: (output_events, State)
ChatCLI->>ChatCLI: State(**output_state)
Note right of ChatCLI: TypeError! output_state is already State
ChatCLI--xUser: CRASH
Note over User,RuntimeV2: After Fix (This PR - CORRECT)
User->>ChatCLI: User input
ChatCLI->>LLMRails: process_events_async(events, State)
Note right of ChatCLI: Pass State object directly
LLMRails->>RuntimeV2: process_events(events, State)
RuntimeV2-->>LLMRails: (output_events, State)
Note right of RuntimeV2: Returns State object
LLMRails-->>ChatCLI: (output_events, State)
ChatCLI->>ChatCLI: cast(State, output_state)
Note right of ChatCLI: Simple type cast, no conversion
ChatCLI-->>User: Success!
|
cparisien
approved these changes
Nov 24, 2025
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Pouyanpi
added a commit
that referenced
this pull request
Nov 26, 2025
#1509) * fix(cli): Fix TypeError in v2.x chat due to incorrect State/dict conversion Fixes incorrect type assumptions in PR #1380 that caused TypeError when using v2.x chat CLI. The bug incorrectly assumed process_events_async returns dict and tried to convert State objects with State(**output_state). Changes: - Fix type annotations in LLMRails.process_events_async to return Union[dict, State] - Remove incorrect asdict() conversion and State(**) reconstruction in chat.py - Add integration tests to prevent regression
4 tasks
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Description
Fixes issue #1505 where PR #1380 introduced a
TypeErrorin the v2.x chat CLI due to incorrect type conversion betweenStateanddict.Root Cause:
LLMRails.process_events_async()was incorrect (claimed to returndictfor both v1.0 and v2.x)Stateobjects, not dictschat.pybased on the wrong annotation, addingasdict()andState(**output_state)conversionsTypeError: State() argument after ** must be a mapping, not StateWhy Tests Didn't Catch This:
runtime.process_events()directly, bypassing theLLMRailswrapperLLMRails.process_events_async()with v2.xRunning
nemoguardrails chat --config=./examples/configs/nemoguards_v2works as before