Skip to content

fix(response_format): raise TypeError when json_data is not a dict#1

Open
devteamaegis wants to merge 1 commit into
mainfrom
fix/typeerror-unhandled-validationerror
Open

fix(response_format): raise TypeError when json_data is not a dict#1
devteamaegis wants to merge 1 commit into
mainfrom
fix/typeerror-unhandled-validationerror

Conversation

@devteamaegis
Copy link
Copy Markdown
Owner

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_response passes the decoded value directly to pydantic_model.model_validate(). Pydantic requires a mapping, so it raises pydantic_core.ValidationError with 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_json in src/mistralai/extra/utils/response_format.py calls model_validate(json_data) unconditionally, without checking that json_data is a dict.

Fix

Added isinstance(json_data, dict) guard in pydantic_model_from_json. If the check fails, a TypeError is 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.py with three cases: a list input raises TypeError, a scalar input raises TypeError, and a valid dict input produces the correct model instance.

Fixes mistralai#560

… 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: convert_to_parsed_chat_completion_response crashes with ValidationError when model returns a JSON array or scalar

1 participant