Skip to content

Commit

Permalink
fix(js): Use batch endpoint instead of individual run endpoint in cas…
Browse files Browse the repository at this point in the history
…e of server info fetch failure (#1189)

CC @akira
  • Loading branch information
jacoblee93 authored Nov 8, 2024
1 parent bce2a8d commit 5f17121
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 31 deletions.
18 changes: 1 addition & 17 deletions js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ export class Client {
this._serverInfo = await this._getServerInfo();
} catch (e) {

Check warning on line 839 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

'e' is defined but never used. Allowed unused args must match /^_/u
console.warn(
`[WARNING]: LangSmith failed to fetch info on supported operations. Falling back to single calls and default limits.`
`[WARNING]: LangSmith failed to fetch info on supported operations. Falling back to batch operations and default limits.`
);
}
}
Expand Down Expand Up @@ -956,22 +956,6 @@ export class Client {
if (!rawBatch.post.length && !rawBatch.patch.length) {
return;
}
const serverInfo = await this._ensureServerInfo();
if (serverInfo.version === undefined) {
this.autoBatchTracing = false;
for (const preparedCreateParam of rawBatch.post) {
await this.createRun(preparedCreateParam as CreateRunParams);
}
for (const preparedUpdateParam of rawBatch.patch) {
if (preparedUpdateParam.id !== undefined) {
await this.updateRun(
preparedUpdateParam.id,
preparedUpdateParam as UpdateRunParams
);
}
}
return;
}
const batchChunks = {
post: [] as (typeof rawBatch)["post"],
patch: [] as (typeof rawBatch)["patch"],
Expand Down
34 changes: 20 additions & 14 deletions js/src/tests/batch_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,19 +753,19 @@ describe.each(ENDPOINT_TYPES)(
});
});

it("If batching is unsupported, fall back to old endpoint", async () => {
it("Use batch endpoint if info call fails", async () => {
const client = new Client({
apiKey: "test-api-key",
autoBatchTracing: true,
});
const callSpy = jest
.spyOn((client as any).caller, "call")
.spyOn((client as any).batchIngestCaller, "call")
.mockResolvedValue({
ok: true,
text: () => "",
});
jest.spyOn(client as any, "_getServerInfo").mockImplementation(() => {
return {};
throw new Error("Totally expected mock error");
});
const projectName = "__test_batch";

Expand All @@ -784,26 +784,32 @@ describe.each(ENDPOINT_TYPES)(
dotted_order: dottedOrder,
});

await new Promise((resolve) => setTimeout(resolve, 300));
await client.awaitPendingTraceBatches();

const calledRequestParam: any = callSpy.mock.calls[0][2];

expect(
await parseMockRequestBody(calledRequestParam?.body)
).toMatchObject({
id: runId,
session_name: projectName,
extra: expect.anything(),
start_time: expect.any(Number),
name: "test_run",
run_type: "llm",
inputs: { text: "hello world" },
trace_id: runId,
dotted_order: dottedOrder,
post: [
{
id: runId,
session_name: projectName,
extra: expect.anything(),
start_time: expect.any(Number),
name: "test_run",
run_type: "llm",
inputs: { text: "hello world" },
trace_id: runId,
dotted_order: dottedOrder,
},
],
patch: [],
});

expect(callSpy).toHaveBeenCalledWith(
_getFetchImplementation(),
"https://api.smith.langchain.com/runs",
"https://api.smith.langchain.com/runs/batch",
expect.objectContaining({
body: expect.any(String),
})
Expand Down

0 comments on commit 5f17121

Please sign in to comment.