Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/a2a/utils/input_mode_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ def validate_input_modes(message: Message, agent_card: AgentCard) -> None:

Media types are compared on type and subtype alone, ignoring case
(RFC 2045 section 5.1) and parameters, so `Text/Plain; charset=utf-8`
matches a card that declares `text/plain`.
matches a card that declares `text/plain`. A declared media range
(RFC 9110 section 12.5.1) matches by type: `image/*` accepts `image/png`
but not `video/mp4`, and `*/*` accepts any media type.
"""
modes = set(agent_card.default_input_modes)
for skill in agent_card.skills:
Expand All @@ -33,7 +35,9 @@ def validate_input_modes(message: Message, agent_card: AgentCard) -> None:
return

for part in message.parts:
if part.media_type and _essence(part.media_type) not in declared:
if part.media_type and not _is_declared(
_essence(part.media_type), declared
):
raise ContentTypeNotSupportedError(
message=(
f'Media type {part.media_type} is not supported. '
Expand All @@ -42,6 +46,14 @@ def validate_input_modes(message: Message, agent_card: AgentCard) -> None:
)


def _is_declared(essence: str, declared: set[str]) -> bool:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image and image/ now pass against image/* since the split doesn't check for a subtype. They were rejected before and aren't valid media types. Please only do the range lookup when the part has a non-empty type and subtype, and add a test for it.

"""Returns whether `essence` is declared exactly or by a media range."""
if essence in declared or '*/*' in declared:
return True
media_type = essence.split('/', 1)[0]
return f'{media_type}/*' in declared


def _essence(media_type: str) -> str:
"""Returns `type/subtype` in lower case, without parameters."""
return media_type.split(';', 1)[0].strip().lower()
34 changes: 34 additions & 0 deletions tests/utils/test_input_mode_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,37 @@ def test_parameters_do_not_widen_the_declared_type():
)

assert 'text/html; charset=utf-8' in exc_info.value.message


@pytest.mark.parametrize(
('part_type', 'card_type'),
[
('image/png', 'image/*'),
('image/png; q=1', 'IMAGE/*'),
('Image/PNG', 'image/*;q=0.8'),
('video/mp4', '*/*'),
],
)
def test_declared_media_range_matches_its_type(part_type, card_type):
"""RFC 9110 12.5.1: `type/*` covers every subtype, `*/*` every type."""
validate_input_modes(
make_message(Part(raw=b'x', media_type=part_type)),
make_card('text/plain', card_type),
)


def test_media_range_does_not_widen_to_other_types():
with pytest.raises(ContentTypeNotSupportedError) as exc_info:
validate_input_modes(
make_message(Part(raw=b'x', media_type='video/mp4')),
make_card('text/plain', 'image/*'),
)

assert 'video/mp4' in exc_info.value.message


def test_media_range_declared_by_a_skill_widens_the_union():
validate_input_modes(
make_message(Part(raw=b'x', media_type='audio/wav')),
make_card('text/plain', skill_modes=['audio/*']),
)
Loading