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

fix(js): Use batch endpoint instead of individual run endpoint in case of server info fetch failure #1189

Merged
merged 1 commit into from
Nov 8, 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
2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "langsmith",
"version": "0.2.4",
"version": "0.2.5",
"description": "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform.",
"packageManager": "yarn@1.22.19",
"files": [
Expand Down
18 changes: 1 addition & 17 deletions js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@
// If there is an item on the queue we were unable to pop,
// just return it as a single batch.
if (popped.length === 0 && this.items.length > 0) {
const item = this.items.shift()!;

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

View workflow job for this annotation

GitHub Actions / Check linting

Forbidden non-null assertion
popped.push(item);
poppedSizeBytes += item.size;
this.sizeBytes -= item.size;
Expand Down Expand Up @@ -836,9 +836,9 @@
if (this._serverInfo === undefined) {
try {
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 @@
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 Expand Up @@ -1552,7 +1536,7 @@
treeFilter?: string;
isRoot?: boolean;
dataSourceType?: string;
}): Promise<any> {

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

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
let projectIds_ = projectIds || [];
if (projectNames) {
projectIds_ = [
Expand Down Expand Up @@ -1840,7 +1824,7 @@
`Failed to list shared examples: ${response.status} ${response.statusText}`
);
}
return result.map((example: any) => ({

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

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
...example,
_hostUrl: this.getHostUrl(),
}));
Expand Down Expand Up @@ -1977,7 +1961,7 @@
}
// projectId querying
return true;
} catch (e) {

Check warning on line 1964 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
return false;
}
}
Expand Down Expand Up @@ -3317,7 +3301,7 @@
async _logEvaluationFeedback(
evaluatorResponse: EvaluationResult | EvaluationResults,
run?: Run,
sourceInfo?: { [key: string]: any }

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

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
): Promise<[results: EvaluationResult[], feedbacks: Feedback[]]> {
const evalResults: Array<EvaluationResult> =
this._selectEvalResults(evaluatorResponse);
Expand Down Expand Up @@ -3356,7 +3340,7 @@
public async logEvaluationFeedback(
evaluatorResponse: EvaluationResult | EvaluationResults,
run?: Run,
sourceInfo?: { [key: string]: any }

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

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
): Promise<EvaluationResult[]> {
const [results] = await this._logEvaluationFeedback(
evaluatorResponse,
Expand Down Expand Up @@ -3806,7 +3790,7 @@

public async createCommit(
promptIdentifier: string,
object: any,

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

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
options?: {
parentCommitHash?: string;
}
Expand Down Expand Up @@ -3857,7 +3841,7 @@
isPublic?: boolean;
isArchived?: boolean;
}
): Promise<Record<string, any>> {

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

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
if (!(await this.promptExists(promptIdentifier))) {
throw new Error("Prompt does not exist, you must create it first.");
}
Expand All @@ -3868,7 +3852,7 @@
throw await this._ownerConflictError("update a prompt", owner);
}

const payload: Record<string, any> = {};

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

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type

if (options?.description !== undefined)
payload.description = options.description;
Expand Down
2 changes: 1 addition & 1 deletion js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ export { RunTree, type RunTreeConfig } from "./run_trees.js";
export { overrideFetchImplementation } from "./singletons/fetch.js";

// Update using yarn bump-version
export const __version__ = "0.2.4";
export const __version__ = "0.2.5";
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
Loading