Skip to content

Commit

Permalink
feat(core): Add a way to manually specify serializable fields (#7667)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 authored Feb 7, 2025
1 parent 7459710 commit 754ffed
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
4 changes: 4 additions & 0 deletions langchain-core/src/callbacks/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ export abstract class BaseCallbackHandler
return undefined;
}

get lc_serializable_keys(): string[] | undefined {
return undefined;
}

/**
* The name of the serializable. Override to provide an alias or
* to preserve the serialized module name in minified environments.
Expand Down
18 changes: 17 additions & 1 deletion langchain-core/src/load/serializable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,24 @@ export abstract class Serializable implements SerializableInterface {
return undefined;
}

/**
* A manual list of keys that should be serialized.
* If not overridden, all fields passed into the constructor will be serialized.
*/
get lc_serializable_keys(): string[] | undefined {
return undefined;
}

constructor(kwargs?: SerializedFields, ..._args: never[]) {
this.lc_kwargs = kwargs || {};
if (this.lc_serializable_keys !== undefined) {
this.lc_kwargs = Object.fromEntries(
Object.entries(kwargs || {}).filter(([key]) =>
this.lc_serializable_keys?.includes(key)
)
);
} else {
this.lc_kwargs = kwargs ?? {};
}
}

toJSON(): Serialized {
Expand Down

0 comments on commit 754ffed

Please sign in to comment.