From 9c0721beaa526a4437671e6cc70915073be835e3 Mon Sep 17 00:00:00 2001 From: Holt Skinner Date: Tue, 29 Jul 2025 10:31:00 -0700 Subject: [PATCH] fix: Update `agent_card_builder` to follow grammar rules Merge https://github.com/google/adk-python/pull/2226 Fixes #2223 COPYBARA_INTEGRATE_REVIEW=https://github.com/google/adk-python/pull/2226 from holtskinner:a2a-fixes ff556224e4071a8287a9ced19f645f0edd9916ef PiperOrigin-RevId: 788512608 --- .../adk/a2a/utils/agent_card_builder.py | 26 ++++++++++++++++--- .../a2a/utils/test_agent_card_builder.py | 11 ++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/google/adk/a2a/utils/agent_card_builder.py b/src/google/adk/a2a/utils/agent_card_builder.py index 047f786c..06e0d55e 100644 --- a/src/google/adk/a2a/utils/agent_card_builder.py +++ b/src/google/adk/a2a/utils/agent_card_builder.py @@ -224,7 +224,7 @@ def _build_code_executor_skill(agent: LlmAgent) -> AgentSkill: return AgentSkill( id=f'{agent.name}-code-executor', name='code-execution', - description='Can execute codes', + description='Can execute code', examples=None, input_modes=None, output_modes=None, @@ -359,11 +359,29 @@ def _build_llm_agent_description_with_instructions(agent: LlmAgent) -> str: def _replace_pronouns(text: str) -> str: - """Replace pronouns in text for agent description (you -> I, your -> my, etc.).""" - pronoun_map = {'you': 'I', 'your': 'my', 'yours': 'mine'} + """Replace pronouns and conjugate common verbs for agent description. + (e.g., "You are" -> "I am", "your" -> "my"). + """ + pronoun_map = { + # Longer phrases with verb conjugations + 'you are': 'I am', + 'you were': 'I was', + "you're": 'I am', + "you've": 'I have', + # Standalone pronouns + 'yours': 'mine', + 'your': 'my', + 'you': 'I', + } + + # Sort keys by length (descending) to ensure longer phrases are matched first. + # This prevents "you" in "you are" from being replaced on its own. + sorted_keys = sorted(pronoun_map.keys(), key=len, reverse=True) + + pattern = r'\b(' + '|'.join(re.escape(key) for key in sorted_keys) + r')\b' return re.sub( - r'\b(you|your|yours)\b', + pattern, lambda match: pronoun_map[match.group(1).lower()], text, flags=re.IGNORECASE, diff --git a/tests/unittests/a2a/utils/test_agent_card_builder.py b/tests/unittests/a2a/utils/test_agent_card_builder.py index 964c7188..fb52dd5c 100644 --- a/tests/unittests/a2a/utils/test_agent_card_builder.py +++ b/tests/unittests/a2a/utils/test_agent_card_builder.py @@ -403,6 +403,17 @@ class TestHelperFunctions: # Assert assert result == "youth, yourself, yourname" # No changes + def test_replace_pronouns_phrases(self): + """Test _replace_pronouns with phrases that should be replaced.""" + # Arrange + text = "You are a helpful chatbot" + + # Act + result = _replace_pronouns(text) + + # Assert + assert result == "I am a helpful chatbot" + def test_get_default_description_llm_agent(self): """Test _get_default_description for LlmAgent.""" # Arrange