Skip to content

Conversation

@yurekami
Copy link

Summary

Problem

When using response_format_from_pydantic_model() with Pydantic models that have constraint keywords (like Field(min_length=1)), the rec_strict_json_schema() function was raising a ValueError because it only considered str and bool as valid terminal types, but JSON Schema constraint keywords have numeric values (int or float).

Example that failed:

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

class ModelWithConstraints(BaseModel):
    items: list[int] = Field(min_length=1)
    name: str = Field(min_length=1, max_length=100)

# This raised ValueError: Unexpected type: 1
response_format = response_format_from_pydantic_model(ModelWithConstraints)

Solution

Added int and float to the type check in rec_strict_json_schema():

if isinstance(schema_node, (str, bool, int, float)) or schema_node is None:
    return schema_node

Test plan

  • Added test for rec_strict_json_schema with numeric constraints (minItems, maxItems, minLength, maxLength, minimum, maximum, multipleOf)
  • Added test for response_format_from_pydantic_model with constrained Pydantic models
  • Added test to verify invalid types still raise ValueError

Fixes #300

🤖 Generated with Claude Code

The rec_strict_json_schema function now handles int and float values
as terminal types. This fixes issue mistralai#300 where Pydantic models with
constraint keywords like min_length, max_length, minItems, maxItems,
minimum, maximum, and multipleOf would cause a ValueError.

JSON Schema constraint keywords have numeric values (int or float),
and these should be preserved without recursion, just like str and bool.

Fixes mistralai#300

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
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]: response_format_from_pydantic_model can't accept integer keyword

1 participant