Skip to content

Commit 458d24e

Browse files
timnseanzhougoogle
authored andcommitted
fix: Convert examples for A2A agent card
Merge #3999 The AgentSkill in an A2A AgentCard expects examples to be a list of queries as strings. Therefore, agent examples, e.g., as provided by an ExampleTool, must be converted. This change performs that extraction of just the inputs and converting them to a string to add to the AgentSkill. ### Testing Plan **Unit Tests:** - [x] I have added or updated unit tests for my change. - [x] All unit tests pass locally. **Manual End-to-End (E2E) Tests:** Create an agent with ExampleTool and use agent card builder to create agent card for that agent. Fails without this change, succeeds with change included. ### Checklist - [x] I have read the [CONTRIBUTING.md](https://github.com/google/adk-python/blob/main/CONTRIBUTING.md) document. - [x] I have performed a self-review of my own code. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have added tests that prove my fix is effective or that my feature works. - [x] New and existing unit tests pass locally with my changes. - [x] I have manually tested my changes end-to-end. - [x] Any dependent changes have been merged and published in downstream modules. Co-authored-by: Xiang (Sean) Zhou <seanzhougoogle@google.com> COPYBARA_INTEGRATE_REVIEW=#3999 from timn:timn/fix-a2a-examples-from-tool 34c727c PiperOrigin-RevId: 855288587
1 parent 4a34501 commit 458d24e

File tree

2 files changed

+102
-2
lines changed

2 files changed

+102
-2
lines changed

src/google/adk/a2a/utils/agent_card_builder.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ async def _build_llm_agent_skills(agent: LlmAgent) -> List[AgentSkill]:
114114
id=agent.name,
115115
name='model',
116116
description=agent_description,
117-
examples=agent_examples,
117+
examples=_extract_inputs_from_examples(agent_examples),
118118
input_modes=_get_input_modes(agent),
119119
output_modes=_get_output_modes(agent),
120120
tags=['llm'],
@@ -239,7 +239,7 @@ async def _build_non_llm_agent_skills(agent: BaseAgent) -> List[AgentSkill]:
239239
id=agent.name,
240240
name=agent_name,
241241
description=agent_description,
242-
examples=agent_examples,
242+
examples=_extract_inputs_from_examples(agent_examples),
243243
input_modes=_get_input_modes(agent),
244244
output_modes=_get_output_modes(agent),
245245
tags=[agent_type],
@@ -350,6 +350,7 @@ def _build_llm_agent_description_with_instructions(agent: LlmAgent) -> str:
350350

351351
def _replace_pronouns(text: str) -> str:
352352
"""Replace pronouns and conjugate common verbs for agent description.
353+
353354
(e.g., "You are" -> "I am", "your" -> "my").
354355
"""
355356
pronoun_map = {
@@ -460,6 +461,33 @@ def _get_default_description(agent: BaseAgent) -> str:
460461
return 'A custom agent'
461462

462463

464+
def _extract_inputs_from_examples(examples: Optional[list[dict]]) -> list[str]:
465+
"""Extracts only the input strings so they can be added to an AgentSkill."""
466+
if examples is None:
467+
return []
468+
469+
extracted_inputs = []
470+
for example in examples:
471+
example_input = example.get('input')
472+
if not example_input:
473+
continue
474+
475+
parts = example_input.get('parts')
476+
if parts is not None:
477+
part_texts = []
478+
for part in parts:
479+
text = part.get('text')
480+
if text is not None:
481+
part_texts.append(text)
482+
extracted_inputs.append('\n'.join(part_texts))
483+
else:
484+
text = example_input.get('text')
485+
if text is not None:
486+
extracted_inputs.append(text)
487+
488+
return extracted_inputs
489+
490+
463491
async def _extract_examples_from_agent(
464492
agent: BaseAgent,
465493
) -> Optional[List[Dict]]:

tests/unittests/a2a/utils/test_agent_card_builder.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from google.adk.a2a.utils.agent_card_builder import _build_sequential_description
2929
from google.adk.a2a.utils.agent_card_builder import _convert_example_tool_examples
3030
from google.adk.a2a.utils.agent_card_builder import _extract_examples_from_instruction
31+
from google.adk.a2a.utils.agent_card_builder import _extract_inputs_from_examples
3132
from google.adk.a2a.utils.agent_card_builder import _get_agent_skill_name
3233
from google.adk.a2a.utils.agent_card_builder import _get_agent_type
3334
from google.adk.a2a.utils.agent_card_builder import _get_default_description
@@ -41,6 +42,7 @@
4142
from google.adk.agents.loop_agent import LoopAgent
4243
from google.adk.agents.parallel_agent import ParallelAgent
4344
from google.adk.agents.sequential_agent import SequentialAgent
45+
from google.adk.examples import Example
4446
from google.adk.tools.example_tool import ExampleTool
4547
import pytest
4648

@@ -1100,3 +1102,73 @@ def test_extract_examples_from_instruction_odd_number_of_matches(self):
11001102
assert len(result) == 1 # Only complete pairs should be included
11011103
assert result[0]["input"] == {"text": "What is the weather?"}
11021104
assert result[0]["output"] == [{"text": "What time is it?"}]
1105+
1106+
def test_extract_inputs_from_examples_from_plain_text_input(self):
1107+
"""Test _extract_inputs_from_examples on plain text as input."""
1108+
# Arrange
1109+
examples = [
1110+
{
1111+
"input": {"text": "What is the weather?"},
1112+
"output": [{"text": "What time is it?"}],
1113+
},
1114+
{
1115+
"input": {"text": "The weather is sunny."},
1116+
"output": [{"text": "It is 3 PM."}],
1117+
},
1118+
]
1119+
1120+
# Act
1121+
result = _extract_inputs_from_examples(examples)
1122+
1123+
# Assert
1124+
assert len(result) == 2
1125+
assert result[0] == "What is the weather?"
1126+
assert result[1] == "The weather is sunny."
1127+
1128+
def test_extract_inputs_from_examples_from_example_tool(self):
1129+
"""Test _extract_inputs_from_examples as extracted from ExampleTool."""
1130+
1131+
# Arrange
1132+
# This is what would be extracted from an ExampleTool
1133+
examples = [
1134+
{
1135+
"input": {
1136+
"role": "user",
1137+
"parts": [{"text": "What is the weather?"}],
1138+
},
1139+
"output": [
1140+
{
1141+
"role": "model",
1142+
"parts": [{"text": "What time is it?"}],
1143+
},
1144+
],
1145+
},
1146+
{
1147+
"input": {
1148+
"role": "user",
1149+
"parts": [{"text": "The weather is sunny."}],
1150+
},
1151+
"output": [
1152+
{
1153+
"role": "model",
1154+
"parts": [{"text": "It is 3 PM."}],
1155+
},
1156+
],
1157+
},
1158+
]
1159+
1160+
# Act
1161+
result = _extract_inputs_from_examples(examples)
1162+
1163+
# Assert
1164+
assert len(result) == 2
1165+
assert result[0] == "What is the weather?"
1166+
assert result[1] == "The weather is sunny."
1167+
1168+
def test_extract_inputs_from_examples_none_input(self):
1169+
"""Test _extract_inputs_from_examples on None as input."""
1170+
# Act
1171+
result = _extract_inputs_from_examples(None)
1172+
1173+
# Assert
1174+
assert len(result) == 0

0 commit comments

Comments
 (0)