fix: Image Pydantic schema to properly handle Union[str, Image] deserialization#7195
Open
Sean-Kenneth-Doherty wants to merge 1 commit into
Conversation
Author
|
@microsoft-github-policy-service agree |
86240bc to
b5ffb62
Compare
When UserMessage.content contains mixed Image and str items, Pydantic tries Image validation before the string branch. The previous any_schema validator raised TypeError for strings, preventing Union[str, Image] fallback. Use a narrow Image schema that accepts Image instances or serialized dicts, and add a regression to ensure mixed text/image UserMessage content round-trips through JSON in both orders. Fixes microsoft#7170.
b5ffb62 to
a28d655
Compare
Author
|
Refreshed this PR today and tightened the patch:
Validation run locally:
GitHub checks are also passing: GitGuardian Security Checks and license/cla. |
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.
Why are these changes needed?
Fixes #7170 -
UserMessageJSON deserialization fails whencontentcontains both text andImageitems.Problem
When
UserMessage.contentcontains mixed content such as[image, "describe this"], deserialization fails with:The
Imagepydantic schema usedcore_schema.any_schema(), so the Image validator accepted the string candidate fromUnion[str, Image]and raised before Pydantic could fall through to thestrbranch.Solution
Make
Imagevalidation narrow and Union-friendly:Imageinstancesdatakey through the existing base64 pathThis keeps strings out of the Image validator and lets
Union[str, Image]choose the string branch normally.Related issue number
Closes #7170
Checks
Testing
Local checks:
uv run --package autogen-core pytest packages/autogen-core/tests/test_serialization.py- 15 passeduv run --package autogen-core ruff check packages/autogen-core/src/autogen_core/_image.py packages/autogen-core/tests/test_serialization.py- passeduv run --package autogen-core mypy --config-file ../../pyproject.toml src/autogen_core/_image.py tests/test_serialization.py- passedHosted checks: