fix(extra): Field examples attr raises ValueError when give int#289
Open
Athroniaeth wants to merge 1 commit intomistralai:mainfrom
Open
fix(extra): Field examples attr raises ValueError when give int#289Athroniaeth wants to merge 1 commit intomistralai:mainfrom
Athroniaeth wants to merge 1 commit intomistralai:mainfrom
Conversation
I use pydantic models and the examples attribute of the Field descriptor in my structured output projects whenever possible. I noticed in the Mistral documentation that there was a function for obtaining a schema, and I wanted to test it to compare the one I had made with yours, and I noticed that I had a strange error.
It seems that the `int` was omitted from what I assume to be the list of primary types in the `rec_strict_json_schema` function.
Here's how to reproduce the error:
```python
from mistralai.extra import response_format_from_pydantic_model
from pydantic import BaseModel, Field
class Pass1(BaseModel):
string: int = Field(...)
boolean: bool = Field(...)
number: int = Field(...)
class Pass2(BaseModel):
string: int = Field(..., examples=["example1", "example2"])
boolean: bool = Field(..., examples=[True, False])
number: int = Field(...)
class Failed(BaseModel):
string: int = Field(..., examples=["example1", "example2"])
boolean: bool = Field(..., examples=[True, False])
number: int = Field(..., examples=[42, 7, 100])
print(response_format_from_pydantic_model(Pass1))
print(response_format_from_pydantic_model(Pass2))
print(response_format_from_pydantic_model(Failed))
```
The result :
```
uv run /home/.../PycharmProjects/.../.venv/bin/python /home/.../PycharmProjects/.../benchmarks/format.py
type='json_schema' json_schema=JSONSchema(name='Pass1', schema_definition={'properties': {'string': {'title': 'String', 'type': 'integer'}, 'boolean': {'title': 'Boolean', 'type': 'boolean'}, 'number': {'title': 'Number', 'type': 'integer'}}, 'required': ['string', 'boolean', 'number'], 'title': 'Pass1', 'type': 'object', 'additionalProperties': False}, description=Unset(), strict=True)
type='json_schema' json_schema=JSONSchema(name='Pass2', schema_definition={'properties': {'string': {'examples': ['example1', 'example2'], 'title': 'String', 'type': 'integer'}, 'boolean': {'examples': [True, False], 'title': 'Boolean', 'type': 'boolean'}, 'number': {'title': 'Number', 'type': 'integer'}}, 'required': ['string', 'boolean', 'number'], 'title': 'Pass2', 'type': 'object', 'additionalProperties': False}, description=Unset(), strict=True)
Traceback (most recent call last):
File "/home/.../PycharmProjects/.../benchmarks/format.py", line 24, in <module>
print(response_format_from_pydantic_model(Failed))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/.../PycharmProjects/.../.venv/lib/python3.12/site-packages/mistralai/extra/utils/response_format.py", line 13, in response_format_from_pydantic_model
model_schema = rec_strict_json_schema(model.model_json_schema())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/.../PycharmProjects/.../.venv/lib/python3.12/site-packages/mistralai/extra/utils/_pydantic_helper.py", line 15, in rec_strict_json_schema
schema_node[key] = rec_strict_json_schema(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/.../PycharmProjects/.../.venv/lib/python3.12/site-packages/mistralai/extra/utils/_pydantic_helper.py", line 15, in rec_strict_json_schema
schema_node[key] = rec_strict_json_schema(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/.../PycharmProjects/.../.venv/lib/python3.12/site-packages/mistralai/extra/utils/_pydantic_helper.py", line 15, in rec_strict_json_schema
schema_node[key] = rec_strict_json_schema(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/.../PycharmProjects/.../.venv/lib/python3.12/site-packages/mistralai/extra/utils/_pydantic_helper.py", line 18, in rec_strict_json_schema
schema_node[i] = rec_strict_json_schema(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/.../PycharmProjects/.../.venv/lib/python3.12/site-packages/mistralai/extra/utils/_pydantic_helper.py", line 20, in rec_strict_json_schema
raise ValueError(f"Unexpected type: {schema_node}")
ValueError: Unexpected type: 42
Process finished with exit code 1
```
The result after patchfix :
```
uv run /home/.../PycharmProjects/.../.venv/bin/python /home/.../PycharmProjects/.../benchmarks/format.py
type='json_schema' json_schema=JSONSchema(name='Pass1', schema_definition={'properties': {'string': {'title': 'String', 'type': 'integer'}, 'boolean': {'title': 'Boolean', 'type': 'boolean'}, 'number': {'title': 'Number', 'type': 'integer'}}, 'required': ['string', 'boolean', 'number'], 'title': 'Pass1', 'type': 'object', 'additionalProperties': False}, description=Unset(), strict=True)
type='json_schema' json_schema=JSONSchema(name='Pass2', schema_definition={'properties': {'string': {'examples': ['example1', 'example2'], 'title': 'String', 'type': 'integer'}, 'boolean': {'examples': [True, False], 'title': 'Boolean', 'type': 'boolean'}, 'number': {'title': 'Number', 'type': 'integer'}}, 'required': ['string', 'boolean', 'number'], 'title': 'Pass2', 'type': 'object', 'additionalProperties': False}, description=Unset(), strict=True)
type='json_schema' json_schema=JSONSchema(name='Failed', schema_definition={'properties': {'string': {'examples': ['example1', 'example2'], 'title': 'String', 'type': 'integer'}, 'boolean': {'examples': [True, False], 'title': 'Boolean', 'type': 'boolean'}, 'number': {'examples': [42, 7, 100], 'title': 'Number', 'type': 'integer'}}, 'required': ['string', 'boolean', 'number'], 'title': 'Failed', 'type': 'object', 'additionalProperties': False}, description=Unset(), strict=True)
Process finished with exit code 0
```
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.
I use pydantic models and the examples attribute of the Field descriptor in my structured output projects whenever possible. I noticed in the Mistral documentation that there was a function for obtaining a schema, and I wanted to test it to compare the one I had made with yours, and I noticed that I had a strange error.
It seems that the
intwas omitted from what I assume to be the list of primary types in therec_strict_json_schemafunction.Here's how to reproduce the error:
The result :
The result after patchfix :