chore(types): Type-clean server/ (20 errors) #1397
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
Type-cleaned the nemoguardrails/server directory to get it clean according to Pyright. Added the directory to be automatically checked by pyright in the pre-commits.
Type-cleaning
This report summarizes the type-safety fixes implemented in the pull request. The changes have been categorized by their potential risk of disrupting existing functionality.
🔴 High Risk
This change involves significant assumptions about data structures and alters runtime logic to enforce type consistency.
nemoguardrails/server/api.py, Line 451res.response[0]could be astror adict. Assigning it directly tobot_messagecreated an inconsistent type, which could cause errors in downstream processing or when serializing the final response.bot_messageis always adict, creating a consistent data type for the rest of the function.bot_message_contentis not astr, it must be adictthat already conforms to the required message structure. If the model were to return another data type (e.g., an integer), it would pass through and likely cause an error later.bot_message_contentwith validation, which would explicitly handle malformed responses instead of implicitly trusting the structure. However, the current fix is a pragmatic solution for the common cases.🟠 Medium Risk
These changes modify API contracts, introduce new failure modes, or alter control flow to handle potential
Nonevalues. They are generally safe but represent a stricter enforcement of types.Type: Making API Model Fields Optional
nemoguardrails/server/api.py, Lines 189 & 235messagesfield inRequestBodyandResponseBodywas required (List[dict]), but in practice, it might be omitted. This could lead to validation errors.Optional[List[dict]].messagesfield to beNone. To handle this, thechat_completionfunction was updated to default to an empty list ifbody.messagesisNone(messages = body.messages or []), preventing errors downstream.RequestBodywould be to usedefault_factory=list, which would always ensure an empty list is present if the field is omitted. The chosen approach of usingOptionalis also a standard and valid pattern.Type: Enforcing Consistent Response Model
nemoguardrails/server/api.pychat_completionendpoint returned raw dictionaries (dict), which lacked schema enforcement and could lead to inconsistent responses.ResponseBodymodel.ResponseBody, the API response is now validated against a defined schema, improving reliability and self-documentation.ResponseBodymodel.Type: Adding Explicit
NoneChecks and New Error Pathsnemoguardrails/server/api.py, Lines 333 & 371full_llm_rails_configandconfig_idscould potentially beNoneat runtime, leading toAttributeErrororTypeErrorin subsequent code.None.🟢 Low Risk
These changes are simple type hint additions, corrections of obvious bugs, or improvements to developer experience that have no impact on runtime logic.
Type: Adding Type Hints to Variables and Collections
nemoguardrails/server/api.py,nemoguardrails/server/datastore/redis_store.pyregistered_loggersandllm_rails_instances, were untyped, reducing code clarity and preventing effective static analysis.Type: Correcting
staticmethodUsagenemoguardrails/server/api.py, Line 511on_any_eventwas incorrectly marked as a@staticmethod. The parent classFileSystemEventHandlerexpects an instance method, which receivesselfas the first argument.@staticmethoddecorator was removed.Type: Enabling Type Checking for the
serverModulepyproject.toml, Line 159nemoguardrails/server/directory was not included in thepyrightconfiguration, so type errors in this part of the codebase were not being detected.includelist inpyproject.toml.Test Plan
Type-checking
$ poetry run pre-commit run --all-files check yaml...............................................................Passed fix end of files.........................................................Passed trim trailing whitespace.................................................Passed isort (python)...........................................................Passed black....................................................................Passed Insert license in comments...............................................Passed pyright..................................................................PassedUnit-tests
Local CLI check
Checklist