From ea2022d63497374c1aaeb19bd64c6f79d547cde1 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 15 Jan 2026 21:31:43 +0000 Subject: [PATCH] Refactor to eliminate duplicate code in model protocol detection Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> --- .../_workflows/_checkpoint_encoding.py | 31 ++++++++----------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/python/packages/core/agent_framework/_workflows/_checkpoint_encoding.py b/python/packages/core/agent_framework/_workflows/_checkpoint_encoding.py index d5a1a7f970..97ef4caab5 100644 --- a/python/packages/core/agent_framework/_workflows/_checkpoint_encoding.py +++ b/python/packages/core/agent_framework/_workflows/_checkpoint_encoding.py @@ -210,27 +210,12 @@ def decode_checkpoint_value(value: Any) -> Any: return value -def _supports_model_protocol(obj: object) -> bool: - """Detect objects that expose dictionary serialization hooks.""" - try: - obj_type: type[Any] = type(obj) - except Exception: - return False - - has_to_dict = hasattr(obj, "to_dict") and callable(getattr(obj, "to_dict", None)) # type: ignore[arg-type] - has_from_dict = hasattr(obj_type, "from_dict") and callable(getattr(obj_type, "from_dict", None)) - - has_to_json = hasattr(obj, "to_json") and callable(getattr(obj, "to_json", None)) # type: ignore[arg-type] - has_from_json = hasattr(obj_type, "from_json") and callable(getattr(obj_type, "from_json", None)) - - return (has_to_dict and has_from_dict) or (has_to_json and has_from_json) - - def _class_supports_model_protocol(cls: type[Any]) -> bool: """Check if a class type supports the model serialization protocol. - This is similar to _supports_model_protocol but works on classes directly, - used for validation during deserialization. + Checks for pairs of serialization/deserialization methods: + - to_dict/from_dict + - to_json/from_json """ has_to_dict = hasattr(cls, "to_dict") and callable(getattr(cls, "to_dict", None)) has_from_dict = hasattr(cls, "from_dict") and callable(getattr(cls, "from_dict", None)) @@ -241,6 +226,16 @@ def _class_supports_model_protocol(cls: type[Any]) -> bool: return (has_to_dict and has_from_dict) or (has_to_json and has_from_json) +def _supports_model_protocol(obj: object) -> bool: + """Detect objects that expose dictionary serialization hooks.""" + try: + obj_type: type[Any] = type(obj) + except Exception: + return False + + return _class_supports_model_protocol(obj_type) + + def _import_qualified_name(qualname: str) -> type[Any] | None: if ":" not in qualname: return None