Skip to content

Commit

Permalink
Allow manually specified client in run tree with no env vars (#463)
Browse files Browse the repository at this point in the history
We were initializing a client by default every time, which means if you
don't have an env var set, you can't use the run tree.

Should we remove the thrown error altogether on client initialization?
Why was that added in the first place?

@hinthornw @dqbd
  • Loading branch information
jacoblee93 authored Feb 22, 2024
1 parent e56ea50 commit 5e074f7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
10 changes: 0 additions & 10 deletions js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ export class Client {
this.apiUrl = trimQuotes(config.apiUrl ?? defaultConfig.apiUrl) ?? "";
this.apiKey = trimQuotes(config.apiKey ?? defaultConfig.apiKey);
this.webUrl = trimQuotes(config.webUrl ?? defaultConfig.webUrl);
this.validateApiKeyIfHosted();
this.timeout_ms = config.timeout_ms ?? 12_000;
this.caller = new AsyncCaller(config.callerOptions ?? {});
this.hideInputs = config.hideInputs ?? defaultConfig.hideInputs;
Expand Down Expand Up @@ -328,15 +327,6 @@ export class Client {
};
}

private validateApiKeyIfHosted(): void {
const isLocal = isLocalhost(this.apiUrl);
if (!isLocal && !this.apiKey) {
throw new Error(
"API key must be provided when using hosted LangSmith API"
);
}
}

private getHostUrl(): string {
if (this.webUrl) {
return this.webUrl;
Expand Down
6 changes: 3 additions & 3 deletions js/src/run_trees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class RunTree implements BaseRun {
dotted_order: string;

constructor(config: RunTreeConfig) {
const defaultConfig = RunTree.getDefaultConfig();
const defaultConfig = RunTree.getDefaultConfig(config.client);
Object.assign(this, { ...defaultConfig, ...config });
if (!this.trace_id) {
if (this.parent_run) {
Expand All @@ -84,7 +84,7 @@ export class RunTree implements BaseRun {
}
}
}
private static getDefaultConfig(): object {
private static getDefaultConfig(client?: Client): object {
return {
id: uuid.v4(),
run_type: "chain",
Expand All @@ -101,7 +101,7 @@ export class RunTree implements BaseRun {
serialized: {},
inputs: {},
extra: {},
client: new Client({}),
client: client ?? new Client({}),
};
}

Expand Down
36 changes: 36 additions & 0 deletions js/src/tests/run_trees.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-disable no-process-env */

import { jest } from "@jest/globals";
import { Client } from "../client.js";
import { RunTree } from "../run_trees.js";

test.concurrent(
"Should work with manually set API key",
async () => {
const key = process.env.LANGCHAIN_API_KEY;
delete process.env.LANGCHAIN_API_KEY;
const langchainClient = new Client({
autoBatchTracing: true,
callerOptions: { maxRetries: 0 },
timeout_ms: 30_000,
apiKey: key,
});
const callSpy = jest
.spyOn((langchainClient as any).caller, "call")
.mockResolvedValue({
ok: true,
text: () => "",
});
const projectName = "__test_persist_update_run_tree";
const runTree = new RunTree({
name: "Test Run Tree",
inputs: { input: "foo1" },
client: langchainClient,
project_name: projectName,
});
await runTree.postRun();
await new Promise((resolve) => setTimeout(resolve, 1000));
expect(callSpy).toHaveBeenCalled();
},
180_000
);

0 comments on commit 5e074f7

Please sign in to comment.