Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
fix(core): deprecate enforcing block limits (#9983)
Sarah Woodersd3f3a38fc424
diff --git a/letta/schemas/block.py b/letta/schemas/block.pyindex 48734db0d..45f584b52 100644--- a/letta/schemas/block.py+++ b/letta/schemas/block.py@@ -1,5 +1,5 @@from datetime import datetime-from typing import Any, List, Optional+from typing import List, Optionalfrom pydantic import ConfigDict, Field, field_validator, model_validator@@ -7,8 +7,6 @@ from letta.constants import CORE_MEMORY_BLOCK_CHAR_LIMIT, DEFAULT_HUMAN_BLOCK_DEfrom letta.schemas.enums import PrimitiveTypefrom letta.schemas.letta_base import LettaBase-INT32_MAX = 2147483647-# block of the LLM context@@ -50,14 +48,6 @@ class BaseBlock(LettaBase, validate_assignment=True):model_config = ConfigDict(extra="ignore") # Ignores extra fields- @field_validator("limit", mode="after")- @classmethod- def validate_limit_int32(cls, v: int) -> int:- """Ensure limit is within PostgreSQL INTEGER (int32) range."""- if v > INT32_MAX:- raise ValueError(f"limit must be <= {INT32_MAX} (int32 max), got {v}")- return v-@field_validator("value", mode="before")@classmethoddef sanitize_value_null_bytes(cls, v):@@ -66,29 +56,6 @@ class BaseBlock(LettaBase, validate_assignment=True):return v.replace("\x00", "")return v- @model_validator(mode="before")- @classmethod- def verify_char_limit(cls, data: Any) -> Any:- """Validate the character limit before model instantiation.-- Notes:- - Runs on raw input; do not mutate input.- - For update schemas (e.g., BlockUpdate), `value` and `limit` may be absent.- In that case, only validate when both are provided.- """- if isinstance(data, dict):- limit = data.get("limit")- value = data.get("value")-- # Only enforce the char limit when both are present.- # Pydantic will separately enforce required fields where applicable.- if limit is not None and value is not None and isinstance(value, str):- if len(value) > limit:- error_msg = f"Edit failed: Exceeds {limit} character limit (requested {len(value)})"- raise ValueError(error_msg)-- return data-def __setattr__(self, name, value):"""Run validation if self.value is updated"""super().__setattr__(name, value)diff --git a/letta/services/agent_manager.py b/letta/services/agent_manager.pyindex 73af54829..a4271169a 100644--- a/letta/services/agent_manager.py+++ b/letta/services/agent_manager.py@@ -73,7 +73,7 @@ from letta.schemas.tool_rule import ContinueToolRule, RequiresApprovalToolRule,from letta.schemas.user import User as PydanticUserfrom letta.server.db import db_registryfrom letta.services.archive_manager import ArchiveManager-from letta.services.block_manager import BlockManager, validate_block_limit_constraint+from letta.services.block_manager import BlockManagerfrom letta.services.context_window_calculator.context_window_calculator import ContextWindowCalculatorfrom letta.services.context_window_calculator.token_counter import create_token_counterfrom letta.services.conversation_manager import ConversationManager@@ -2072,9 +2072,6 @@ class AgentManager:update_data = block_update.model_dump(to_orm=True, exclude_unset=True, exclude_none=True)- # Validate limit constraints before updating- validate_block_limit_constraint(update_data, matched_block)-# If a custom block manager is injected (e.g. GitEnabledBlockManager), route# through it so git-backed memory semantics apply.if self.block_manager.__class__ is not BlockManager:diff --git a/letta/services/block_manager.py b/letta/services/block_manager.pyindex e0257df1b..ff62af49d 100644--- a/letta/services/block_manager.py+++ b/letta/services/block_manager.py@@ -32,62 +32,6 @@ logger = get_logger(__name__)PROMPT_AFFECTING_BLOCK_FIELDS = {"description", "label", "limit", "read_only", "value"}-def validate_block_limit_constraint(update_data: dict, existing_block: BlockModel) -> None:- """- Validates that block limit constraints are satisfied when updating a block.-- Rules:- - If limit is being updated, it must be >= the length of the value (existing or new)- - If value is being updated, its length must not exceed the limit (existing or new)-- Args:- update_data: Dictionary of fields to update- existing_block: The current block being updated-- Raises:- LettaInvalidArgumentError: If validation fails- """- # If limit is being updated, ensure it's >= current value length- if "limit" in update_data:- # Get the value that will be used (either from update_data or existing)- value_to_check = update_data.get("value", existing_block.value)- limit_to_check = update_data["limit"]- if value_to_check and limit_to_check < len(value_to_check):- raise LettaInvalidArgumentError(- f"Limit ({limit_to_check}) cannot be less than current value length ({len(value_to_check)} characters)",- argument_name="limit",- )- # If value is being updated and there's an existing limit, ensure value doesn't exceed limit- elif "value" in update_data and existing_block.limit:- if len(update_data["value"]) > existing_block.limit:- raise LettaInvalidArgumentError(- f"Value length ({len(update_data['value'])} characters) exceeds block limit ({existing_block.limit} characters)",- argument_name="value",- )---def validate_block_creation(block_data: dict) -> None:- """- Validates that block limit constraints are satisfied when creating a block.-- Rules:- - If both value and limit are provided, limit must be >= value length-- Args:- block_data: Dictionary of block fields for creation-- Raises:- LettaInvalidArgumentError: If validation fails- """- value = block_data.get("value")- limit = block_data.get("limit")-- if value and limit and len(value) > limit:- raise LettaInvalidArgumentError(- f"Block limit ({limit}) must be greater than or equal to value length ({len(value)} characters)", argument_name="limit"- )--def _cursor_filter(sort_col, id_col, ref_sort_val, ref_id, forward: bool):"""Returns a SQLAlchemy filter expression for cursor-based pagination.@@ -199,8 +143,6 @@ class BlockManager:# Extract tags before creating the ORM model (tags is not a column)tags = data.pop("tags", None) or []- # Validate block creation constraints- validate_block_creation(data)block_model = BlockModel(**data, organization_id=actor.organization_id)await block_model.create_async(session, actor=actor, no_commit=True, no_refresh=True)@@ -239,7 +181,6 @@ class BlockManager:tags = block_data.pop("tags", None) or []if tags:tags_by_index[i] = tags- validate_block_creation(block_data)validated_data.append(block_data)block_models = [BlockModel(**data, organization_id=actor.organization_id) for data in validated_data]@@ -276,9 +217,6 @@ class BlockManager:# Extract tags from update data (it's not a column on the block table)new_tags = update_data.pop("tags", None)- # Validate limit constraints before updating- validate_block_limit_constraint(update_data, block)-current_tags: Optional[List[str]] = Noneif new_tags is not None:result = await session.execute(select(BlocksTags.tag).where(BlocksTags.block_id == block_id))@@ -886,9 +824,6 @@ class BlockManager:for block in blocks:new_val = updates[block.id]- if len(new_val) > block.limit:- logger.warning(f"Value length ({len(new_val)}) exceeds limit ({block.limit}) for block {block.id!r}, truncating...")- new_val = new_val[: block.limit]block.value = new_val# context manager now handles commitsdiff --git a/tests/managers/test_block_manager.py b/tests/managers/test_block_manager.pyindex ddfe4a2c1..555aa0995 100644--- a/tests/managers/test_block_manager.py+++ b/tests/managers/test_block_manager.py@@ -559,31 +559,6 @@ async def test_update_block(server: SyncServer, default_user):assert updated_block.description == "Updated description"-async def test_update_block_limit(server: SyncServer, default_user):- block_manager = BlockManager()- block = await block_manager.create_or_update_block_async(- PydanticBlock(label="persona", value="Original Content", limit=20000), actor=default_user- )-- limit = len("Updated Content") * 2000- update_data = BlockUpdate(value="Updated Content" * 2000, description="Updated description")-- # Check that exceeding the block limit raises an exception- with pytest.raises(LettaInvalidArgumentError):- await block_manager.update_block_async(block_id=block.id, block_update=update_data, actor=default_user)-- # Ensure the update works when within limits- update_data = BlockUpdate(value="Updated Content" * 2000, description="Updated description", limit=limit)- await block_manager.update_block_async(block_id=block.id, block_update=update_data, actor=default_user)-- # Retrieve the updated block and validate the update- updated_block = await block_manager.get_block_by_id_async(actor=default_user, block_id=block.id)-- assert updated_block.value == "Updated Content" * 2000- assert updated_block.description == "Updated description"--@pytest.mark.asyncioasync def test_update_block_limit_does_not_reset(server: SyncServer, default_user):block_manager = BlockManager()@@ -704,7 +679,9 @@ async def test_batch_create_multiple_blocks(server: SyncServer, default_user):assert expected_labels.issubset(all_labels)-async def test_bulk_update_skips_missing_and_truncates_then_returns_none(server: SyncServer, default_user: PydanticUser, caplog):+async def test_bulk_update_skips_missing_and_updates_without_truncation_then_returns_none(+ server: SyncServer, default_user: PydanticUser, caplog+):mgr = BlockManager()# create one block with a small limit@@ -713,7 +690,7 @@ async def test_bulk_update_skips_missing_and_truncates_then_returns_none(server:actor=default_user,)- # prepare updates: one real id with an over‐limit value, plus one missing id+ # prepare updates: one real id with an over-limit value, plus one missing idlong_val = random_string(10) # length > limit==5updates = {b.id: long_val,@@ -725,14 +702,13 @@ async def test_bulk_update_skips_missing_and_truncates_then_returns_none(server:# default return_hydrated=False → should be Noneassert result is None- # warnings should mention skipping the missing ID and truncation+ # warnings should mention skipping the missing IDassert "skipping during bulk update" in caplog.text- assert "truncating" in caplog.text- # confirm the value was truncated to `limit` characters+ # confirm the value was not truncatedreloaded = await mgr.get_block_by_id_async(actor=default_user, block_id=b.id)- assert len(reloaded.value) == 5- assert reloaded.value == long_val[:5]+ assert len(reloaded.value) == len(long_val)+ assert reloaded.value == long_val@pytest.mark.skip(reason="TODO: implement for async")diff --git a/tests/test_client.py b/tests/test_client.pyindex 2ffd84331..30aa5caec 100644--- a/tests/test_client.py+++ b/tests/test_client.py@@ -7,7 +7,7 @@ from http.server import BaseHTTPRequestHandler, HTTPServerimport httpximport pytestfrom dotenv import load_dotenv-from letta_client import APIError, Letta+from letta_client import Lettafrom letta_client.types import MessageCreateParamfrom letta_client.types.agent_state import AgentStatefrom sqlalchemy import delete@@ -422,53 +422,6 @@ def test_attach_detach_agent_memory_block(client: Letta, agent: AgentState):assert example_new_label not in [block.label for block in client.agents.blocks.list(agent_id=updated_agent.id).items]-def test_update_agent_memory_limit(client: Letta):- """Test that we can update the limit of a block in an agent's memory"""-- agent = client.agents.create(- model="anthropic/claude-haiku-4-5",- embedding="openai/text-embedding-3-small",- memory_blocks=[- {"label": "human", "value": "username: sarah", "limit": 1000},- {"label": "persona", "value": "you are sarah", "limit": 1000},- ],- )-- current_labels = [block.label for block in client.agents.blocks.list(agent_id=agent.id).items]- example_label = current_labels[0]- example_new_limit = 1-- current_labels = [block.label for block in client.agents.blocks.list(agent_id=agent.id).items]- example_label = current_labels[0]- example_new_limit = 1- current_block = client.agents.blocks.retrieve(agent_id=agent.id, block_label=example_label)- current_block_length = len(current_block.value)-- assert example_new_limit != current_block.limit- assert example_new_limit < current_block_length-- # We expect this to throw a value error- with pytest.raises(APIError):- client.agents.blocks.update(- agent_id=agent.id,- block_label=example_label,- limit=example_new_limit,- )-- # Now try the same thing with a higher limit- example_new_limit = current_block_length + 10000- assert example_new_limit > current_block_length- client.agents.blocks.update(- agent_id=agent.id,- block_label=example_label,- limit=example_new_limit,- )-- assert example_new_limit == client.agents.blocks.retrieve(agent_id=agent.id, block_label=example_label).limit-- client.agents.delete(agent.id)--# --------------------------------------------------------------------------------------------------------------------# Agent Tools# --------------------------------------------------------------------------------------------------------------------diff --git a/tests/test_memory.py b/tests/test_memory.pyindex a6f7c74fd..23fffb3cf 100644--- a/tests/test_memory.py+++ b/tests/test_memory.py@@ -23,13 +23,6 @@ def test_chat_memory_init_and_utils(chat_memory: Memory):assert set(chat_memory.list_block_labels()) == {"persona", "human"}-def test_memory_limit_validation(chat_memory: Memory):- with pytest.raises(ValueError):- ChatMemory(persona="x " * 60000, human="y " * 60000)- with pytest.raises(ValueError):- chat_memory.get_block("persona").value = "x " * 60000--def test_get_block_not_found(chat_memory: Memory):with pytest.raises(KeyError):chat_memory.get_block("missing")diff --git a/tests/test_sdk_client.py b/tests/test_sdk_client.pyindex 8954c5181..762985f10 100644--- a/tests/test_sdk_client.py+++ b/tests/test_sdk_client.py@@ -801,38 +801,6 @@ def test_add_remove_agent_memory_block(client: LettaSDKClient, agent: AgentStateassert example_new_label not in current_labels-def test_update_agent_memory_limit(client: LettaSDKClient, agent: AgentState):- """Test that we can update the limit of a block in an agent's memory"""-- current_labels = [block.label for block in client.agents.blocks.list(agent_id=agent.id).items]- example_label = current_labels[0]- example_new_limit = 1- current_block = client.agents.blocks.retrieve(agent_id=agent.id, block_label=example_label)- current_block_length = len(current_block.value)-- assert example_new_limit != client.agents.blocks.retrieve(agent_id=agent.id, block_label=example_label).limit- assert example_new_limit < current_block_length-- # We expect this to throw a value error- with pytest.raises(APIError):- client.agents.blocks.update(- agent_id=agent.id,- block_label=example_label,- limit=example_new_limit,- )-- # Now try the same thing with a higher limit- example_new_limit = current_block_length + 10000- assert example_new_limit > current_block_length- client.agents.blocks.update(- agent_id=agent.id,- block_label=example_label,- limit=example_new_limit,- )-- assert example_new_limit == client.agents.blocks.retrieve(agent_id=agent.id, block_label=example_label).limit--def test_messages(client: LettaSDKClient, agent: AgentState):send_message_response = client.agents.messages.create(agent_id=agent.id,