-
Notifications
You must be signed in to change notification settings - Fork 88
Auto entity deserialization #544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Rodrigo Brandão (rodrigobr-msft)
merged 3 commits into
main
from
users/robrandao/entity-handling
Aug 18, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...es/microsoft-agents-activity/microsoft_agents/activity/entity/_validate_known_entities.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
|
|
||
| from typing import Any | ||
|
|
||
| from .activity_treatment import ActivityTreatment | ||
| from .ai_entity import AIEntity | ||
| from .entity import Entity | ||
| from .entity_types import EntityTypes | ||
| from .geo_coordinates import GeoCoordinates | ||
| from .mention import Mention | ||
| from .place import Place | ||
| from .product_info import ProductInfo | ||
| from .stream_info import StreamInfo | ||
| from .thing import Thing | ||
|
|
||
| _KNOWN_ENTITY_TYPES: dict[str, tuple[str, type[Entity]]] = { | ||
| EntityTypes.ACTIVITY_TREATMENT.value.casefold(): ( | ||
| EntityTypes.ACTIVITY_TREATMENT.value, | ||
| ActivityTreatment, | ||
| ), | ||
| EntityTypes.AI_CITATION.value.casefold(): ( | ||
| EntityTypes.AI_CITATION.value, | ||
| AIEntity, | ||
| ), | ||
| EntityTypes.GEO_COORDINATES.value.casefold(): ( | ||
| EntityTypes.GEO_COORDINATES.value, | ||
| GeoCoordinates, | ||
| ), | ||
| EntityTypes.MENTION.value.casefold(): (EntityTypes.MENTION.value, Mention), | ||
| EntityTypes.PLACE.value.casefold(): (EntityTypes.PLACE.value, Place), | ||
| EntityTypes.PRODUCT_INFO.value.casefold(): ( | ||
| EntityTypes.PRODUCT_INFO.value, | ||
| ProductInfo, | ||
| ), | ||
| EntityTypes.STREAM_INFO.value.casefold(): ( | ||
| EntityTypes.STREAM_INFO.value, | ||
| StreamInfo, | ||
| ), | ||
| EntityTypes.THING.value.casefold(): (EntityTypes.THING.value, Thing), | ||
| } | ||
|
|
||
|
|
||
| def _validate_known_entities(entities: Any) -> list[Entity]: | ||
| """Deserialize known activity entities while preserving unknown entity types. | ||
|
|
||
| :param entities: The data to validate and deserialize into known entity types. | ||
| :returns: A list of validated entities, with known types deserialized into their respective classes | ||
| :raises ValueError: If the input is not a list or tuple, or if an entity is not a dict or Entity instance. | ||
| """ | ||
| if entities is None: | ||
| return [] | ||
|
|
||
| if not isinstance(entities, (list, tuple)): | ||
| raise ValueError("entities must be a list or tuple") | ||
|
|
||
| validated_entities: list[Entity] = [] | ||
| for entity in entities: | ||
| if isinstance(entity, Entity): | ||
| validated_entities.append(entity) | ||
| continue | ||
|
|
||
| if not isinstance(entity, dict): | ||
| raise ValueError(f"entity must be a dict or Entity, got {type(entity)}") | ||
|
|
||
| entity_type = entity.get("type") | ||
| if not entity_type: | ||
| raise ValueError( | ||
| "entity must have a 'type' field. Cannot infer entity type from data." | ||
| ) | ||
|
|
||
| known_entity = ( | ||
| _KNOWN_ENTITY_TYPES.get(entity_type.casefold()) | ||
| if isinstance(entity_type, str) | ||
| else None | ||
| ) | ||
| if known_entity: | ||
| canonical_type, entity_cls = known_entity | ||
| entity = {**entity, "type": canonical_type} | ||
| else: | ||
| entity_cls = Entity | ||
|
|
||
| validated_entities.append(entity_cls.model_validate(entity)) | ||
|
|
||
| return validated_entities |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,252 @@ | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
|
|
||
| import pytest | ||
| from pydantic import ValidationError | ||
|
|
||
| from microsoft_agents.activity import ( | ||
| Activity, | ||
| ActivityTreatment, | ||
| AIEntity, | ||
| Entity, | ||
| GeoCoordinates, | ||
| Mention, | ||
| Place, | ||
| ProductInfo, | ||
| StreamInfo, | ||
| Thing, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("entity_data", "expected_type"), | ||
| [ | ||
| ( | ||
| {"type": "activityTreatment", "treatment": "targeted"}, | ||
| ActivityTreatment, | ||
| ), | ||
| ({"type": "GeoCoordinates", "latitude": 47.6}, GeoCoordinates), | ||
| ({"type": "mention", "text": "Ada"}, Mention), | ||
| ({"type": "Place", "name": "Seattle"}, Place), | ||
| ({"type": "ProductInfo", "id": "copilot"}, ProductInfo), | ||
| ({"type": "streaminfo", "streamSequence": 1}, StreamInfo), | ||
| ({"type": "Thing", "name": "Document"}, Thing), | ||
| ( | ||
| { | ||
| "type": "https://schema.org/Message", | ||
| "@type": "Message", | ||
| "@context": "https://schema.org", | ||
| }, | ||
| AIEntity, | ||
| ), | ||
| ], | ||
| ) | ||
| def test_activity_deserializes_known_entity_types(entity_data, expected_type): | ||
| activity = Activity.model_validate( | ||
| { | ||
| "type": "message", | ||
| "entities": [entity_data], | ||
| } | ||
| ) | ||
|
|
||
| assert isinstance(activity.entities[0], expected_type) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("entity_data", "expected_type", "canonical_type"), | ||
| [ | ||
| ({"type": "MENTION"}, Mention, "mention"), | ||
| ( | ||
| {"type": "ACTIVITYTREATMENT", "treatment": "targeted"}, | ||
| ActivityTreatment, | ||
| "activityTreatment", | ||
| ), | ||
| ({"type": "PRODUCTINFO", "id": "copilot"}, ProductInfo, "ProductInfo"), | ||
| ({"type": "STREAMINFO", "streamSequence": 1}, StreamInfo, "streaminfo"), | ||
| ( | ||
| {"type": "HTTPS://SCHEMA.ORG/MESSAGE"}, | ||
| AIEntity, | ||
| "https://schema.org/Message", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_activity_matches_known_entity_types_case_insensitively( | ||
| entity_data, expected_type, canonical_type | ||
| ): | ||
| activity = Activity.model_validate( | ||
| { | ||
| "type": "message", | ||
| "entities": [entity_data], | ||
| } | ||
| ) | ||
|
|
||
| assert isinstance(activity.entities[0], expected_type) | ||
| assert activity.entities[0].type == canonical_type | ||
|
|
||
|
|
||
| def test_activity_preserves_unknown_entity_types(): | ||
| activity = Activity.model_validate( | ||
| { | ||
| "type": "message", | ||
| "entities": [ | ||
| { | ||
| "type": "customEntity", | ||
| "customValue": "value", | ||
| } | ||
| ], | ||
| } | ||
| ) | ||
|
|
||
| entity = activity.entities[0] | ||
| assert type(entity) is Entity | ||
| assert entity.additional_properties == {"custom_value": "value"} | ||
|
|
||
|
|
||
| def test_activity_deserializes_schema_message_as_ai_entity_without_additional_type(): | ||
| activity = Activity.model_validate( | ||
| { | ||
| "type": "message", | ||
| "entities": [ | ||
| { | ||
| "type": "https://schema.org/Message", | ||
| "@type": "Message", | ||
| "@context": "https://schema.org", | ||
| } | ||
| ], | ||
| } | ||
| ) | ||
|
|
||
| assert isinstance(activity.entities[0], AIEntity) | ||
| assert activity.entities[0].additional_type == ["AIGeneratedContent"] | ||
|
|
||
|
|
||
| def test_activity_preserves_existing_entity_instances(): | ||
| mention = Mention(text="Ada") | ||
|
|
||
| activity = Activity(type="message", entities=[mention]) | ||
|
|
||
| assert activity.entities[0] is mention | ||
|
|
||
|
|
||
| def test_activity_normalizes_null_entities_to_empty_list(): | ||
| activity = Activity.model_validate( | ||
| { | ||
| "type": "message", | ||
| "entities": None, | ||
| } | ||
| ) | ||
|
|
||
| assert activity.entities == [] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("entities", ["mention", {"type": "mention"}]) | ||
| def test_activity_rejects_invalid_entities_collection(entities): | ||
| with pytest.raises(ValidationError, match="entities must be a list or tuple"): | ||
| Activity.model_validate( | ||
| { | ||
| "type": "message", | ||
| "entities": entities, | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("entity", [None, "mention", 1]) | ||
| def test_activity_rejects_invalid_entity_items(entity): | ||
| with pytest.raises(ValidationError, match="entity must be a dict or Entity"): | ||
| Activity.model_validate( | ||
| { | ||
| "type": "message", | ||
| "entities": [entity], | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("entity", [{}, {"text": "Ada"}, {"type": ""}]) | ||
| def test_activity_rejects_entities_without_type(entity): | ||
| with pytest.raises(ValidationError, match="entity must have a 'type' field"): | ||
| Activity.model_validate( | ||
| { | ||
| "type": "message", | ||
| "entities": [entity], | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| def test_activity_accepts_entity_tuple(): | ||
| activity = Activity.model_validate( | ||
| { | ||
| "type": "message", | ||
| "entities": ({"type": "mention", "text": "Ada"},), | ||
| } | ||
| ) | ||
|
|
||
| assert activity.entities == [Mention(text="Ada")] | ||
|
|
||
|
|
||
| def test_activity_rejects_invalid_known_entity(): | ||
| with pytest.raises(ValidationError): | ||
| Activity.model_validate( | ||
| { | ||
| "type": "message", | ||
| "entities": [{"type": "activityTreatment"}], | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| def test_activity_entities_round_trip_preserves_typed_models(): | ||
| activity = Activity.model_validate( | ||
| { | ||
| "type": "message", | ||
| "channelId": "msteams:copilot", | ||
| "entities": [ | ||
| {"type": "activityTreatment", "treatment": "targeted"}, | ||
| { | ||
| "type": "https://schema.org/Message", | ||
| "@type": "Message", | ||
| "@context": "https://schema.org", | ||
| "citation": [ | ||
| { | ||
| "@type": "Claim", | ||
| "position": 1, | ||
| "appearance": {"name": "SDK documentation"}, | ||
| } | ||
| ], | ||
| }, | ||
| { | ||
| "type": "GeoCoordinates", | ||
| "latitude": 47.6, | ||
| "longitude": -122.3, | ||
| }, | ||
| { | ||
| "type": "mention", | ||
| "mentioned": {"id": "user-id", "name": "Ada"}, | ||
| "text": "<at>Ada</at>", | ||
| }, | ||
| {"type": "Place", "name": "Seattle"}, | ||
| {"type": "ProductInfo", "id": "copilot"}, | ||
| { | ||
| "type": "streaminfo", | ||
| "streamSequence": 1, | ||
| "streamId": "stream-id", | ||
| }, | ||
| {"type": "Thing", "name": "Document"}, | ||
| {"type": "customEntity", "customValue": "value"}, | ||
| ], | ||
| } | ||
| ) | ||
|
|
||
| serialized = activity.model_dump(mode="json", by_alias=True, exclude_none=True) | ||
| round_tripped = Activity.model_validate(serialized) | ||
|
|
||
| assert round_tripped == activity | ||
| assert [type(entity) for entity in round_tripped.entities] == [ | ||
| ActivityTreatment, | ||
| AIEntity, | ||
| GeoCoordinates, | ||
| Mention, | ||
| Place, | ||
| ProductInfo, | ||
| StreamInfo, | ||
| Thing, | ||
| Entity, | ||
| ] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.