From d490def095db83bb9dcb62fb1b172820ccc3606b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iker=20Le=C3=B1ena?= <91603522+LenenaIker@users.noreply.github.com> Date: Mon, 19 May 2025 11:35:58 +0200 Subject: [PATCH 1/2] Update _pydantic_helper.py This function was giving me an error when using Pydantic objects with Mistral. Now is working properly. --- src/mistralai/extra/utils/_pydantic_helper.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mistralai/extra/utils/_pydantic_helper.py b/src/mistralai/extra/utils/_pydantic_helper.py index 08523f41..4ab800c8 100644 --- a/src/mistralai/extra/utils/_pydantic_helper.py +++ b/src/mistralai/extra/utils/_pydantic_helper.py @@ -15,6 +15,8 @@ def rec_strict_json_schema(schema_node: Any) -> Any: elif isinstance(schema_node, list): for i, value in enumerate(schema_node): schema_node[i] = rec_strict_json_schema(value) + elif not schema_node: + pass else: raise ValueError(f"Unexpected type: {schema_node}") return schema_node From cea2aa955b54a3dbf6bd882023000c683fbab422 Mon Sep 17 00:00:00 2001 From: Alexandre Menasria Date: Thu, 22 May 2025 11:11:57 +0200 Subject: [PATCH 2/2] Fix rec_strict_json_schema for None default fields models --- src/mistralai/extra/utils/_pydantic_helper.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/mistralai/extra/utils/_pydantic_helper.py b/src/mistralai/extra/utils/_pydantic_helper.py index 4ab800c8..f042c394 100644 --- a/src/mistralai/extra/utils/_pydantic_helper.py +++ b/src/mistralai/extra/utils/_pydantic_helper.py @@ -1,11 +1,12 @@ from typing import Any + def rec_strict_json_schema(schema_node: Any) -> Any: """ Recursively set the additionalProperties property to False for all objects in the JSON Schema. This makes the JSON Schema strict (i.e. no additional properties are allowed). """ - if isinstance(schema_node, (str, bool)): + if isinstance(schema_node, (str, bool)) or schema_node is None: return schema_node if isinstance(schema_node, dict): if "type" in schema_node and schema_node["type"] == "object": @@ -15,8 +16,6 @@ def rec_strict_json_schema(schema_node: Any) -> Any: elif isinstance(schema_node, list): for i, value in enumerate(schema_node): schema_node[i] = rec_strict_json_schema(value) - elif not schema_node: - pass else: raise ValueError(f"Unexpected type: {schema_node}") return schema_node