Bug fixes
This commit is contained in:
parent
10feb2d067
commit
bd7a0586b6
1 changed files with 117 additions and 8 deletions
20
great_ai/helper/numpy_to_python.py
Normal file
20
great_ai/helper/numpy_to_python.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
from typing import Any
|
||||
|
||||
import numpy as np
|
||||
from pydantic_core import PydanticSerializationError
|
||||
|
||||
|
||||
def numpy_to_python(value: Any) -> Any:
|
||||
"""Convert NumPy values encountered by Pydantic's JSON serializer."""
|
||||
if isinstance(value, np.ndarray):
|
||||
return value.tolist()
|
||||
if isinstance(value, np.floating):
|
||||
return float(value)
|
||||
if isinstance(value, np.generic):
|
||||
converted = value.item()
|
||||
if not isinstance(converted, type(value)):
|
||||
return converted
|
||||
raise PydanticSerializationError(
|
||||
f"Unable to serialize {type(value).__name__}; convert it to a JSON-compatible "
|
||||
"Python value before returning or logging it."
|
||||
)
|
||||
Loading…
Reference in a new issue