Skip to content

Commit 1e339f6

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
fix: resolve AttributeError by supporting both Pydantic and Protobuf AgentCard serialization
PiperOrigin-RevId: 918921714
1 parent 3b3471a commit 1e339f6

7 files changed

Lines changed: 493 additions & 24 deletions

File tree

agentplatform/_genai/_agent_engines_utils.py

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,8 @@ def _generate_class_methods_spec_or_raise(
652652

653653
class_method = _to_proto(schema_dict)
654654
class_method[_MODE_KEY_IN_SCHEMA] = mode
655-
if hasattr(agent, "agent_card"):
656-
class_method[_A2A_AGENT_CARD] = json_format.MessageToJson(
655+
if hasattr(agent, "agent_card") and getattr(agent, "agent_card") is not None:
656+
class_method[_A2A_AGENT_CARD] = _serialize_agent_card_to_json(
657657
getattr(agent, "agent_card")
658658
)
659659
class_methods_spec.append(class_method)
@@ -2263,3 +2263,57 @@ def _import_autogen_tools_or_warn() -> Optional[types.ModuleType]:
22632263
"call `pip install google-cloud-aiplatform[ag2]`."
22642264
)
22652265
return None
2266+
2267+
2268+
def _serialize_agent_card_to_dict(card: Any) -> Optional[Dict[str, Any]]:
2269+
"""Validates and serializes an AgentCard to a dictionary representation.
2270+
2271+
Args:
2272+
card: The AgentCard instance (must be a Protobuf Message).
2273+
2274+
Returns:
2275+
The serialized card as a dictionary.
2276+
2277+
Raises:
2278+
TypeError: If the card type is not a Protobuf Message (e.g., Pydantic).
2279+
"""
2280+
if card is None:
2281+
return None
2282+
2283+
if hasattr(card, "DESCRIPTOR"):
2284+
from google.protobuf import json_format
2285+
2286+
return typing.cast(dict[str, Any], json_format.MessageToDict(card))
2287+
2288+
raise TypeError(
2289+
f"Unsupported AgentCard type: {type(card)}. "
2290+
"The agentplatform namespace exclusively supports Protobuf Messages. "
2291+
"Pydantic models are not supported."
2292+
)
2293+
2294+
2295+
def _serialize_agent_card_to_json(card: Any) -> Optional[str]:
2296+
"""Validates and serializes an AgentCard to a JSON string representation.
2297+
2298+
Args:
2299+
card: The AgentCard instance (must be a Protobuf Message).
2300+
2301+
Returns:
2302+
The serialized card as a JSON string.
2303+
2304+
Raises:
2305+
TypeError: If the card type is not a Protobuf Message (e.g., Pydantic).
2306+
"""
2307+
if card is None:
2308+
return None
2309+
2310+
if hasattr(card, "DESCRIPTOR"):
2311+
from google.protobuf import json_format
2312+
2313+
return typing.cast(str, json_format.MessageToJson(card))
2314+
2315+
raise TypeError(
2316+
f"Unsupported AgentCard type: {type(card)}. "
2317+
"The agentplatform namespace exclusively supports Protobuf Messages. "
2318+
"Pydantic models are not supported."
2319+
)

agentplatform/_genai/agent_engines.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2498,12 +2498,12 @@ def _create_config(
24982498

24992499
if hasattr(agent, "agent_card"):
25002500
agent_card = getattr(agent, "agent_card")
2501-
if agent_card:
2501+
if agent_card is not None:
25022502
try:
2503-
from google.protobuf import json_format
2504-
2505-
agent_engine_spec["agent_card"] = json_format.MessageToDict(
2506-
agent_card
2503+
agent_engine_spec["agent_card"] = (
2504+
_agent_engines_utils._serialize_agent_card_to_dict(
2505+
agent_card
2506+
)
25072507
)
25082508
except Exception as e:
25092509
raise ValueError(

agentplatform/agent_engines/_agent_engines.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,11 +2005,11 @@ def _generate_class_methods_spec_or_raise(
20052005
class_method[_MODE_KEY_IN_SCHEMA] = mode
20062006
# A2A agent card is a special case, when running in A2A mode,
20072007
if hasattr(agent_engine, "agent_card"):
2008-
from google.protobuf import json_format
2009-
2010-
class_method[_A2A_AGENT_CARD] = json_format.MessageToJson(
2011-
getattr(agent_engine, "agent_card")
2012-
)
2008+
card = getattr(agent_engine, "agent_card")
2009+
if card is not None:
2010+
class_method[_A2A_AGENT_CARD] = (
2011+
_agent_engines_utils._serialize_agent_card_to_json(card)
2012+
)
20132013
class_methods_spec.append(class_method)
20142014

20152015
return class_methods_spec

0 commit comments

Comments
 (0)