fix(response_format): raise TypeError when json_data is not a dict#1
Open
devteamaegis wants to merge 1 commit into
Open
fix(response_format): raise TypeError when json_data is not a dict#1devteamaegis wants to merge 1 commit into
devteamaegis wants to merge 1 commit into
Conversation
… pydantic_model_from_json pydantic_model.model_validate() requires a mapping; passing a JSON array or scalar decoded from model output raised a cryptic ValidationError. Add an isinstance(json_data, dict) guard and raise TypeError with a descriptive message instead. Fixes mistralai#560
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.
What's broken
When a Mistral model returns well-formed JSON that is not an object — for example a JSON array like
[1, 2, 3]or a bare scalar —convert_to_parsed_chat_completion_responsepasses the decoded value directly topydantic_model.model_validate(). Pydantic requires a mapping, so it raisespydantic_core.ValidationErrorwith the opaque message "Input should be a valid dictionary or instance of ...". There is no hint that the root problem is the model's response shape, not a programming error.Why it happens
pydantic_model_from_jsoninsrc/mistralai/extra/utils/response_format.pycallsmodel_validate(json_data)unconditionally, without checking thatjson_datais adict.Fix
Added
isinstance(json_data, dict)guard inpydantic_model_from_json. If the check fails, aTypeErroris raised with a message that names the model class and the actual type received, making the failure immediately actionable.Test
Added
tests/test_struct_chat.pywith three cases: a list input raisesTypeError, a scalar input raisesTypeError, and a valid dict input produces the correct model instance.Fixes mistralai#560