Description of the Bug
In backend/app/models.py, the ChatMessage.role field is defined as a plain String column:
role: Mapped[str] = mapped_column(String(20))
There is no constraint limiting the values to the two valid roles: "user" and "assistant". Any string value can be inserted into this column.
This is a data integrity issue because:
- A bug in the chat code could write
"system", "agent", or any other value
- Misspelled values like
"assitance" or "userr" would be silently accepted
- Frontend code that checks
message.role === "user" or message.role === "assistant" could miss messages with invalid roles
- There is no database-level or application-level validation
Compare with the User.role field which properly uses a SQLAlchemyEnum type with UserRole enum.
Steps to Reproduce
- Insert a ChatMessage with role "system" or "invalid" directly into the database
- The database accepts the value without error
- Frontend code filtering by role may behave unexpectedly
Expected Behavior
The role column should use an Enum type similar to User.role:
class ChatRole(str, enum.Enum):
user = "user"
assistant = "assistant"
role: Mapped[ChatRole] = mapped_column(SQLAlchemyEnum(ChatRole))
Affected File
backend/app/models.py (line ~197)
Suggested Fix
Add a ChatRole enum and use SQLAlchemyEnum for the column definition. Update all code that writes messages to use the enum values.
GSSoC '26
Description of the Bug
In
backend/app/models.py, theChatMessage.rolefield is defined as a plainStringcolumn:There is no constraint limiting the values to the two valid roles:
"user"and"assistant". Any string value can be inserted into this column.This is a data integrity issue because:
"system","agent", or any other value"assitance"or"userr"would be silently acceptedmessage.role === "user"ormessage.role === "assistant"could miss messages with invalid rolesCompare with the
User.rolefield which properly uses aSQLAlchemyEnumtype withUserRoleenum.Steps to Reproduce
Expected Behavior
The
rolecolumn should use an Enum type similar toUser.role:Affected File
backend/app/models.py(line ~197)Suggested Fix
Add a
ChatRoleenum and useSQLAlchemyEnumfor the column definition. Update all code that writes messages to use the enum values.GSSoC '26