Skip to content

Commit b1f6be9

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
fix: resolve AttributeError when serializing agent instances with diverse card format architectures
PiperOrigin-RevId: 927226105
1 parent 0c72b97 commit b1f6be9

3 files changed

Lines changed: 69 additions & 13 deletions

File tree

vertexai/_genai/_agent_engines_utils.py

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -653,9 +653,9 @@ def _generate_class_methods_spec_or_raise(
653653
class_method = _to_proto(schema_dict)
654654
class_method[_MODE_KEY_IN_SCHEMA] = mode
655655
if hasattr(agent, "agent_card"):
656-
class_method[_A2A_AGENT_CARD] = json_format.MessageToJson(
657-
getattr(agent, "agent_card")
658-
)
656+
card = getattr(agent, "agent_card")
657+
if card is not None:
658+
class_method[_A2A_AGENT_CARD] = _serialize_agent_card_to_json(card)
659659
class_methods_spec.append(class_method)
660660

661661
return class_methods_spec
@@ -2148,3 +2148,59 @@ def _add_telemetry_enablement_env(
21482148
return env_vars
21492149

21502150
return env_vars | env_to_add
2151+
2152+
2153+
def _serialize_agent_card_to_dict(card: Any) -> Optional[Dict[str, Any]]:
2154+
"""Validates and serializes an AgentCard to a dictionary representation.
2155+
2156+
Args:
2157+
card: The AgentCard instance (Pydantic model or Protobuf Message).
2158+
2159+
Returns:
2160+
The serialized card as a dictionary.
2161+
2162+
Raises:
2163+
TypeError: If the card type is not supported.
2164+
"""
2165+
if card is None:
2166+
return None
2167+
2168+
if hasattr(card, "model_dump"):
2169+
return typing.cast(dict[str, Any], card.model_dump(exclude_none=True))
2170+
elif hasattr(card, "DESCRIPTOR"):
2171+
from google.protobuf import json_format
2172+
2173+
return typing.cast(dict[str, Any], json_format.MessageToDict(card))
2174+
else:
2175+
raise TypeError(
2176+
f"Unsupported AgentCard type: {type(card)}. "
2177+
"Only Pydantic models and Protobuf Messages are supported."
2178+
)
2179+
2180+
2181+
def _serialize_agent_card_to_json(card: Any) -> Optional[str]:
2182+
"""Validates and serializes an AgentCard to a JSON string representation.
2183+
2184+
Args:
2185+
card: The AgentCard instance (Pydantic model or Protobuf Message).
2186+
2187+
Returns:
2188+
The serialized card as a JSON string.
2189+
2190+
Raises:
2191+
TypeError: If the card type is not supported.
2192+
"""
2193+
if card is None:
2194+
return None
2195+
2196+
if hasattr(card, "model_dump_json"):
2197+
return typing.cast(str, card.model_dump_json())
2198+
elif hasattr(card, "DESCRIPTOR"):
2199+
from google.protobuf import json_format
2200+
2201+
return typing.cast(str, json_format.MessageToJson(card))
2202+
else:
2203+
raise TypeError(
2204+
f"Unsupported AgentCard type: {type(card)}. "
2205+
"Only Pydantic models and Protobuf Messages are supported."
2206+
)

vertexai/_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(

vertexai/agent_engines/_agent_engines.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,11 +1997,11 @@ def _generate_class_methods_spec_or_raise(
19971997
class_method[_MODE_KEY_IN_SCHEMA] = mode
19981998
# A2A agent card is a special case, when running in A2A mode,
19991999
if hasattr(agent_engine, "agent_card"):
2000-
from google.protobuf import json_format
2001-
2002-
class_method[_A2A_AGENT_CARD] = json_format.MessageToJson(
2003-
getattr(agent_engine, "agent_card")
2004-
)
2000+
card = getattr(agent_engine, "agent_card")
2001+
if card is not None:
2002+
class_method[_A2A_AGENT_CARD] = (
2003+
_utils._serialize_agent_card_to_json(card)
2004+
)
20052005
class_methods_spec.append(class_method)
20062006

20072007
return class_methods_spec

0 commit comments

Comments
 (0)