Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
fix: Fix leaky opens (#2924)
Matthew Zhou5511a0801715
diff --git a/.github/scripts/model-sweep/model_sweep.py b/.github/scripts/model-sweep/model_sweep.pyindex 322b427b4..97a19306e 100644--- a/.github/scripts/model-sweep/model_sweep.py+++ b/.github/scripts/model-sweep/model_sweep.py@@ -36,7 +36,8 @@ from letta.schemas.llm_config import LLMConfigdef get_llm_config(filename: str, llm_config_dir: str = "tests/configs/llm_model_configs") -> LLMConfig:filename = os.path.join(llm_config_dir, filename)- config_data = json.load(open(filename, "r"))+ with open(filename, "r") as f:+ config_data = json.load(f)llm_config = LLMConfig(**config_data)return llm_configdiff --git a/examples/files/main.py b/examples/files/main.pyindex 98c29c551..5560c0718 100644--- a/examples/files/main.py+++ b/examples/files/main.py@@ -66,14 +66,13 @@ except Exception as e:# 1. From an existing file# "rb" means "read binary"-file = open("example-on-disk.txt", "rb")--# Upload the file to the folder-file = client.folders.files.upload(- folder_id=folder_id,- file=file,- duplicate_handling="skip"-)+with open("example-on-disk.txt", "rb") as f:+ # Upload the file to the folder+ file = client.folders.files.upload(+ folder_id=folder_id,+ file=f,+ duplicate_handling="skip"+ )# 2. From a string by encoding it into a base64 stringimport io@@ -111,11 +110,12 @@ if not os.path.exists("memgpt.pdf"):f.write(response.content)# Upload the PDF to the folder-file = client.folders.files.upload(- folder_id=folder_id,- file=open("memgpt.pdf", "rb"),- duplicate_handling="skip"-)+with open("memgpt.pdf", "rb") as f:+ file = client.folders.files.upload(+ folder_id=folder_id,+ file=f,+ duplicate_handling="skip"+ )## Now we need to create an agent that can use this folderdiff --git a/examples/sleeptime/sleeptime_source_example.py b/examples/sleeptime/sleeptime_source_example.pyindex c782060e2..35b12ed9e 100644--- a/examples/sleeptime/sleeptime_source_example.py+++ b/examples/sleeptime/sleeptime_source_example.py@@ -39,10 +39,11 @@ client.agents.sources.attach()# upload a file: this will trigger processing-job = client.sources.files.upload(- file=open("handbook.pdf", "rb"),- source_id=source.id-)+with open("handbook.pdf", "rb") as f:+ job = client.sources.files.upload(+ file=f,+ source_id=source.id+ )time.sleep(2)diff --git a/fern/examples/data_sources.py b/fern/examples/data_sources.pyindex 49c8b519f..dc8b61f82 100644--- a/fern/examples/data_sources.py+++ b/fern/examples/data_sources.py@@ -25,7 +25,8 @@ with open("dummy.txt", "w") as f:f.write("Remember that the user is a redhead")# upload a file into the source-job = client.sources.files.upload(source_id=source.id, file=open("dummy.txt", "rb"))+with open("dummy.txt", "rb") as f:+ job = client.sources.files.upload(source_id=source.id, file=f)# wait until the job is completedwhile True:diff --git a/letta/utils.py b/letta/utils.pyindex 3723c33c7..0640c9dbf 100644--- a/letta/utils.py+++ b/letta/utils.py@@ -941,7 +941,8 @@ def get_human_text(name: str, enforce_limit=True):for file_path in list_human_files():file = os.path.basename(file_path)if f"{name}.txt" == file or name == file:- human_text = open(file_path, encoding="utf-8").read().strip()+ with open(file_path, encoding="utf-8") as f:+ human_text = f.read().strip()if enforce_limit and len(human_text) > CORE_MEMORY_HUMAN_CHAR_LIMIT:raise ValueError(f"Contents of {name}.txt is over the character limit ({len(human_text)} > {CORE_MEMORY_HUMAN_CHAR_LIMIT})")return human_text@@ -953,7 +954,8 @@ def get_persona_text(name: str, enforce_limit=True):for file_path in list_persona_files():file = os.path.basename(file_path)if f"{name}.txt" == file or name == file:- persona_text = open(file_path, encoding="utf-8").read().strip()+ with open(file_path, encoding="utf-8") as f:+ persona_text = f.read().strip()if enforce_limit and len(persona_text) > CORE_MEMORY_PERSONA_CHAR_LIMIT:raise ValueError(f"Contents of {name}.txt is over the character limit ({len(persona_text)} > {CORE_MEMORY_PERSONA_CHAR_LIMIT})"diff --git a/paper_experiments/doc_qa_task/doc_qa.py b/paper_experiments/doc_qa_task/doc_qa.pyindex a999de11d..aaf39ccf8 100644--- a/paper_experiments/doc_qa_task/doc_qa.py+++ b/paper_experiments/doc_qa_task/doc_qa.py@@ -256,7 +256,8 @@ def run_docqa_task(print("Results file:", filename)if os.path.exists(filename):- all_response_data = json.load(open(filename, "r"))+ with open(filename, "r") as f:+ all_response_data = json.load(f)else:all_response_data = []diff --git a/paper_experiments/doc_qa_task/llm_judge_doc_qa.py b/paper_experiments/doc_qa_task/llm_judge_doc_qa.pyindex c7d1d385d..5041d6562 100644--- a/paper_experiments/doc_qa_task/llm_judge_doc_qa.py+++ b/paper_experiments/doc_qa_task/llm_judge_doc_qa.py@@ -86,7 +86,8 @@ if __name__ == "__main__":args = parser.parse_args()# load data- data = json.load(open(args.file))+ with open(args.file, "r") as f:+ data = json.load(f)# counterscorrect = 0@@ -149,9 +150,10 @@ if __name__ == "__main__":total += 1# Dump aggregated results- json.dump(- {"accuracy": correct / total, "total": total, "results": results},- open(f"results_{model}_{num_docs}_{baseline}.json", "w"),- indent=4,- )+ with open(f"results_{model}_{num_docs}_{baseline}.json", "w") as f:+ json.dump(+ {"accuracy": correct / total, "total": total, "results": results},+ f,+ indent=4,+ )print(correct / total)diff --git a/tests/helpers/endpoints_helper.py b/tests/helpers/endpoints_helper.pyindex bef12971a..41ea70f21 100644--- a/tests/helpers/endpoints_helper.py+++ b/tests/helpers/endpoints_helper.py@@ -50,9 +50,11 @@ def setup_agent(include_base_tools: bool = True,include_base_tool_rules: bool = True,) -> AgentState:- config_data = json.load(open(filename, "r"))+ with open(filename, "r") as f:+ config_data = json.load(f)llm_config = LLMConfig(**config_data)- embedding_config = EmbeddingConfig(**json.load(open(EMBEDDING_CONFIG_PATH)))+ with open(EMBEDDING_CONFIG_PATH, "r") as f:+ embedding_config = EmbeddingConfig(**json.load(f))# setup configconfig = LettaConfig()@@ -93,7 +95,8 @@ def setup_agent(async def run_embedding_endpoint(filename, actor=None):# load JSON file- config_data = json.load(open(filename, "r"))+ with open(filename, "r") as f:+ config_data = json.load(f)print(config_data)embedding_config = EmbeddingConfig(**config_data)diff --git a/tests/integration_test_builtin_tools.py b/tests/integration_test_builtin_tools.pyindex cb072cacc..3885cda5c 100644--- a/tests/integration_test_builtin_tools.py+++ b/tests/integration_test_builtin_tools.py@@ -97,7 +97,8 @@ def agent_state(client: Letta) -> AgentState:def get_llm_config(filename: str, llm_config_dir: str = "tests/configs/llm_model_configs") -> LLMConfig:filename = os.path.join(llm_config_dir, filename)- config_data = json.load(open(filename, "r"))+ with open(filename, "r") as f:+ config_data = json.load(f)llm_config = LLMConfig(**config_data)return llm_configdiff --git a/tests/integration_test_send_message.py b/tests/integration_test_send_message.pyindex 87a1098a7..e96f46c2c 100644--- a/tests/integration_test_send_message.py+++ b/tests/integration_test_send_message.py@@ -47,7 +47,8 @@ logger = get_logger(__name__)def get_llm_config(filename: str, llm_config_dir: str = "tests/configs/llm_model_configs") -> LLMConfig:filename = os.path.join(llm_config_dir, filename)- config_data = json.load(open(filename, "r"))+ with open(filename, "r") as f:+ config_data = json.load(f)llm_config = LLMConfig(**config_data)return llm_configdiff --git a/tests/integration_test_summarizer.py b/tests/integration_test_summarizer.pyindex 2f0965c92..276985f99 100644--- a/tests/integration_test_summarizer.py+++ b/tests/integration_test_summarizer.py@@ -269,9 +269,11 @@ def test_summarizer(config_filename, server, default_user):agent_name = str(uuid.uuid5(namespace, f"integration-test-summarizer-{config_filename}"))# Load configs- config_data = json.load(open(os.path.join(LLM_CONFIG_DIR, config_filename)))+ with open(os.path.join(LLM_CONFIG_DIR, config_filename), "r") as f:+ config_data = json.load(f)llm_config = LLMConfig(**config_data)- embedding_config = EmbeddingConfig(**json.load(open(EMBEDDING_CONFIG_PATH)))+ with open(EMBEDDING_CONFIG_PATH, "r") as f:+ embedding_config = EmbeddingConfig(**json.load(f))# Ensure cleanupcleanup(server=server, agent_uuid=agent_name, actor=default_user)diff --git a/tests/test_embeddings.py b/tests/test_embeddings.pyindex a4c13791f..06689bab8 100644--- a/tests/test_embeddings.py+++ b/tests/test_embeddings.py@@ -18,9 +18,11 @@ included_files = []config_dir = "tests/configs/embedding_model_configs"config_files = glob.glob(os.path.join(config_dir, "*.json"))-embedding_configs = [- EmbeddingConfig(**json.load(open(config_file))) for config_file in config_files if config_file.split("/")[-1] in included_files-]+embedding_configs = []+for config_file in config_files:+ if config_file.split("/")[-1] in included_files:+ with open(config_file, "r") as f:+ embedding_configs.append(EmbeddingConfig(**json.load(f)))@pytest.fixture