Skip to content

bug: TypeError in chat.py when processing events #1505

@notetiene

Description

@notetiene

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 State

Error Location:

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 State

Root 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:

  1. RuntimeV2_x.process_events():

  2. LLMRails.process_events_async():

  3. chat.py:

    • Assumes output_state is a dict based on the incorrect type annotation
    • Attempts to reconstruct: State(**output_state)
    • Fails because output_state is already a State instance
    • output_state_typed: State = cast(State, State(**output_state))

Affected Files:

  1. nemoguardrails/cli/chat.py:

  2. nemoguardrails/rails/llm/llmrails.py:

Related Commit:

Steps To Reproduce

  1. Use the hello_world example:

  2. Run the chat interface:

    nemoguardrails chat --config examples/v2_x/tutorial/hello_world_1/config.yml
  3. 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

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions