Skip to content

fix(topic_safety): handle InternalEvent objects in topic safety actions for Colang 2.0 #1335

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

Pouyanpi
Copy link
Collaborator

Description

Fixes TypeError when using topic safety rails with Colang 2.0 configurations.

Problem

When running topic safety checks in Colang 2.0, the topic_safety_check_input action would fail with:

TypeError: 'InternalEvent' object is not subscriptable

This occurred because Colang 2.0 passes InternalEvent objects to the action, but the code was trying to pass them directly to to_chat_messages() which expects dictionary events.

Solution

  • Convert InternalEvent objects to dictionary format before passing to to_chat_messages()
  • Maintain backward compatibility with dictionary events
  • Follows separation of concerns by keeping the conversion logic in the topic safety action

Testing

  • Added regression test that fails without the fix and passes with it
  • Verified fix works with content safety v2 example configuration

Related PR(s)

#1289

…ns for Colang 2.0

Convert InternalEvent objects to dictionary format before passing to to_chat_messages() to prevent
'InternalEvent' object is not subscriptable
TypeError when using topic safety with Colang 2.0 runtime.
@Pouyanpi Pouyanpi added this to the v0.16.0 milestone Aug 15, 2025
@Pouyanpi Pouyanpi requested review from makeshn and Copilot August 15, 2025 14:23
@Pouyanpi Pouyanpi self-assigned this Aug 15, 2025
@Pouyanpi Pouyanpi added bug Something isn't working colang 2.0 labels Aug 15, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a TypeError that occurs when using topic safety rails with Colang 2.0 configurations. The issue arises because Colang 2.0 passes InternalEvent objects to the topic_safety_check_input action, but the existing code expected dictionary events.

  • Added conversion logic to handle both InternalEvent objects and dictionary events
  • Maintained backward compatibility with existing dictionary-based event handling
  • Added comprehensive regression test to prevent future occurrences

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
nemoguardrails/library/topic_safety/actions.py Added conversion logic to transform InternalEvent objects to dictionary format before passing to to_chat_messages()
tests/test_topic_safety_internalevent.py Added regression test that verifies the fix handles InternalEvent objects without throwing TypeError

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

# convert InternalEvent objects to dictionary format for compatibility with to_chat_messages
dict_events = []
for event in events:
if hasattr(event, "name") and hasattr(event, "arguments"):
Copy link
Preview

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using isinstance check instead of hasattr for more robust type checking. This would be more explicit about the expected type: isinstance(event, InternalEvent)

Suggested change
if hasattr(event, "name") and hasattr(event, "arguments"):
if isinstance(event, InternalEvent):

Copilot uses AI. Check for mistakes.

@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 87.50000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 70.88%. Comparing base (949e422) to head (b42f739).

Files with missing lines Patch % Lines
nemoguardrails/library/topic_safety/actions.py 87.50% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1335      +/-   ##
===========================================
+ Coverage    70.70%   70.88%   +0.18%     
===========================================
  Files          161      161              
  Lines        16312    16319       +7     
===========================================
+ Hits         11533    11568      +35     
+ Misses        4779     4751      -28     
Flag Coverage Δ
python 70.88% <87.50%> (+0.18%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
nemoguardrails/library/topic_safety/actions.py 88.88% <87.50%> (+61.22%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working colang 2.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants