You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm encountering an issue with the _serialize_json method when using SQLModel in conjunction with the Langsmith library. The problem arises when the model_dump() method is called on an SQLModel class rather than an instance, leading to a TypeError due to the missing required self argument.
Steps to Reproduce:
Create an SQLModel class.
Attempt to serialize an SQLModel class or instance using the _serialize_json function.
If an SQLModel class (not an instance) is passed, the error is raised.
Error logs:
Failed to use model_dump to serialize <class 'sqlmodel.main.SQLModelMetaclass'> to JSON: TypeError("SQLModel.model_dump() missing 1 required positional argument: 'self'")
Failed to use dict to serialize <class 'sqlmodel.main.SQLModelMetaclass'> to JSON: TypeError("SQLModel.dict() missing 1 required positional argument: 'self'")
...
This occurs because the _serialize_json method attempts to call model_dump() on an SQLModelMetaclass rather than an instance of the SQLModel.
Suggestion:
Potential Solution: Modify the _serialize_json method to ensure that model_dump() is only called on instances of SQLModel, not the class itself. Here's a possible update to prevent this issue:
`def _serialize_json(obj: Any) -> Any:
try:
if isinstance(obj, (set, tuple)):
if hasattr(obj, "_asdict") and callable(obj._asdict):
# NamedTuple
return obj._asdict()
return list(obj)
# Ensure we are working with an instance, not a class
if isinstance(obj, type):
return repr(obj)
serialization_methods = [
("model_dump", True), # Pydantic V2 with non-serializable fields
("dict", False), # Pydantic V1 with non-serializable fields
("to_dict", False), # dataclasses-json
]
for attr, exclude_none in serialization_methods:
if hasattr(obj, attr) and callable(getattr(obj, attr)):
try:
method = getattr(obj, attr)
return (
method(exclude_none=exclude_none) if exclude_none else method()
)
except Exception as e:
logger.error(
f"Failed to use {attr} to serialize {type(obj)} to"
f" JSON: {repr(e)}"
)
pass
return _simple_default(obj)
except BaseException as e:
logger.debug(f"Failed to serialize {type(obj)} to JSON: {e}")
return repr(obj)
`
This modification checks whether the object passed to _serialize_json is a class or an instance before attempting to call model_dump.
Environment:
Python Version: [please provide]
SQLModel Version: [please provide]
Langsmith Version: [please provide]
Let me know if any other information is needed!
The text was updated successfully, but these errors were encountered:
Issue you'd like to raise.
Langsmith Version: 0.1.130
SQLModel Version: 0.0.22
Description:
I'm encountering an issue with the _serialize_json method when using SQLModel in conjunction with the Langsmith library. The problem arises when the model_dump() method is called on an SQLModel class rather than an instance, leading to a TypeError due to the missing required self argument.
Steps to Reproduce:
Create an SQLModel class.
Attempt to serialize an SQLModel class or instance using the _serialize_json function.
If an SQLModel class (not an instance) is passed, the error is raised.
Error logs:
Failed to use model_dump to serialize <class 'sqlmodel.main.SQLModelMetaclass'> to JSON: TypeError("SQLModel.model_dump() missing 1 required positional argument: 'self'")
Failed to use dict to serialize <class 'sqlmodel.main.SQLModelMetaclass'> to JSON: TypeError("SQLModel.dict() missing 1 required positional argument: 'self'")
...
This occurs because the _serialize_json method attempts to call model_dump() on an SQLModelMetaclass rather than an instance of the SQLModel.
Suggestion:
Potential Solution: Modify the _serialize_json method to ensure that model_dump() is only called on instances of SQLModel, not the class itself. Here's a possible update to prevent this issue:
`def _serialize_json(obj: Any) -> Any:
try:
if isinstance(obj, (set, tuple)):
if hasattr(obj, "_asdict") and callable(obj._asdict):
# NamedTuple
return obj._asdict()
return list(obj)
`
This modification checks whether the object passed to _serialize_json is a class or an instance before attempting to call model_dump.
Environment:
Python Version: [please provide]
SQLModel Version: [please provide]
Langsmith Version: [please provide]
Let me know if any other information is needed!
The text was updated successfully, but these errors were encountered: