Skip to content

[BUG] ChatMessage.role column uses plain String(20) with no constraint — any value can be stored #752

Description

@vipul674

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

  1. Insert a ChatMessage with role "system" or "invalid" directly into the database
  2. The database accepts the value without error
  3. 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

  • Yes, I am participating in GirlScript Summer of Code and would like to fix this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    gssocGirlScript Summer of Code 2026 issue/PR

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions