Repository navigation
Add ChatRole enum for message history role validation - #518
Merged
Merged
Conversation
Contributor
Author
|
Current implementation will break existing string-based behavior (e.g. llm), will fix this asap, don't review just yet |
Collaborator
All good - we can hold |
Collaborator
|
Hey @nabilasiregar, let us know when this PR is ready for us to take a look at again 👍 |
Contributor
Author
|
Hey @vishal-bala it's ready |
vishal-bala
self-requested a review
March 2, 2026 12:03
vishal-bala
approved these changes
Mar 2, 2026
| if role not in valid_roles: | ||
| raise ValueError( | ||
| f"Invalid role '{role}'. Valid roles are: {valid_roles}" | ||
| if role in valid_roles: |
Collaborator
There was a problem hiding this comment.
Just a slight convenience thing, but you can also just do membership checks on the enum 😄
if role in ChatRole:
...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Changes for #484
ChatRole(str, Enum)toschema.pyas the single source of truthfor valid roles
ChatMessage.roleto useChatRolewith afield_validatorthat coerces valid strings automatically
_validate_rolesinbase_history.pyto derive valid roles fromChatRoleinstead of hardcoded valuesChatRolefrom themessage_historypackage__init__.pyto_dict()Note
Medium Risk
Changes the public
ChatMessage.roletype/validation and role filtering behavior, which may affect downstream callers that assume plain strings (especially the legacyllmrole), though deprecated values are still accepted with warnings.Overview
Adds a
ChatRoleenum (user/assistant/system/tool) as the single source of truth for message roles, and updatesChatMessage.roleto coerce valid role strings into the enum while emittingDeprecationWarnings for legacy/unknown string roles.Aligns
BaseMessageHistory._validate_rolesto derive allowed roles fromChatRole(warning-but-allow forllm), exportsChatRolefrommessage_history.__init__, and expands unit tests to cover coercion, deprecation warnings, invalid role rejection, andto_dict()role serialization.Written by Cursor Bugbot for commit 94211c8. This will update automatically on new commits. Configure here.