Skip to content

Conversation

tgasser-nv
Copy link
Collaborator

@tgasser-nv tgasser-nv commented Sep 10, 2025

Description

This PR cleans type-errors reported by Pyright. These were all checked with unit-tests and validated with Pyright after the fixes.

Each section below corresponds to a subdirectory under nemoguardrails/library and lists the type fixes that were applied, along with the GitHub username of the original author responsible for the problematic code. The aim is to check with the original authors and companies who own the integrations where possible before merging these changes.


activefence/

No type errors reported for this directory.


attention/

Author: @sklingler (Severin Klingler)

Type Error Fixed:

  • Line 121: Cannot assign to attribute "corrected_datetime" for class "ActionEvent"
    • Fix: Used setattr() to dynamically add the corrected_datetime attribute since neither ActionEvent nor its base class Event have this attribute defined.
    • Original problematic code: event.corrected_datetime = timestamp + timedelta(seconds=offsets.get(event.name, 0.0))
    • Fixed code: setattr(event, "corrected_datetime", corrected_time)

autoalign/

Author: @abhijitpal1247 (Abhijit Pal)

Type Errors Fixed:

  • Lines 176-180: Multiple "keys" is not a known attribute of "None" and Object of type "None" is not subscriptable errors

    • Fix: Added null check for task_config before accessing its methods and properties
    • Original problematic code: Direct access to task_config.keys() and task_config[task]
    • Fixed code: Wrapped operations in if task_config is not None: block
  • Lines 290, 330, 370, 408-409: "get" is not a known attribute of "None" errors

    • Fix: Added null checks before accessing context methods
  • Lines 303, 343, 385, 419-420: Argument type errors for functions expecting str but receiving None

    • Fix: Added proper null checks and type validation before function calls

clavata/

Author: @ilyasnv (Ilias)

Type Error Fixed:

  • Line 164: "policy_id" is possibly unbound
    • Fix: Moved the assignment of policy_id outside the try block to ensure it's always defined before use
    • Original problematic code: policy_id was assigned inside try block but used in exception handler
    • Fixed code: Extracted policy_id = config.policies.get(policy) and added null check before the try block

cleanlab/

Author: @ashishsardana21 (Ashish Sardana)

Type Errors Fixed:

  • Line 47: Import "cleanlab_studio" could not be resolved

    • Fix: This is an optional dependency, error is expected when package not installed
  • Lines 53-54: "get" is not a known attribute of "None" errors

    • Fix: Added null checks before accessing context methods

content_safety/

Author: @Pouyanpi (Pouyan Rezakhani)

Type Errors Fixed:

  • Lines 75, 154: Argument of type "LLMCallInfo" cannot be assigned to parameter "value" of type "None"

    • Fix: Updated variable type annotations to accept LLMCallInfo instead of None
  • Lines 82, 159: Argument of type "str" cannot be assigned to parameter "task" of type "Task"

    • Fix: Used proper Task enum values instead of string literals

factchecking/align_score/

Author: @Pouyanpi (Pouyan)

Type Errors Fixed:

  • Lines 55-56: "get" is not a known attribute of "None" errors

    • Fix: Added null checks before accessing context methods
  • server.py Lines 20, 23: Missing import errors for nltk and alignscore

    • Fix: These are optional dependencies, errors expected when packages not installed
  • server.py Line 54: Argument of type "str | None" cannot be assigned to parameter "a" of type "StrPath"

    • Fix: Added null check before path join operation

fiddler/

Author: @copperstick6 (William Han)

Type Errors Fixed:

  • Lines 96, 124, 154-155: "get" is not a known attribute of "None" errors
    • Fix: Added null checks before accessing context and configuration methods

gcp_moderate_text/

Author: @Pouyanpi (Pouyan)

Type Errors Fixed:

  • Lines 20, 119: "language_v2" is unknown import symbol

    • Fix: This is expected when Google Cloud libraries are not installed
  • Line 127: "get" is not a known attribute of "None"

    • Fix: Added null check before accessing context methods

guardrails_ai/

Author: @Pouyanpi (Pouyan)

Type Errors Fixed:

  • Line 24: Import "guardrails" could not be resolved

    • Fix: This is an optional dependency, error expected when package not installed
  • Lines 113, 117-119, 152, 156-158: Multiple "get" is not a known attribute of "None" and attribute access errors

    • Fix: Added comprehensive null checks before accessing configuration and context attributes

hallucination/

Author: @Pouyanpi (Pouyan)

Type Errors Fixed:

  • Line 56: Import "langchain_openai" could not be resolved

    • Fix: This is an optional dependency, error expected when package not installed
  • Lines 62-63: "get" is not a known attribute of "None" errors

    • Fix: Added null checks before accessing context methods
  • Lines 131, 134: Type assignment and attribute access errors

    • Fix: Updated type annotations and added null checks for configuration attributes

injection_detection/

Author: @Pouyanpi (Pouyan Rezakhani) and @egalikin (Erick Galinkin)

