fix: Convert examples for A2A agent card

Merge https://github.com/google/adk-python/pull/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=https://github.com/google/adk-python/pull/3999 from timn:timn/fix-a2a-examples-from-tool 34c727c3311d2ec945efa3eec652d9e28b6ae2a9
PiperOrigin-RevId: 855288587
This commit is contained in:
Tim Niemueller
2026-01-12 10:36:48 -08:00
committed by Copybara-Service
parent 4a34501d38
commit 458d24e24e
2 changed files with 102 additions and 2 deletions
+30 -2
View File
@@ -114,7 +114,7 @@ async def _build_llm_agent_skills(agent: LlmAgent) -> List[AgentSkill]:
id=agent.name,
name='model',
description=agent_description,
examples=agent_examples,
examples=_extract_inputs_from_examples(agent_examples),
input_modes=_get_input_modes(agent),
output_modes=_get_output_modes(agent),
tags=['llm'],
@@ -239,7 +239,7 @@ async def _build_non_llm_agent_skills(agent: BaseAgent) -> List[AgentSkill]:
id=agent.name,
name=agent_name,
description=agent_description,
examples=agent_examples,
examples=_extract_inputs_from_examples(agent_examples),
input_modes=_get_input_modes(agent),
output_modes=_get_output_modes(agent),
tags=[agent_type],
@@ -350,6 +350,7 @@ def _build_llm_agent_description_with_instructions(agent: LlmAgent) -> str:
def _replace_pronouns(text: str) -> str:
"""Replace pronouns and conjugate common verbs for agent description.
(e.g., "You are" -> "I am", "your" -> "my").
"""
pronoun_map = {
@@ -460,6 +461,33 @@ def _get_default_description(agent: BaseAgent) -> str:
return 'A custom agent'
def _extract_inputs_from_examples(examples: Optional[list[dict]]) -> list[str]:
"""Extracts only the input strings so they can be added to an AgentSkill."""
if examples is None:
return []
extracted_inputs = []
for example in examples:
example_input = example.get('input')
if not example_input:
continue
parts = example_input.get('parts')
if parts is not None:
part_texts = []
for part in parts:
text = part.get('text')
if text is not None:
part_texts.append(text)
extracted_inputs.append('\n'.join(part_texts))
else:
text = example_input.get('text')
if text is not None:
extracted_inputs.append(text)
return extracted_inputs
async def _extract_examples_from_agent(
agent: BaseAgent,
) -> Optional[List[Dict]]:
@@ -28,6 +28,7 @@ from google.adk.a2a.utils.agent_card_builder import _build_parallel_description
from google.adk.a2a.utils.agent_card_builder import _build_sequential_description
from google.adk.a2a.utils.agent_card_builder import _convert_example_tool_examples
from google.adk.a2a.utils.agent_card_builder import _extract_examples_from_instruction
from google.adk.a2a.utils.agent_card_builder import _extract_inputs_from_examples
from google.adk.a2a.utils.agent_card_builder import _get_agent_skill_name
from google.adk.a2a.utils.agent_card_builder import _get_agent_type
from google.adk.a2a.utils.agent_card_builder import _get_default_description
@@ -41,6 +42,7 @@ from google.adk.agents.llm_agent import LlmAgent
from google.adk.agents.loop_agent import LoopAgent
from google.adk.agents.parallel_agent import ParallelAgent
from google.adk.agents.sequential_agent import SequentialAgent
from google.adk.examples import Example
from google.adk.tools.example_tool import ExampleTool
import pytest
@@ -1100,3 +1102,73 @@ class TestExampleExtractionFunctions:
assert len(result) == 1 # Only complete pairs should be included
assert result[0]["input"] == {"text": "What is the weather?"}
assert result[0]["output"] == [{"text": "What time is it?"}]
def test_extract_inputs_from_examples_from_plain_text_input(self):
"""Test _extract_inputs_from_examples on plain text as input."""
# Arrange
examples = [
{
"input": {"text": "What is the weather?"},
"output": [{"text": "What time is it?"}],
},
{
"input": {"text": "The weather is sunny."},
"output": [{"text": "It is 3 PM."}],
},
]
# Act
result = _extract_inputs_from_examples(examples)
# Assert
assert len(result) == 2
assert result[0] == "What is the weather?"
assert result[1] == "The weather is sunny."
def test_extract_inputs_from_examples_from_example_tool(self):
"""Test _extract_inputs_from_examples as extracted from ExampleTool."""
# Arrange
# This is what would be extracted from an ExampleTool
examples = [
{
"input": {
"role": "user",
"parts": [{"text": "What is the weather?"}],
},
"output": [
{
"role": "model",
"parts": [{"text": "What time is it?"}],
},
],
},
{
"input": {
"role": "user",
"parts": [{"text": "The weather is sunny."}],
},
"output": [
{
"role": "model",
"parts": [{"text": "It is 3 PM."}],
},
],
},
]
# Act
result = _extract_inputs_from_examples(examples)
# Assert
assert len(result) == 2
assert result[0] == "What is the weather?"
assert result[1] == "The weather is sunny."
def test_extract_inputs_from_examples_none_input(self):
"""Test _extract_inputs_from_examples on None as input."""
# Act
result = _extract_inputs_from_examples(None)
# Assert
assert len(result) == 0