chore(types): Type-clean llm/ (27 errors) #1394
Open
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.
Description
Cleaned the integrations/ directory with help from Cursor/Claude 4 Sonnet to get the low-hanging items.
Overview
This document summarizes all Python type error fixes implemented on the
chore/type-clean-library
branch. The fixes systematically resolved 145+ Pyright type errors across 29 files while maintaining 100% test compatibility (1,340+ tests passing).🔴 HIGH RISK CHANGES
These changes affect core infrastructure and could potentially impact system behavior if not handled correctly.
Core Context Management
File:
nemoguardrails/context.py
Line 28: Added explicit type annotation to global context variable
Risk: High - affects global context variable used throughout the application for LLM call tracking
Impact: Enables proper type checking for LLM call information across all modules
Task Manager API Changes
File:
nemoguardrails/llm/taskmanager.py
Lines 427, 474: Updated method signatures to accept both string and Task enum
Risk: High - core API change affecting task processing throughout the system
Impact: Maintains backwards compatibility while enabling type safety for task operations
Dynamic Attribute Assignment
File:
nemoguardrails/library/attention/actions.py
Line 121: Used
setattr()
for dynamic attribute assignmentLines 101-102: Added proper type annotations for container attributes
Risk: High - dynamic attribute assignment on core event objects
Impact: Enables timestamp correction on action events without breaking the data model
🟡 MEDIUM RISK CHANGES
These changes affect business logic and external integrations but have controlled impact.
Optional Dependency Handling
Files: Multiple library modules
Pattern: Wrapped optional imports with try/except and
# type: ignore
Affected Files:
nemoguardrails/library/sensitive_data_detection/actions.py
(Lines 19-28)nemoguardrails/library/factchecking/align_score/server.py
(Lines 20-28)nemoguardrails/library/jailbreak_detection/heuristics/checks.py
(Lines 18-31)nemoguardrails/library/hallucination/actions.py
(Lines 19-26)nemoguardrails/library/cleanlab/actions.py
(Line 50)nemoguardrails/library/gcp_moderate_text/actions.py
(Lines 20, 119)nemoguardrails/library/guardrails_ai/errors.py
(Line 17)Risk: Medium - affects availability of optional features when dependencies are missing
Impact: Graceful degradation when optional dependencies are unavailable
Context and Configuration Validation
Files: Multiple action files
Pattern: Added explicit null checks before accessing context or configuration
Affected Files:
nemoguardrails/library/autoalign/actions.py
(Lines 291-305)nemoguardrails/library/factchecking/align_score/actions.py
(Lines 55-56)nemoguardrails/library/fiddler/actions.py
(Lines 96, 124, 154)nemoguardrails/library/llama_guard/actions.py
(Lines 67, 112)nemoguardrails/library/patronusai/actions.py
(Lines 86, 256)nemoguardrails/library/self_check/facts/actions.py
(Lines 53-54)nemoguardrails/library/self_check/input_check/actions.py
(Line 52)nemoguardrails/library/self_check/output_check/actions.py
(Lines 54-55)Risk: Medium - changes error handling behavior for missing context
Impact: More explicit error messages when required context is missing
LLM Instance Validation
Files: Multiple self-check and guard modules
Pattern: Added null checks for LLM instances before usage
Affected Files:
nemoguardrails/library/self_check/facts/actions.py
(Lines 78-79)nemoguardrails/library/self_check/input_check/actions.py
(Lines 73-74)nemoguardrails/library/self_check/output_check/actions.py
(Lines 77-78)nemoguardrails/library/llama_guard/actions.py
(Lines 69, 114)nemoguardrails/library/patronusai/actions.py
(Lines 88, 258)Risk: Medium - affects behavior when LLM models are unavailable
Impact: Graceful fallback when LLM instances are not properly configured
Configuration Access Validation
Files: Multiple library modules
Pattern: Added null checks before accessing nested configuration attributes
Affected Files:
nemoguardrails/library/autoalign/actions.py
(Lines 176-181)nemoguardrails/library/guardrails_ai/actions.py
(Lines 117-123)nemoguardrails/library/injection_detection/actions.py
(Lines 133-135)nemoguardrails/library/jailbreak_detection/actions.py
(Lines 56-58)nemoguardrails/library/patronusai/actions.py
(Lines 268-270)nemoguardrails/library/sensitive_data_detection/actions.py
(Lines 119, 131)Risk: Medium - affects feature availability when configurations are incomplete
Impact: Prevents crashes when optional configurations are missing
ML Framework Integration
Files: Machine learning related modules
Pattern: Added TYPE_CHECKING blocks and runtime null checks for ML dependencies
Affected Files:
nemoguardrails/library/jailbreak_detection/heuristics/checks.py
(Lines 18-31, 41)nemoguardrails/library/jailbreak_detection/model_based/checks.py
(Lines 22-28)nemoguardrails/library/jailbreak_detection/model_based/models.py
(Lines 23-24)nemoguardrails/library/injection_detection/actions.py
(Lines 37-44)Risk: Medium - affects ML-based security features when frameworks are unavailable
Impact: Graceful handling when PyTorch, Transformers, or YARA are not installed
🟢 LOW RISK CHANGES
These changes are primarily cosmetic or defensive programming improvements with minimal functional impact.
Type Annotation Corrections
Files: Various files
Pattern: Fixed return type annotations to match actual function behavior
Affected Files:
nemoguardrails/library/injection_detection/actions.py
(Line 118)Risk: Low - purely cosmetic type annotation fixes
Impact: Improved type checking accuracy with no runtime changes
Variable Initialization
Files: Various files
Pattern: Added explicit variable initialization to prevent unbound variable errors
Affected Files:
nemoguardrails/library/jailbreak_detection/actions.py
(Line 134)nemoguardrails/library/topic_safety/actions.py
(Line 43)nemoguardrails/library/clavata/actions.py
(Line 164)Risk: Low - defensive programming to ensure variables are always defined
Impact: Prevents potential runtime errors in edge cases
Enum Value Access Fixes
Files: Configuration modules
Pattern: Fixed enum value access using proper string conversion
Affected Files:
nemoguardrails/library/injection_detection/yara_config.py
(Lines 44, 50, 61)Risk: Low - fixes enum string representation
Impact: Consistent enum value handling
HTTP Client Parameter Fixes
Files: Request handling modules
Pattern: Used proper timeout objects instead of raw integers
Affected Files:
nemoguardrails/library/jailbreak_detection/request.py
(Line 119)Risk: Low - fixes HTTP client parameter types
Impact: Proper timeout handling in HTTP requests
Default Value Handling
Files: Server modules
Pattern: Added default values for optional parameters using
or
operatorAffected Files:
nemoguardrails/library/jailbreak_detection/server.py
(Lines 83, 90, 98, 101)Risk: Low - provides sensible defaults for optional parameters
Impact: Prevents type errors when optional parameters are None
Event Type Handling
Files: Safety monitoring modules
Pattern: Added type checking for mixed event object types
Affected Files:
nemoguardrails/library/topic_safety/actions.py
(Lines 53-60)Risk: Low - handles mixed event types more robustly
Impact: Better handling of different event object formats
Import Statement Corrections
Files: Registry modules
Pattern: Fixed placement of
# type: ignore
commentsAffected Files:
nemoguardrails/library/guardrails_ai/registry.py
(Line 107)Risk: Low - cosmetic fix for type ignore placement
Impact: Proper suppression of import warnings
Summary Statistics
Risk Distribution
File Impact
nemoguardrails/library/
context.py
,taskmanager.py
)Validation Results
Key Patterns Applied
Test Plan
Type-checking
Unit-tests
Local CLI check
Related Issue(s)
Top-level PR to merge into before develop-branch merge: #1367
Checklist