Serializable

Trait Serializable 

Source
pub trait Serializable:
    Any
    + Send
    + Sync {
    // Required method
    fn get_lc_namespace() -> Vec<String>
       where Self: Sized;

    // Provided methods
    fn is_lc_serializable() -> bool
       where Self: Sized { ... }
    fn lc_secrets(&self) -> HashMap<String, String> { ... }
    fn lc_attributes(&self) -> HashMap<String, Value> { ... }
    fn lc_id() -> Vec<String>
       where Self: Sized { ... }
    fn lc_type_name(&self) -> &'static str { ... }
    fn to_json(&self) -> Serialized
       where Self: Sized + Serialize { ... }
    fn to_json_not_implemented(&self) -> Serialized { ... }
}
Expand description

Trait for objects that can be serialized to JSON for LangChain compatibility.

This trait provides the core serialization functionality, allowing objects to be serialized and deserialized across different versions and implementations.

§Example

use agent_chain_core::load::Serializable;

struct MyModel {
    name: String,
    value: i32,
}

impl Serializable for MyModel {
    fn is_lc_serializable() -> bool {
        true
    }

    fn get_lc_namespace() -> Vec<String> {
        vec!["my_package".to_string(), "models".to_string()]
    }
}

Required Methods§

Source

fn get_lc_namespace() -> Vec<String>
where Self: Sized,

Get the namespace of the LangChain object.

For example, if the class is langchain.llms.openai.OpenAI, then the namespace is ["langchain", "llms", "openai"].

Provided Methods§

Source

fn is_lc_serializable() -> bool
where Self: Sized,

Is this class serializable?

By design, even if a type implements Serializable, it is not serializable by default. This is to prevent accidental serialization of objects that should not be serialized.

Returns false by default.

Source

fn lc_secrets(&self) -> HashMap<String, String>

A map of constructor argument names to secret ids.

For example, {"openai_api_key": "OPENAI_API_KEY"}.

Source

fn lc_attributes(&self) -> HashMap<String, Value>

List of attribute names that should be included in the serialized kwargs.

These attributes must be accepted by the constructor.

Source

fn lc_id() -> Vec<String>
where Self: Sized,

Return a unique identifier for this class for serialization purposes.

The unique identifier is a list of strings that describes the path to the object.

Source

fn lc_type_name(&self) -> &'static str

Get the type name of this object.

Source

fn to_json(&self) -> Serialized
where Self: Sized + Serialize,

Serialize this object to JSON.

Returns a SerializedConstructor if the object is serializable, or a SerializedNotImplemented if it is not.

Source

fn to_json_not_implemented(&self) -> Serialized

Serialize a “not implemented” object.

Implementors§