fix(core): allow Image validator to be Union-friendly for multimodal payloads #7175
+54
−5
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?
This PR fixes a critical bug in
UserMessagedeserialization where mixed-media payloads (containing bothstrandImageobjects) cause aTypeErrorduring JSON deserialization (Issue #7170).The Root Cause:
In Pydantic V2, a
TypeErrorraised within a custom validator (specifically inImage.__get_pydantic_core_schema__) is interpreted as a terminal failure during Union resolution. This prevents the resolver from attempting subsequent branches in aUnionorList[Union[...]].The Fix:
By refactoring the
Imagevalidator to raise aValueErrorinstead of aTypeErrorwhen a non-dict/non-Image type is encountered, we enable the Pydantic C-engine to treat the current branch as a mismatch rather than a fatal error. This allows the resolver to successfully fall back to thestrbranch, enabling full support for multimodal message schemas in parity with OpenAI and Gemini API standards.Related issue number
Closes #7170
Checks