Skip to content

pydantic_model_field

PydanticModel

Bases: Field

Source code in griptape/schemas/pydantic_model_field.py
class PydanticModel(fields.Field):
    def _serialize(self, value: Optional[BaseModel], attr: Any, obj: Any, **kwargs) -> Optional[dict]:
        if value is None:
            return None
        return value.model_dump()

    def _deserialize(self, value: dict, attr: Any, data: Any, **kwargs) -> dict:
        # Not implemented as it is non-trivial to deserialize json back into a model
        # since we need to know the model class to instantiate it.
        # Would rather not implement right now rather than implement incorrectly.
        raise NotImplementedError("Model fields cannot be deserialized directly.")

_deserialize(value, attr, data, **kwargs)

Source code in griptape/schemas/pydantic_model_field.py
def _deserialize(self, value: dict, attr: Any, data: Any, **kwargs) -> dict:
    # Not implemented as it is non-trivial to deserialize json back into a model
    # since we need to know the model class to instantiate it.
    # Would rather not implement right now rather than implement incorrectly.
    raise NotImplementedError("Model fields cannot be deserialized directly.")

_serialize(value, attr, obj, **kwargs)

Source code in griptape/schemas/pydantic_model_field.py
def _serialize(self, value: Optional[BaseModel], attr: Any, obj: Any, **kwargs) -> Optional[dict]:
    if value is None:
        return None
    return value.model_dump()