Skip to content

Timestamp collision in ChatMessage #449

@austinmyc

Description

@austinmyc

In message_history.schema.ChatMessage, the id is being generated with the session tag and the current timestamp.

@model_validator(mode="before")
    @classmethod
    def generate_id(cls, values):
        if TIMESTAMP_FIELD_NAME not in values:
            values[TIMESTAMP_FIELD_NAME] = current_timestamp()
        if ID_FIELD_NAME not in values:
            values[ID_FIELD_NAME] = (
                f"{values[SESSION_FIELD_NAME]}:{values[TIMESTAMP_FIELD_NAME]}"
            )
        return values

This means that during many use cases, like add_messages(messages) or store(prompt, response) that uses add_messages fail to insert the full conversation to the database, as messages might end up having the same id, and newest message would overwrite the older messages.

A simple fix might be adding a counter or a hash for the id:

values[ID_FIELD_NAME] = f"{session_tag}:{timestamp}:{uuid4().hex[:8]}"

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions