Skip to content

Conversation

@yashwantbezawada
Copy link

Fixes #279

Problem

The rec_strict_json_schema function raises a ValueError when processing Pydantic models with constrained types like conlist, conset, etc.

These constrained types generate JSON schemas with numeric constraint values (e.g., minItems: 3, maxItems: 5), which are valid JSON schema properties. However, the function only handled strings, booleans, dicts, lists, and None, causing it to fail on these numeric values.

Changes

Updated rec_strict_json_schema to accept int and float as valid primitive types, similar to how it handles str and bool. These values are passed through unchanged during schema processing.

Also updated the test suite to verify that numeric values are handled correctly and added a specific test case for conlist models.

Testing

Verified with the exact reproduction case from the issue:

from pydantic import BaseModel, conlist
from mistralai.extra.utils.response_format import response_format_from_pydantic_model

class ExampleModel(BaseModel):
    words: conlist(str, min_length=3, max_length=3)

response_format = response_format_from_pydantic_model(ExampleModel)

This now works without raising a ValueError.

Resolves mistralai#279

The rec_strict_json_schema function was raising ValueError when
encountering numeric values in JSON schemas. This prevented usage
of Pydantic models with constrained types like conlist, conset, etc.

These constrained types include numeric constraint values (minItems,
maxItems, etc.) in their JSON schemas, which are valid JSON schema
properties that should be preserved during processing.

This change updates the function to treat int and float as valid
primitive types alongside str and bool.
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 CLIENT]: Incompatibility with Pydantic conlist and similar

1 participant