Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
perf(msg): set empty dictionary as defautl value for `msg.metadata` and improve the structured model in the werewolf game example (#1234)
qbc6137edd3d830
diff --git a/examples/game/werewolves/structured_model.py b/examples/game/werewolves/structured_model.pyindex a5f89e1cf..1e9b7d814 100644--- a/examples/game/werewolves/structured_model.py+++ b/examples/game/werewolves/structured_model.py@@ -2,7 +2,7 @@"""The structured output models used in the werewolf game."""from typing import Literal-from pydantic import BaseModel, Field+from pydantic import BaseModel, Field, model_validatorfrom agentscope.agent import AgentBase@@ -52,6 +52,24 @@ def get_poison_model(agents: list[AgentBase]) -> type[BaseModel]:default=None,)+ # pylint: disable=no-self-argument+ @model_validator(mode="before")+ def clear_name_if_no_poison(cls, values: dict) -> dict:+ """Clear name if no poison is used.+ This is to avoid validation errors when `poison` is false but a+ `name` is provided.++ Args:+ values (`dict`):+ The input data for the model.+ Returns:+ `dict`:+ The validated and possibly modified data.+ """+ if isinstance(values, dict) and not values.get("poison"):+ values["name"] = None+ return values+return WitchPoisonModel@@ -85,4 +103,22 @@ def get_hunter_model(agents: list[AgentBase]) -> type[BaseModel]:default=None,)+ # pylint: disable=no-self-argument+ @model_validator(mode="before")+ def clear_name_if_no_shoot(cls, values: dict) -> dict:+ """If shoot is false, set name to None to skip validation.+ This is to avoid validation errors when `shoot` is false but a+ `name` is provided.++ Args:+ values (`dict`):+ The input data for the model.+ Returns:+ `dict`:+ The validated and possibly modified data.+ """+ if isinstance(values, dict) and not values.get("shoot"):+ values["name"] = None+ return values+return HunterModeldiff --git a/src/agentscope/message/_message_base.py b/src/agentscope/message/_message_base.pyindex ea690843c..6a69a9c58 100644--- a/src/agentscope/message/_message_base.py+++ b/src/agentscope/message/_message_base.py@@ -61,7 +61,7 @@ class Msg:assert role in ["user", "assistant", "system"]self.role = role- self.metadata = metadata+ self.metadata = metadata or {}self.id = shortuuid.uuid()self.timestamp = (diff --git a/tests/formatter_a2a_test.py b/tests/formatter_a2a_test.pyindex 2c7ec9f8f..944387427 100644--- a/tests/formatter_a2a_test.py+++ b/tests/formatter_a2a_test.py@@ -378,7 +378,7 @@ class A2AFormatterTest(IsolatedAsyncioTestCase):"unknown content'}",},],- "metadata": None,+ "metadata": {},"timestamp": as_msgs[0].timestamp,},)@@ -398,7 +398,7 @@ class A2AFormatterTest(IsolatedAsyncioTestCase):"output": "Artifact tool output.",},],- "metadata": None,+ "metadata": {},"timestamp": as_msgs[1].timestamp,},)