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

core[patch]: Fix async local storage automatic tracing for language models #4883

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions langchain-core/src/runnables/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,18 +281,23 @@ export abstract class Runnable<
}

protected _separateRunnableConfigFromCallOptions(
options: Partial<CallOptions> = {}
options?: Partial<CallOptions>
): [RunnableConfig, Omit<Partial<CallOptions>, keyof RunnableConfig>] {
const runnableConfig: RunnableConfig = ensureConfig({
callbacks: options.callbacks,
tags: options.tags,
metadata: options.metadata,
runName: options.runName,
configurable: options.configurable,
recursionLimit: options.recursionLimit,
maxConcurrency: options.maxConcurrency,
});
const callOptions = { ...options };
let runnableConfig;
if (options === undefined) {
runnableConfig = ensureConfig(options);
} else {
runnableConfig = ensureConfig({
callbacks: options.callbacks,
tags: options.tags,
metadata: options.metadata,
runName: options.runName,
configurable: options.configurable,
recursionLimit: options.recursionLimit,
maxConcurrency: options.maxConcurrency,
});
}
const callOptions = { ...(options as Partial<CallOptions>) };
delete callOptions.callbacks;
delete callOptions.tags;
delete callOptions.metadata;
Expand Down
3 changes: 3 additions & 0 deletions langchain-core/src/runnables/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ const PRIMITIVES = new Set(["string", "number", "boolean"]);

/**
* Ensure that a passed config is an object with all required keys present.
*
* Note: To make sure async local storage loading works correctly, this
* should not be called with a default or prepopulated config argument.
*/
export function ensureConfig<CallOptions extends RunnableConfig>(
config?: CallOptions
Expand Down
2 changes: 1 addition & 1 deletion langchain-core/src/runnables/tests/runnable_remote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe("RemoteRunnable", () => {
expect(fetch).toHaveBeenCalledWith(
`${BASE_URL}/a/invoke`,
expect.objectContaining({
body: '{"input":{"text":"string"},"config":{},"kwargs":{}}',
body: '{"input":{"text":"string"},"config":{"tags":[],"metadata":{},"recursionLimit":25},"kwargs":{}}',
})
);
expect(result).toEqual(["a", "b", "c"]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ test("Config should be automatically populated after setting global async local
);
expect(res4?.tags).toEqual(["tester_with_config"]);

const chatModel = new FakeListChatModel({ responses: ["test"] }).bind({
stop: [],
});
const chatModel = new FakeListChatModel({ responses: ["test"] });
const outer4 = RunnableLambda.from(async () => {
const res = await chatModel.invoke("hey");
return res;
Expand Down
22 changes: 0 additions & 22 deletions langchain/src/runnables/tests/runnable_remote.int.test.ts

This file was deleted.

218 changes: 0 additions & 218 deletions langchain/src/runnables/tests/runnable_remote.test.ts

This file was deleted.

Loading