Skip to content

Commit f52f736

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
fix: support both Pydantic and Protobuf AgentCard during A2aAgent serialization
PiperOrigin-RevId: 918921714
1 parent 3bc2f58 commit f52f736

7 files changed

Lines changed: 477 additions & 25 deletions

File tree

agentplatform/_genai/_agent_engines_utils.py

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -665,9 +665,11 @@ def _generate_class_methods_spec_or_raise(
665665
class_method = _to_proto(schema_dict)
666666
class_method[_MODE_KEY_IN_SCHEMA] = mode
667667
if hasattr(agent, "agent_card"):
668-
class_method[_A2A_AGENT_CARD] = json_format.MessageToJson(
669-
getattr(agent, "agent_card")
670-
)
668+
card = getattr(agent, "agent_card")
669+
if card is not None:
670+
class_method[_A2A_AGENT_CARD] = _serialize_agent_card_to_json(
671+
card
672+
)
671673
class_methods_spec.append(class_method)
672674

673675
return class_methods_spec
@@ -2290,3 +2292,59 @@ def _import_autogen_tools_or_warn() -> Optional[types.ModuleType]:
22902292
"call `pip install google-cloud-aiplatform[ag2]`."
22912293
)
22922294
return None
2295+
2296+
2297+
def _serialize_agent_card_to_dict(card: Any) -> Optional[Dict[str, Any]]:
2298+
"""Validates and serializes an AgentCard to a dictionary representation.
2299+
2300+
Args:
2301+
card: The AgentCard instance (Pydantic model or Protobuf Message).
2302+
2303+
Returns:
2304+
The serialized card as a dictionary.
2305+
2306+
Raises:
2307+
TypeError: If the card type is not supported.
2308+
"""
2309+
if card is None:
2310+
return None
2311+
2312+
if hasattr(card, "model_dump"):
2313+
return typing.cast(dict[str, Any], card.model_dump(exclude_none=True))
2314+
elif hasattr(card, "DESCRIPTOR"):
2315+
from google.protobuf import json_format
2316+
2317+
return typing.cast(dict[str, Any], json_format.MessageToDict(card))
2318+
else:
2319+
raise TypeError(
2320+
f"Unsupported AgentCard type: {type(card)}. "
2321+
"Only Pydantic models and Protobuf Messages are supported."
2322+
)
2323+
2324+
2325+
def _serialize_agent_card_to_json(card: Any) -> Optional[str]:
2326+
"""Validates and serializes an AgentCard to a JSON string representation.
2327+
2328+
Args:
2329+
card: The AgentCard instance (Pydantic model or Protobuf Message).
2330+
2331+
Returns:
2332+
The serialized card as a JSON string.
2333+
2334+
Raises:
2335+
TypeError: If the card type is not supported.
2336+
"""
2337+
if card is None:
2338+
return None
2339+
2340+
if hasattr(card, "model_dump_json"):
2341+
return typing.cast(str, card.model_dump_json())
2342+
elif hasattr(card, "DESCRIPTOR"):
2343+
from google.protobuf import json_format
2344+
2345+
return typing.cast(str, json_format.MessageToJson(card))
2346+
else:
2347+
raise TypeError(
2348+
f"Unsupported AgentCard type: {type(card)}. "
2349+
"Only Pydantic models and Protobuf Messages are supported."
2350+
)

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
@@ -2003,11 +2003,11 @@ def _generate_class_methods_spec_or_raise(
20032003
class_method[_MODE_KEY_IN_SCHEMA] = mode
20042004
# A2A agent card is a special case, when running in A2A mode,
20052005
if hasattr(agent_engine, "agent_card"):
2006-
from google.protobuf import json_format
2007-
2008-
class_method[_A2A_AGENT_CARD] = json_format.MessageToJson(
2009-
getattr(agent_engine, "agent_card")
2010-
)
2006+
card = getattr(agent_engine, "agent_card")
2007+
if card is not None:
2008+
class_method[_A2A_AGENT_CARD] = _agent_engines_utils._serialize_agent_card_to_json(
2009+
card
2010+
)
20112011
class_methods_spec.append(class_method)
20122012

20132013
return class_methods_spec

0 commit comments

Comments
 (0)