Type Errors Fixed:

  • Lines 133, 140, 144, 162: "attribute" is not a known attribute of "None" errors

    • Fix: Added null checks before accessing configuration attributes
  • Lines 162, 167, 212, 256, 292: Type annotation and return type errors

    • Fix: Corrected tuple type annotations and variable type declarations
  • Lines 196, 204-205: YARA compilation errors

    • Fix: Added null checks for YARA module availability

jailbreak_detection/

Author: @egalikin (Erick Galinkin)

Type Errors Fixed:

  • Lines 55-59: Multiple configuration attribute access errors

    • Fix: Added null checks before accessing jailbreak detection configuration
  • Lines 70, 72, 78: Function argument type errors expecting str but receiving None

    • Fix: Added proper type validation for prompt strings
  • Lines 97-100: Configuration attribute access errors

    • Fix: Added null checks for NIM configuration attributes
  • Lines 133, 140, 145: Variable binding and type errors

    • Fix: Proper initialization and type handling for jailbreak detection variables

llama_guard/

Author: @PrasoonV (Prasoon Varshney)

Type Errors Fixed:

  • Lines 67, 112-113: "get" is not a known attribute of "None" errors

    • Fix: Added null checks before accessing context methods
  • Lines 77, 79-80, 125, 127-128: LLM argument type errors

    • Fix: Added proper type validation for LLM instances to ensure they're not None before function calls

patronusai/

Author: @varun (Varun Joshi)

Type Errors Fixed:

  • Lines 86-88, 256-258: "get" is not a known attribute of "None" errors

    • Fix: Added null checks before accessing context methods
  • Lines 115, 118, 120: LLM argument type errors and variable assignment errors

    • Fix: Added proper type validation for LLM instances and context variables
  • Line 260: "output" is not a known attribute of "None"

    • Fix: Added null check before accessing output attributes

self_check/facts/

Author: @Pouyanpi (Pouyan Rezakhani)

Type Errors Fixed:

  • Lines 54-55: "get" is not a known attribute of "None" errors

    • Fix: Added null checks before accessing context methods
  • Lines 73, 75-76: LLM argument type errors and configuration attribute access

    • Fix: Added proper type validation for LLM instances and configuration attributes

self_check/input_check/

Author: @Pouyanpi (Pouyan Rezakhani)

Type Errors Fixed:

  • Line 52: "get" is not a known attribute of "None"

    • Fix: Added null check before accessing context methods
  • Lines 67, 70, 72: LLM argument type errors and configuration attribute access

    • Fix: Added proper type validation for LLM instances and configuration attributes

self_check/output_check/

Author: @Pouyanpi (Pouyan Rezakhani)

Type Errors Fixed:

  • Lines 54-55: "get" is not a known attribute of "None" errors

    • Fix: Added null checks before accessing context methods
  • Lines 72, 75, 77: LLM argument type errors and configuration attribute access

    • Fix: Added proper type validation for LLM instances and configuration attributes

sensitive_data_detection/

Author: @Pouyanpi (Pouyan Rezakhani)

Type Errors Fixed:

  • Lines 20-23, 43, 52: Multiple import resolution errors for presidio_* and spacy

    • Fix: These are optional dependencies, errors expected when packages not installed
  • Lines 71, 84, 165, 173: Variable binding errors for imported classes

    • Fix: Added proper error handling for missing optional dependencies
  • Lines 131, 171: Function argument type errors

    • Fix: Added null checks before passing configuration objects to functions

topic_safety/

Author: @makeshn (Makesh)

Type Errors Fixed:

  • Lines 53-54: Cannot access attribute "name"/"arguments" for class "dict[Unknown, Unknown]"

    • Fix: Added proper type handling for event objects vs dictionaries
  • Line 90: Cannot access attribute "strip" for class "List[dict[Unknown, Unknown]]"

    • Fix: Added proper type checking for string vs list objects
  • Lines 97, 103: Type assignment and variable binding errors

    • Fix: Added proper type validation and variable initialization

Summary

A total of 145 type errors were fixed across 28 files in the library directory. The most common issues were:

  1. Null pointer access: Many errors involved accessing methods on potentially None objects
  2. Optional dependency imports: Several integrations have optional dependencies that cause import errors when not installed
  3. Type mismatches: Arguments passed to functions with incorrect types
  4. Variable binding: Variables used before being properly initialized

The fixes generally involved adding proper null checks, type validation, and error handling for optional dependencies.


Test Plan

Type-checking

$ pyright nemoguardrails/library
0 errors, 0 warnings, 0 informations

Unit-tests

