Skip to content

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

@devteamaegis

Description

@devteamaegis

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() without checking that it is a dict. Pydantic's model_validate requires a mapping when validating a model class, so it raises pydantic_core.ValidationError with the unhelpful message "Input should be a valid dictionary or instance of ...".

Repro:

from mistralai.extra.struct_chat import convert_to_parsed_chat_completion_response
from mistralai.client.models import ChatCompletionResponse, ChatCompletionChoice, AssistantMessage, UsageInfo
from pydantic import BaseModel

class Item(BaseModel):
    name: str

usage = UsageInfo(prompt_tokens=5, completion_tokens=10, total_tokens=15)
msg = AssistantMessage(role='assistant', content='[1, 2, 3]')
choice = ChatCompletionChoice(index=0, message=msg, finish_reason='stop')
response = ChatCompletionResponse(id='x', object='chat.completion', model='mistral-7b', created=0, choices=[choice], usage=usage)
convert_to_parsed_chat_completion_response(response, Item)

Traceback:

File "src/mistralai/extra/utils/response_format.py", line 37, in pydantic_model_from_json
    return pydantic_model.model_validate(json_data)
pydantic_core._pydantic_core.ValidationError: 1 validation error for Item
  Input should be a valid dictionary or instance of Item [type=model_type, input_value=[1, 2, 3], input_type=list]

The root cause is in pydantic_model_from_json in src/mistralai/extra/utils/response_format.py: it calls model_validate unconditionally without verifying json_data is a dict. The fix is to add an isinstance(json_data, dict) check and raise a descriptive TypeError if the check fails.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions