Skip to content

Build and run agents you can see, understand and trust.

fix(app): answer 422 when a session patch cannot be represented (#2745)

lihongyuan99d9959e855984
diff --git a/src/agentscope/app/_router/_session.py b/src/agentscope/app/_router/_session.py
index 2273de0ce..9ed7f1aa3 100644
--- a/src/agentscope/app/_router/_session.py
+++ b/src/agentscope/app/_router/_session.py
@@ -6,6 +6,7 @@ from typing import AsyncGenerator
from fastapi import APIRouter, Depends, HTTPException, Query, status
from fastapi.responses import StreamingResponse
+from pydantic import ValidationError
from ..._utils._common import _generate_id
from ..access import ResourceKind
@@ -589,12 +590,20 @@ async def update_session(
"auto": False,
}
+ try:
+ updated_config = SessionConfig.model_validate(
+ {**existing.config.model_dump(mode="json"), **config_updates},
+ )
+ except ValidationError as exc:
+ raise HTTPException(
+ status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
+ detail=exc.errors(),
+ ) from exc
+
return await storage.upsert_session(
user_id=user_id,
agent_id=agent_id,
- config=SessionConfig.model_validate(
- {**existing.config.model_dump(mode="json"), **config_updates},
- ),
+ config=updated_config,
state=updated_state,
session_id=session_id,
)
diff --git a/tests/session_config_patch_test.py b/tests/session_config_patch_test.py
index 0e3cd928e..4b917bd15 100644
--- a/tests/session_config_patch_test.py
+++ b/tests/session_config_patch_test.py
@@ -449,6 +449,33 @@ class SessionConfigPatchTest(IsolatedAsyncioTestCase):
).json()
self.assertIsNone(listed["sessions"][0]["session"]["config"]["cwd"])
+ def test_name_null_is_refused_rather_than_crashing(self) -> None:
+ """Reject a null name with 422 and preserve the stored name."""
+ name = self.client.get(
+ "/sessions/",
+ headers=HEADERS,
+ params={"agent_id": self.agent_id},
+ ).json()["sessions"][0]["session"]["config"]["name"]
+
+ response = self._patch({"name": None})
+
+ self.assertEqual(response.status_code, 422, response.text)
+ self.assertEqual(
+ [
+ (error["type"], error["loc"])
+ for error in response.json()["detail"]
+ ],
+ [("string_type", ["name"])],
+ )
+ self.assertEqual(
+ self.client.get(
+ "/sessions/",
+ headers=HEADERS,
+ params={"agent_id": self.agent_id},
+ ).json()["sessions"][0]["session"]["config"]["name"],
+ name,
+ )
+
def test_cwd_is_not_confined_to_the_workspace_root(self) -> None:
"""Absolute paths and ``..`` are ordinary values, not attacks.