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 astr
or adict
. Assigning it directly tobot_message
created an inconsistent type, which could cause errors in downstream processing or when serializing the final response.bot_message
is always adict
, creating a consistent data type for the rest of the function.bot_message_content
is not astr
, it must be adict
that 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_content
with 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
None
values. They are generally safe but represent a stricter enforcement of types.Type: Making API Model Fields Optional
nemoguardrails/server/api.py
, Lines 189 & 235messages
field inRequestBody
andResponseBody
was required (List[dict]
), but in practice, it might be omitted. This could lead to validation errors.Optional[List[dict]]
.messages
field to beNone
. To handle this, thechat_completion
function was updated to default to an empty list ifbody.messages
isNone
(messages = body.messages or []
), preventing errors downstream.RequestBody
would be to usedefault_factory=list
, which would always ensure an empty list is present if the field is omitted. The chosen approach of usingOptional
is also a standard and valid pattern.Type: Enforcing Consistent Response Model
nemoguardrails/server/api.py
chat_completion
endpoint returned raw dictionaries (dict
), which lacked schema enforcement and could lead to inconsistent responses.ResponseBody
model.ResponseBody
, the API response is now validated against a defined schema, improving reliability and self-documentation.ResponseBody
model.Type: Adding Explicit
None
Checks and New Error Pathsnemoguardrails/server/api.py
, Lines 333 & 371full_llm_rails_config
andconfig_ids
could potentially beNone
at runtime, leading toAttributeError
orTypeError
in 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.py
registered_loggers
andllm_rails_instances
, were untyped, reducing code clarity and preventing effective static analysis.Type: Correcting
staticmethod
Usagenemoguardrails/server/api.py
, Line 511on_any_event
was incorrectly marked as a@staticmethod
. The parent classFileSystemEventHandler
expects an instance method, which receivesself
as the first argument.@staticmethod
decorator was removed.Type: Enabling Type Checking for the
server
Modulepyproject.toml
, Line 159nemoguardrails/server/
directory was not included in thepyright
configuration, so type errors in this part of the codebase were not being detected.include
list 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..................................................................Passed
Unit-tests
Local CLI check
Checklist