Skip to content

Commit 754ffed

Browse files
authored
feat(core): Add a way to manually specify serializable fields (#7667)
1 parent 7459710 commit 754ffed

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

langchain-core/src/callbacks/base.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ export abstract class BaseCallbackHandler
328328
return undefined;
329329
}
330330

331+
get lc_serializable_keys(): string[] | undefined {
332+
return undefined;
333+
}
334+
331335
/**
332336
* The name of the serializable. Override to provide an alias or
333337
* to preserve the serialized module name in minified environments.

langchain-core/src/load/serializable.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,24 @@ export abstract class Serializable implements SerializableInterface {
140140
return undefined;
141141
}
142142

143+
/**
144+
* A manual list of keys that should be serialized.
145+
* If not overridden, all fields passed into the constructor will be serialized.
146+
*/
147+
get lc_serializable_keys(): string[] | undefined {
148+
return undefined;
149+
}
150+
143151
constructor(kwargs?: SerializedFields, ..._args: never[]) {
144-
this.lc_kwargs = kwargs || {};
152+
if (this.lc_serializable_keys !== undefined) {
153+
this.lc_kwargs = Object.fromEntries(
154+
Object.entries(kwargs || {}).filter(([key]) =>
155+
this.lc_serializable_keys?.includes(key)
156+
)
157+
);
158+
} else {
159+
this.lc_kwargs = kwargs ?? {};
160+
}
145161
}
146162

147163
toJSON(): Serialized {

0 commit comments

Comments
 (0)