Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat: Convert AF serialization to formatted json (#1691)
Matthew Zhou01f9a9c24348
diff --git a/letta/server/rest_api/routers/v1/agents.py b/letta/server/rest_api/routers/v1/agents.pyindex 68fdd19b6..34d4e99a5 100644--- a/letta/server/rest_api/routers/v1/agents.py+++ b/letta/server/rest_api/routers/v1/agents.py@@ -1,14 +1,15 @@import jsonimport tracebackfrom datetime import datetime-from typing import Annotated, List, Optional+from typing import Annotated, Any, List, Optionalfrom fastapi import APIRouter, BackgroundTasks, Body, Depends, File, Header, HTTPException, Query, UploadFile, statusfrom fastapi.responses import JSONResponsefrom marshmallow import ValidationError+from orjson import orjsonfrom pydantic import Fieldfrom sqlalchemy.exc import IntegrityError, OperationalError-from starlette.responses import StreamingResponse+from starlette.responses import Response, StreamingResponsefrom letta.agents.letta_agent import LettaAgentfrom letta.constants import DEFAULT_MESSAGE_TOOL, DEFAULT_MESSAGE_TOOL_KWARG@@ -102,7 +103,14 @@ def list_agents()[email protected]("/{agent_id}/export", operation_id="export_agent_serialized")+class IndentedORJSONResponse(Response):+ media_type = "application/json"++ def render(self, content: Any) -> bytes:+ return orjson.dumps(content, option=orjson.OPT_INDENT_2)++[email protected]("/{agent_id}/export", response_class=IndentedORJSONResponse, operation_id="export_agent_serialized")def export_agent_serialized(agent_id: str,server: "SyncServer" = Depends(get_letta_server),@@ -118,9 +126,7 @@ def export_agent_serialized(try:agent = server.agent_manager.serialize(agent_id=agent_id, actor=actor)- # Convert to JSON with 2-space indentation- content = agent.model_dump_json(indent=2)- return JSONResponse(content=content, media_type="application/json")+ return agent.model_dump()except NoResultFound:raise HTTPException(status_code=404, detail=f"Agent with id={agent_id} not found for user_id={actor.id}.")diff --git a/tests/test_agent_serialization.py b/tests/test_agent_serialization.pyindex 3b99775cb..2bd228f9e 100644--- a/tests/test_agent_serialization.py+++ b/tests/test_agent_serialization.py@@ -25,7 +25,6 @@ from letta.schemas.user import Userfrom letta.serialize_schemas.pydantic_agent_schema import AgentSchemafrom letta.server.rest_api.app import appfrom letta.server.server import SyncServer-from letta.utils import parse_jsonconsole = Console()@@ -573,8 +572,7 @@ def test_agent_download_upload_flow(fastapi_client, server, serialize_test_agent# Ensure response matches expected schemaresponse_json = response.json()- parsed_response = parse_json(response_json)- agent_schema = AgentSchema.model_validate(parsed_response) # Validate as Pydantic model+ agent_schema = AgentSchema.model_validate(response_json) # Validate as Pydantic modelagent_json = agent_schema.model_dump(mode="json") # Convert back to serializable JSON# Step 2: Upload the serialized agent as a copy@@ -602,13 +600,6 @@ def test_agent_download_upload_flow(fastapi_client, server, serialize_test_agentprint_dict_diff(json.loads(serialize_test_agent.model_dump_json()), json.loads(agent_copy.model_dump_json()))assert compare_agent_state(server, serialize_test_agent, agent_copy, append_copy_suffix, default_user, other_user)- # Step 4: Ensure copied agent receives messages correctly- server.send_messages(- actor=other_user,- agent_id=copied_agent_id,- messages=[MessageCreate(role=MessageRole.user, content="Hello copied agent!")],- )-@pytest.mark.parametrize("filename",