Skip to content

Commit 28f8a62

Browse files
fix(server): match input modes on type and subtype, ignoring case (a2aproject#1425)
# Description With `validate_input_modes=True`, a card that declares `text/plain` rejects a part sent as `Text/Plain` or `text/plain; charset=utf-8`, because the check compares exact strings. The [spec](https://github.com/a2aproject/A2A/blob/4735f4876449c6f381825d8a841b31b16c6ad0c4/specification/a2a.proto#L387) defines input modes as media types, RFC 2045 section 5.1 makes type and subtype case-insensitive, and a charset parameter does not change the type. The validator now compares lower-cased `type/subtype` on both sides. The error still quotes what the client sent and what the card declares, and `text/html; charset=utf-8` still fails against `text/plain`. ``` $ uv run pytest tests/utils/test_input_mode_validator.py # 11f1116, fix reverted FAILED ...::test_media_types_match_on_type_and_subtype[Text/Plain-text/plain] FAILED ...::test_media_types_match_on_type_and_subtype[text/plain; charset=utf-8-text/plain] 5 failed, 12 passed in 0.12s $ uv run pytest # 11f1116 plus this branch, Python 3.10 2530 passed, 207 skipped, 4 xfailed, 1 xpassed, 200 warnings in 44.04s $ uv run pytest --cov=src src/a2a/utils/input_mode_validator.py 100% TOTAL 94% $ ./scripts/lint.sh SUCCESS: All linting and formatting tasks complete! # its 3 ruff diagnostics are in client_factory.py and transports/__init__.py, which this does not touch ``` a2aproject/a2a-js#745 still compares exact strings, so the two SDKs differ until it gets the same fix, which I can send there next. @mykytanetipa a card that declares `image/*` still only matches that literal string. I kept wildcards out, since that is a policy call on top of a2aproject#1269 rather than a parsing fix. Would you rather have them handled here? - [x] Follow the [`CONTRIBUTING` Guide](https://github.com/a2aproject/a2a-python/blob/main/CONTRIBUTING.md). - [x] Make your Pull Request title in the <https://www.conventionalcommits.org/> specification. - [x] Ensure the tests and linter pass (Run `bash scripts/format.sh` from the repository root to format) - [x] Appropriate docs were updated (if necessary) - The `validate_input_modes` docstring now states the matching rule. Refs a2aproject#1269. No issue exists, since the tests above reproduce it. Signed-off-by: Christopher Krah <48338417+chrikrah@users.noreply.github.com> Co-authored-by: Mateusz Iwaniuk <45974414+Iwaniukooo11@users.noreply.github.com>
1 parent 58788bb commit 28f8a62

2 files changed

Lines changed: 44 additions & 4 deletions

File tree

‎src/a2a/utils/input_mode_validator.py‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,28 @@ def validate_input_modes(message: Message, agent_card: AgentCard) -> None:
2020
a proto3 string, so a part that omits it arrives as `''` and is likewise
2121
not checked -- only a media type the client actually stated can
2222
contradict the card.
23+
24+
Media types are compared on type and subtype alone, ignoring case
25+
(RFC 2045 section 5.1) and parameters, so `Text/Plain; charset=utf-8`
26+
matches a card that declares `text/plain`.
2327
"""
24-
declared = set(agent_card.default_input_modes)
28+
modes = set(agent_card.default_input_modes)
2529
for skill in agent_card.skills:
26-
declared.update(skill.input_modes)
30+
modes.update(skill.input_modes)
31+
declared = {_essence(mode) for mode in modes}
2732
if not declared:
2833
return
2934

3035
for part in message.parts:
31-
if part.media_type and part.media_type not in declared:
36+
if part.media_type and _essence(part.media_type) not in declared:
3237
raise ContentTypeNotSupportedError(
3338
message=(
3439
f'Media type {part.media_type} is not supported. '
35-
f'Supported input modes: {", ".join(sorted(declared))}'
40+
f'Supported input modes: {", ".join(sorted(modes))}'
3641
)
3742
)
43+
44+
45+
def _essence(media_type: str) -> str:
46+
"""Returns `type/subtype` in lower case, without parameters."""
47+
return media_type.split(';', 1)[0].strip().lower()

‎tests/utils/test_input_mode_validator.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,33 @@ def test_skill_modes_alone_are_enforced_when_the_card_declares_none():
119119
make_message(Part(text='hi', media_type='application/x-nope')),
120120
make_card(skill_modes=['image/png']),
121121
)
122+
123+
124+
@pytest.mark.parametrize(
125+
('part_type', 'card_type'),
126+
[
127+
('Text/Plain', 'text/plain'),
128+
('text/plain', 'TEXT/PLAIN'),
129+
('text/plain; charset=utf-8', 'text/plain'),
130+
('text/plain', 'text/plain;charset=utf-8'),
131+
(' application/json ', 'application/json'),
132+
],
133+
)
134+
def test_media_types_match_on_type_and_subtype(part_type, card_type):
135+
"""RFC 2045 5.1: type and subtype are case-insensitive, parameters are not the type."""
136+
validate_input_modes(
137+
make_message(Part(text='hi', media_type=part_type)),
138+
make_card(card_type),
139+
)
140+
141+
142+
def test_parameters_do_not_widen_the_declared_type():
143+
with pytest.raises(ContentTypeNotSupportedError) as exc_info:
144+
validate_input_modes(
145+
make_message(
146+
Part(text='hi', media_type='text/html; charset=utf-8')
147+
),
148+
make_card('text/plain'),
149+
)
150+
151+
assert 'text/html; charset=utf-8' in exc_info.value.message

0 commit comments

Comments
 (0)