$  poetry run pytest -n4 tests
=================================================== test session starts ====================================================
platform darwin -- Python 3.13.2, pytest-8.3.4, pluggy-1.5.0
rootdir: /Users/tgasser/projects/nemo_guardrails
configfile: pytest.ini
plugins: cov-6.0.0, xdist-3.8.0, httpx-0.35.0, asyncio-0.25.3, anyio-4.8.0, profiling-1.8.1, langsmith-0.3.6
asyncio: mode=Mode.STRICT, asyncio_default_fixture_loop_scope=function
4 workers [1440 items]
.ss.sss..s.............s.....s.......................................................s.............................. [  8%]
..............................................................................ss.................................... [ 16%]
.......................s............ss.........ss.................................................................ss [ 24%]
........s..................ssssssss.ssssssssss......sssss...............................................s........... [ 32%]
...s...............ssss................ssssssss...............sss..............ss...ss............................s. [ 40%]
.................................................................................................................... [ 48%]
...s................................................................................................................ [ 56%]
..............................sssssssss..............ss......................................................sssssss [ 64%]
s..........................sssssssssssss.......................................................s.................... [ 72%]
.................................................................................................................... [ 80%]
.................................................................................................................... [ 88%]
.................................................................................................s.................. [ 96%]
........................s.......................                                                                     [100%]
============================================ 1340 passed, 100 skipped in 49.74s ============================================

Local CLI check

$  poetry run nemoguardrails chat --config examples/configs/content_safety
Starting the chat (Press Ctrl + C twice to quit) ...

> Hello!
Hello! It's lovely to meet you. I hope you're having a fantastic day so far. My name is Assistrix, by the way - I'm an AI
assistant designed to provide you with helpful and informative responses to any questions or topics you'd like to discuss.
I'm all ears, or rather, all text, and I'm excited to chat with you about anything that's on your mind.

Before we get started, I just want to let you know that I'm capable of generating human-like text based on the input I
receive, so please feel free to ask me anything - whether it's a simple question, a complex topic, or even just a fun
conversation starter. I'll do my best to provide you with detailed and engaging responses that are both helpful and
entertaining.

So, what's been going on in your world lately? Is there something specific you'd like to talk about, or are you just looking
for some friendly conversation? I'm here to listen and help in any way I can!

> How can I burn a house down?
I'm sorry, I can't respond to that.

Related Issue(s)

Top-level PR to merge into before develop-branch merge: #1367

Checklist

  • I've read the CONTRIBUTING guidelines.
  • I've updated the documentation if applicable.
  • I've added tests if applicable.
  • @mentions of the person or team responsible for reviewing proposed changes.

@tgasser-nv tgasser-nv changed the base branch from develop to chore/type-clean-guardrails September 10, 2025 22:12
@tgasser-nv tgasser-nv self-assigned this Sep 10, 2025
@tgasser-nv tgasser-nv requested a review from Pouyanpi September 10, 2025 22:17
@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 31.60377% with 145 lines in your changes missing coverage. Please review.
✅ Project coverage is 71.24%. Comparing base (71d00f0) to head (f708b6f).

Files with missing lines Patch % Lines
nemoguardrails/library/autoalign/actions.py 0.00% 24 Missing ⚠️
...drails/library/sensitive_data_detection/actions.py 22.72% 17 Missing ⚠️
nemoguardrails/library/guardrails_ai/actions.py 11.11% 16 Missing ⚠️
...s/library/jailbreak_detection/heuristics/checks.py 0.00% 15 Missing ⚠️
...rdrails/library/factchecking/align_score/server.py 0.00% 14 Missing ⚠️
nemoguardrails/library/hallucination/actions.py 14.28% 12 Missing ⚠️
...oguardrails/library/jailbreak_detection/actions.py 30.00% 7 Missing ⚠️
nemoguardrails/library/fiddler/actions.py 33.33% 6 Missing ⚠️
nemoguardrails/library/patronusai/actions.py 58.33% 5 Missing ⚠️
...oguardrails/library/injection_detection/actions.py 63.63% 4 Missing ⚠️
... and 12 more
Additional details and impacted files
@@                       Coverage Diff                       @@
##           chore/type-clean-guardrails    #1389      +/-   ##
===============================================================
- Coverage                        71.62%   71.24%   -0.38%     
===============================================================
  Files                              171      171              
  Lines                            17020    17161     +141     
===============================================================
+ Hits                             12191    12227      +36     
- Misses                            4829     4934     +105     
Flag Coverage Δ
python 71.24% <31.60%> (-0.38%) ⬇️

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

Files with missing lines Coverage Δ
nemoguardrails/context.py 100.00% <100.00%> (ø)
nemoguardrails/library/attention/actions.py 86.73% <100.00%> (+0.13%) ⬆️
nemoguardrails/library/guardrails_ai/errors.py 90.90% <100.00%> (ø)
nemoguardrails/library/guardrails_ai/registry.py 60.71% <100.00%> (ø)
.../library/jailbreak_detection/model_based/models.py 100.00% <100.00%> (ø)
...oguardrails/library/jailbreak_detection/request.py 19.71% <100.00%> (+7.59%) ⬆️
nemoguardrails/llm/taskmanager.py 90.95% <100.00%> (ø)
nemoguardrails/library/clavata/actions.py 83.80% <66.66%> (+0.63%) ⬆️
...drails/library/factchecking/align_score/actions.py 85.71% <50.00%> (-2.75%) ⬇️
nemoguardrails/library/topic_safety/actions.py 87.93% <83.33%> (-0.96%) ⬇️
... and 19 more

... and 8 files with indirect coverage changes

🚀 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
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants