Skip to content

Commit ca113f0

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 ca113f0

8 files changed

Lines changed: 543 additions & 30 deletions

File tree

agentplatform/_genai/_agent_engines_utils.py

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -652,10 +652,9 @@ 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(
657-
getattr(agent, "agent_card")
658-
)
655+
card = getattr(agent, "agent_card", None)
656+
if card is not None:
657+
class_method[_A2A_AGENT_CARD] = _serialize_agent_card_to_json(card)
659658
class_methods_spec.append(class_method)
660659

661660
return class_methods_spec
@@ -2002,7 +2001,7 @@ def _is_adk_agent(agent_engine: _AgentEngineInterface) -> bool:
20022001

20032002

20042003
def _add_telemetry_enablement_env(
2005-
env_vars: Optional[Dict[str, Union[str, Any]]]
2004+
env_vars: Optional[Dict[str, Union[str, Any]]],
20062005
) -> Optional[Dict[str, Union[str, Any]]]:
20072006
"""Adds telemetry enablement env var to the env vars.
20082007
@@ -2263,3 +2262,55 @@ def _import_autogen_tools_or_warn() -> Optional[types.ModuleType]:
22632262
"call `pip install google-cloud-aiplatform[ag2]`."
22642263
)
22652264
return None
2265+
2266+
2267+
def _serialize_agent_card_to_dict(card: Any) -> Optional[Dict[str, Any]]:
2268+
"""Validates and serializes an AgentCard to a dictionary representation.
2269+
2270+
Args:
2271+
card: The AgentCard instance (Protobuf Message).
2272+
2273+
Returns:
2274+
The serialized card as a dictionary.
2275+
2276+
Raises:
2277+
TypeError: If the card type is not supported.
2278+
"""
2279+
if card is None:
2280+
return None
2281+
2282+
if hasattr(card, "DESCRIPTOR"):
2283+
from google.protobuf import json_format
2284+
2285+
return typing.cast(dict[str, Any], json_format.MessageToDict(card))
2286+
else:
2287+
raise TypeError(
2288+
f"Unsupported AgentCard type: {type(card)}. "
2289+
"Only Protobuf Messages are supported in agentplatform."
2290+
)
2291+
2292+
2293+
def _serialize_agent_card_to_json(card: Any) -> Optional[str]:
2294+
"""Validates and serializes an AgentCard to a JSON string representation.
2295+
2296+
Args:
2297+
card: The AgentCard instance (Protobuf Message).
2298+
2299+
Returns:
2300+
The serialized card as a JSON string.
2301+
2302+
Raises:
2303+
TypeError: If the card type is not supported.
2304+
"""
2305+
if card is None:
2306+
return None
2307+
2308+
if hasattr(card, "DESCRIPTOR"):
2309+
from google.protobuf import json_format
2310+
2311+
return typing.cast(str, json_format.MessageToJson(card))
2312+
else:
2313+
raise TypeError(
2314+
f"Unsupported AgentCard type: {type(card)}. "
2315+
"Only Protobuf Messages are supported in agentplatform."
2316+
)

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)