langflow
langflow copied to clipboard
Messages Stored With Same Timestamp
Bug Description
Every message is stored with the same timestamp. Note the actual time when I will send some of them.
Reproduction
get the memory starter example interact with chat a couple of times go to default session and check the timestamp
Expected behavior
The timestamp should match the time the user is interacting.
Who can help?
@anovazzi1
Operating System
Mac
Langflow Version
1.0.12
Python Version
3.12
Screenshot
Flow File
same issue in the latest version 1.3.4. Can anyone please help me out?
I modified two files to fix this issue:
- Added timestamp_to_iso_format to langflow/schema/validators.py Added a new function to properly convert various timestamp formats to ISO 8601 format with millisecond precision:
def timestamp_to_iso_format(timestamp: datetime | str) -> str:
"""Convert timestamp to ISO 8601 format for database compatibility.
Returns in format: YYYY-MM-DDTHH:MM:SS.mmm+00:00
"""
if isinstance(timestamp, str):
# Try to parse the string to a datetime
for fmt in [
"%Y-%m-%d %H:%M:%S.%f %Z",
"%Y-%m-%d %H:%M:%S %Z",
"%Y-%m-%d %H:%M:%S.%f",
"%Y-%m-%d %H:%M:%S",
]:
try:
dt = datetime.strptime(timestamp.strip(), fmt)
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
return dt.isoformat()
except ValueError:
continue
# If it's already in ISO format, return as is
try:
datetime.fromisoformat(timestamp)
return timestamp
except ValueError:
pass
msg = f"Invalid timestamp format: {timestamp}"
raise ValueError(msg)
# Handle datetime object
if timestamp.tzinfo is None:
timestamp = timestamp.replace(tzinfo=timezone.utc)
return timestamp.isoformat()
# Added validator
timestamp_to_iso_format_validator = BeforeValidator(timestamp_to_iso_format)
- Updated Message class in langflow/schema/message.py Modified the Message class to use the ISO format for timestamps and to handle both string and datetime inputs:
Changed the timestamp field definition:
pythontimestamp: str = Field(
default_factory=lambda: timestamp_to_iso_format(datetime.now(timezone.utc))
)
Updated the timestamp serializer:
python@field_serializer("timestamp")
def serialize_timestamp(self, value):
# Return ISO format for database compatibility
return timestamp_to_iso_format(value)
Updated the create method to handle datetime objects: python@classmethod
async def create(cls, **kwargs):
"""If files are present, create the message in a separate thread as is_image_file is blocking."""
# Convert timestamp to ISO format if it's a datetime object
if "timestamp" in kwargs and isinstance(kwargs["timestamp"], datetime):
kwargs["timestamp"] = timestamp_to_iso_format(kwargs["timestamp"])
if "files" in kwargs:
return await asyncio.to_thread(cls, **kwargs)
return cls(**kwargs)
Also updated MessageResponse class to use the new format.
@ogabrielluiz