-
Notifications
You must be signed in to change notification settings - Fork 563
fix(cli): Fix TypeError in v2.x chat due to incorrect State/dict conv… #1509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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
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!
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3 files reviewed, no comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3 files reviewed, no comments
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
#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
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