From ff22d576ad680750e6fc636998adad4b0bec65ec Mon Sep 17 00:00:00 2001 From: Charles Packer Date: Mon, 14 Apr 2025 12:55:34 -0700 Subject: [PATCH] fix: patch .utcnow warning (#1702) --- .../personal_assistant_demo/google_calendar_test_setup.py | 2 +- letta/groups/sleeptime_multi_agent.py | 6 +++--- letta/orm/base.py | 4 ++-- letta/server/rest_api/routers/v1/agents.py | 6 +++--- letta/services/passage_manager.py | 4 ++-- tests/integration_test_summarizer.py | 4 ++-- tests/test_llm_clients.py | 8 +++++--- tests/test_managers.py | 4 ++-- tests/test_static_buffer_summarize.py | 4 ++-- tests/test_v1_routes.py | 4 ++-- tests/utils.py | 5 ++--- 11 files changed, 26 insertions(+), 25 deletions(-) diff --git a/examples/personal_assistant_demo/google_calendar_test_setup.py b/examples/personal_assistant_demo/google_calendar_test_setup.py index a24f2b6d..820feaa4 100644 --- a/examples/personal_assistant_demo/google_calendar_test_setup.py +++ b/examples/personal_assistant_demo/google_calendar_test_setup.py @@ -40,7 +40,7 @@ def main(): service = build("calendar", "v3", credentials=creds) # Call the Calendar API - now = datetime.datetime.utcnow().isoformat() + "Z" # 'Z' indicates UTC time + now = datetime.datetime.now(datetime.timezone.utc).isoformat() + "Z" # 'Z' indicates UTC time print("Getting the upcoming 10 events") events_result = ( service.events() diff --git a/letta/groups/sleeptime_multi_agent.py b/letta/groups/sleeptime_multi_agent.py index 336591f1..0a6546c5 100644 --- a/letta/groups/sleeptime_multi_agent.py +++ b/letta/groups/sleeptime_multi_agent.py @@ -1,6 +1,6 @@ import asyncio import threading -from datetime import datetime +from datetime import datetime, timezone from typing import List, Optional from letta.agent import Agent, AgentState @@ -154,7 +154,7 @@ class SleeptimeMultiAgent(Agent): ) job_update = JobUpdate( status=JobStatus.completed, - completed_at=datetime.utcnow(), + completed_at=datetime.now(timezone.utc), metadata={ "result": result.model_dump(mode="json"), "agent_id": participant_agent.agent_state.id, @@ -165,7 +165,7 @@ class SleeptimeMultiAgent(Agent): except Exception as e: job_update = JobUpdate( status=JobStatus.failed, - completed_at=datetime.utcnow(), + completed_at=datetime.now(timezone.utc), metadata={"error": str(e)}, ) self.job_manager.update_job_by_id(job_id=run_id, job_update=job_update, actor=self.user) diff --git a/letta/orm/base.py b/letta/orm/base.py index 7f982399..bf24707d 100644 --- a/letta/orm/base.py +++ b/letta/orm/base.py @@ -1,4 +1,4 @@ -from datetime import datetime +from datetime import datetime, timezone from typing import Optional from sqlalchemy import Boolean, DateTime, String, func, text @@ -25,7 +25,7 @@ class CommonSqlalchemyMetaMixins(Base): timestamp (Optional[datetime]): The timestamp to set. If None, uses the current UTC time. """ - self.updated_at = timestamp or datetime.utcnow() + self.updated_at = timestamp or datetime.now(timezone.utc) def _set_created_and_updated_by_fields(self, actor_id: str) -> None: """Populate created_by_id and last_updated_by_id based on actor.""" diff --git a/letta/server/rest_api/routers/v1/agents.py b/letta/server/rest_api/routers/v1/agents.py index c0acdccf..35dcd703 100644 --- a/letta/server/rest_api/routers/v1/agents.py +++ b/letta/server/rest_api/routers/v1/agents.py @@ -1,6 +1,6 @@ import json import traceback -from datetime import datetime +from datetime import datetime, timezone from typing import Annotated, Any, List, Optional from fastapi import APIRouter, BackgroundTasks, Body, Depends, File, Header, HTTPException, Query, UploadFile, status @@ -729,7 +729,7 @@ async def process_message_background( # Update job status to completed job_update = JobUpdate( status=JobStatus.completed, - completed_at=datetime.utcnow(), + completed_at=datetime.now(timezone.utc), metadata={"result": result.model_dump(mode="json")}, # Store the result in metadata ) server.job_manager.update_job_by_id(job_id=job_id, job_update=job_update, actor=actor) @@ -738,7 +738,7 @@ async def process_message_background( # Update job status to failed job_update = JobUpdate( status=JobStatus.failed, - completed_at=datetime.utcnow(), + completed_at=datetime.now(timezone.utc), metadata={"error": str(e)}, ) server.job_manager.update_job_by_id(job_id=job_id, job_update=job_update, actor=actor) diff --git a/letta/services/passage_manager.py b/letta/services/passage_manager.py index 8c02655b..6dc25e1a 100644 --- a/letta/services/passage_manager.py +++ b/letta/services/passage_manager.py @@ -1,4 +1,4 @@ -from datetime import datetime +from datetime import datetime, timezone from typing import List, Optional from openai import OpenAI @@ -49,7 +49,7 @@ class PassageManager: "organization_id": data["organization_id"], "metadata_": data.get("metadata", {}), "is_deleted": data.get("is_deleted", False), - "created_at": data.get("created_at", datetime.utcnow()), + "created_at": data.get("created_at", datetime.now(timezone.utc)), } if "agent_id" in data and data["agent_id"]: diff --git a/tests/integration_test_summarizer.py b/tests/integration_test_summarizer.py index 00e92733..43a51e16 100644 --- a/tests/integration_test_summarizer.py +++ b/tests/integration_test_summarizer.py @@ -1,7 +1,7 @@ import json import os import uuid -from datetime import datetime +from datetime import datetime, timezone from typing import List import pytest @@ -55,7 +55,7 @@ def generate_message(role: str, text: str = None, tool_calls: List = None) -> Me id="message-" + str(uuid.uuid4()), role=MessageRole(role), content=[TextContent(text=text or f"{role} message text")], - created_at=datetime.utcnow(), + created_at=datetime.now(timezone.utc), tool_calls=tool_calls or [], ) diff --git a/tests/test_llm_clients.py b/tests/test_llm_clients.py index b5987b15..c33468df 100644 --- a/tests/test_llm_clients.py +++ b/tests/test_llm_clients.py @@ -1,4 +1,4 @@ -from datetime import datetime +from datetime import datetime, timezone from unittest.mock import AsyncMock, patch import pytest @@ -35,10 +35,12 @@ def mock_agent_messages(): return { "agent-1": [ PydanticMessage( - role=MessageRole.system, content=[{"type": "text", "text": "You are a helpful assistant."}], created_at=datetime.utcnow() + role=MessageRole.system, + content=[{"type": "text", "text": "You are a helpful assistant."}], + created_at=datetime.now(timezone.utc), ), PydanticMessage( - role=MessageRole.user, content=[{"type": "text", "text": "What's the weather like?"}], created_at=datetime.utcnow() + role=MessageRole.user, content=[{"type": "text", "text": "What's the weather like?"}], created_at=datetime.now(timezone.utc) ), ] } diff --git a/tests/test_managers.py b/tests/test_managers.py index 0133bad2..1734a1e9 100644 --- a/tests/test_managers.py +++ b/tests/test_managers.py @@ -1768,7 +1768,7 @@ def test_agent_list_passages_filtering(server, default_user, sarah_agent, defaul assert len(source_filtered) == 3 # Test date filtering - now = datetime.utcnow() + now = datetime.now(timezone.utc) future_date = now + timedelta(days=1) past_date = now - timedelta(days=1) @@ -4302,7 +4302,7 @@ def test_job_messages_pagination(server: SyncServer, default_run, default_user, def test_job_messages_ordering(server: SyncServer, default_run, default_user, sarah_agent): """Test that messages are ordered by created_at.""" # Create messages with different timestamps - base_time = datetime.utcnow() + base_time = datetime.now(timezone.utc) message_times = [ base_time - timedelta(minutes=2), base_time - timedelta(minutes=1), diff --git a/tests/test_static_buffer_summarize.py b/tests/test_static_buffer_summarize.py index 9feb4137..d50c4699 100644 --- a/tests/test_static_buffer_summarize.py +++ b/tests/test_static_buffer_summarize.py @@ -1,5 +1,5 @@ import json -from datetime import datetime +from datetime import datetime, timezone from unittest.mock import AsyncMock import pytest @@ -31,7 +31,7 @@ def messages(): Message( role=MessageRole.user if i % 2 == 0 else MessageRole.assistant, content=[TextContent(type="text", text=json.dumps({"message": f"Test message {i}"}))], - created_at=datetime.utcnow(), + created_at=datetime.now(timezone.utc), ) for i in range(15) ] diff --git a/tests/test_v1_routes.py b/tests/test_v1_routes.py index 819c0683..d79741a2 100644 --- a/tests/test_v1_routes.py +++ b/tests/test_v1_routes.py @@ -1,4 +1,4 @@ -from datetime import datetime +from datetime import datetime, timezone from unittest.mock import MagicMock, Mock import pytest @@ -209,7 +209,7 @@ def test_upsert_base_tools(client, mock_sync_server, add_integers_tool): def test_get_run_messages(client, mock_sync_server): """Test getting messages for a run.""" # Create properly formatted mock messages - current_time = datetime.utcnow() + current_time = datetime.now(timezone.utc) mock_messages = [ UserMessage( id=f"message-{i:08x}", diff --git a/tests/utils.py b/tests/utils.py index 6b27d804..7ca72d5b 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,7 +1,6 @@ -import datetime import os import time -from datetime import datetime +from datetime import datetime, timezone from importlib import util from typing import Dict, Iterator, List, Tuple @@ -34,7 +33,7 @@ class DummyDataConnector(DataConnector): file_size=0, # Set to 0 as a placeholder file_creation_date="1970-01-01", # Placeholder date file_last_modified_date="1970-01-01", # Placeholder date - created_at=datetime.utcnow(), + created_at=datetime.now(timezone.utc), ) self.file_to_text[file_metadata.id] = text