Python: make SerializationMixin.from_dict enforce the documented type check#7256
Open
he-yufeng wants to merge 1 commit into
Open
Python: make SerializationMixin.from_dict enforce the documented type check#7256he-yufeng wants to merge 1 commit into
he-yufeng wants to merge 1 commit into
Conversation
… check
from_dict resolved the expected type identifier from the payload itself
(_get_type_identifier(value) prefers value["type"]), so the mismatch
guard could never fire: any supplied 'type' matched itself, and a payload
like {"type": "function_tool", ...} silently deserialized into a Message,
getting its type rewritten on the next to_dict. The docstring has always
promised a ValueError on mismatch.
Resolve the identifier from the class instead, matching what to_dict
emits, so a mismatched or foreign 'type' now raises as documented.
Payloads without a 'type' field and dependency-injection lookups are
unchanged: in every previously valid case the class-resolved identifier
is the same string the payload carried.
Fixes microsoft#7255
Signed-off-by: Yufeng He <40085740+he-yufeng@users.noreply.github.com>
he-yufeng
temporarily deployed
to
github-app-auth
July 22, 2026 11:01 — with
GitHub Actions
Inactive
he-yufeng
temporarily deployed
to
github-app-auth
July 22, 2026 11:01 — with
GitHub Actions
Inactive
he-yufeng
temporarily deployed
to
github-app-auth
July 22, 2026 11:01 — with
GitHub Actions
Inactive
he-yufeng
temporarily deployed
to
github-app-auth
July 22, 2026 11:02 — with
GitHub Actions
Inactive
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a correctness issue in the Python core serialization layer where SerializationMixin.from_dict() could not actually enforce its documented “type mismatch raises ValueError” behavior, allowing payloads with an incorrect "type" discriminator to deserialize into the wrong class (Fixes #7255).
Changes:
- Make
SerializationMixin.from_dict()derive the expected type identifier from the target class rather than the incoming payload, so the mismatch guard can trigger. - Add regression tests ensuring mismatched
"type"is rejected for bothfrom_dict()andfrom_json().
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| python/packages/core/agent_framework/_serialization.py | Fixes the mismatch check by resolving the expected type identifier from the class instead of the payload. |
| python/packages/core/tests/core/test_serializable_mixin.py | Adds tests verifying mismatched "type" raises for both dict and JSON inputs. |
Comment on lines
543
to
544
| if (supplied_type := value.get("type")) and supplied_type != type_id: | ||
| raise ValueError(f"Type mismatch: expected '{type_id}', got '{supplied_type}'") |
eavanvalkenburg
approved these changes
Jul 22, 2026
Contributor
he-yufeng
temporarily deployed
to
github-app-auth
July 22, 2026 14:19 — with
GitHub Actions
Inactive
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #7255.
Problem
SerializationMixin.from_dictdocuments that a mismatchedtypefield raisesValueError, but the guard could never fire: it resolved the expected identifier viacls._get_type_identifier(value), and that helper prefers the payload's owntypefield when one is present. Any suppliedtypetherefore "matched" itself, and a payload like{"type": "function_tool", "role": "user", "contents": []}silently deserialized into aMessage, with the wrong type baked in until the nextto_dict()rewrote it.Fix
Resolve the expected identifier from the class (no payload), which is also what
to_dict()writes, so the mismatch guard now works as documented:In every previously valid case the class-resolved identifier is the same string the payload carried, so well-formed payloads (including no-
typepayloads anddependencies=injection, which keys on the same id) are unchanged. Classes that do polymorphic deserialization (Content, the file-access harness entries) overridefrom_dictthemselves and are unaffected.Tests
Two new tests in
test_serializable_mixin.py: mismatchedtyperaises on bothfrom_dictandfrom_json. Full file passes (28/28).pytest packages/core/tests/coreis green excepttest_feature_stage.py::test_feature_id_allows_lowercase_values, which fails identically on a cleanmaincheckout (pre-existing, unrelated). The exact repro from the issue now raisesType mismatch: expected 'message', got 'function_tool'.