Skip to content

Commit d8d8bab

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

7 files changed

Lines changed: 418 additions & 28 deletions

File tree

agentplatform/_genai/_agent_engines_utils.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -665,9 +665,17 @@ 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+
if hasattr(card, "model_dump_json"):
671+
class_method[_A2A_AGENT_CARD] = card.model_dump_json()
672+
elif hasattr(card, "DESCRIPTOR"):
673+
class_method[_A2A_AGENT_CARD] = json_format.MessageToJson(card)
674+
else:
675+
raise TypeError(
676+
f"Unsupported AgentCard type: {type(card)}. "
677+
"Only Pydantic models and Protobuf Messages are supported."
678+
)
671679
class_methods_spec.append(class_method)
672680

673681
return class_methods_spec

agentplatform/_genai/agent_engines.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2498,13 +2498,23 @@ 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
2507-
)
2503+
if hasattr(agent_card, "model_dump"):
2504+
agent_engine_spec["agent_card"] = agent_card.model_dump(
2505+
exclude_none=True
2506+
)
2507+
elif hasattr(agent_card, "DESCRIPTOR"):
2508+
from google.protobuf import json_format
2509+
2510+
agent_engine_spec["agent_card"] = json_format.MessageToDict(
2511+
agent_card
2512+
)
2513+
else:
2514+
raise TypeError(
2515+
f"Unsupported AgentCard type: {type(agent_card)}. "
2516+
"Only Pydantic models and Protobuf Messages are supported."
2517+
)
25082518
except Exception as e:
25092519
raise ValueError(
25102520
f"Failed to convert agent card to dict (serialization error): {e}"

agentplatform/agent_engines/_agent_engines.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,11 +2003,19 @@ 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+
if hasattr(card, "model_dump_json"):
2009+
class_method[_A2A_AGENT_CARD] = card.model_dump_json()
2010+
elif hasattr(card, "DESCRIPTOR"):
2011+
from google.protobuf import json_format
2012+
2013+
class_method[_A2A_AGENT_CARD] = json_format.MessageToJson(card)
2014+
else:
2015+
raise TypeError(
2016+
f"Unsupported AgentCard type: {type(card)}. "
2017+
"Only Pydantic models and Protobuf Messages are supported."
2018+
)
20112019
class_methods_spec.append(class_method)
20122020

20132021
return class_methods_spec

0 commit comments

Comments
 (0)