Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with SQLModel model_dump Serialization in _serialize_json #1089

Open
adunato opened this issue Oct 11, 2024 · 0 comments
Open

Issue with SQLModel model_dump Serialization in _serialize_json #1089

adunato opened this issue Oct 11, 2024 · 0 comments

Comments

@adunato
Copy link

adunato commented Oct 11, 2024

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)

    # 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!

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

No branches or pull requests

1 participant