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]: print stack trace when error occur in evaluate #695

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
Expand Up @@ -197,4 +197,4 @@
},
"./package.json": "./package.json"
}
}
}
4 changes: 4 additions & 0 deletions js/src/evaluation/_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { AsyncCaller } from "../utils/async_caller.js";
import { atee } from "../utils/atee.js";
import { getLangChainEnvVarsMetadata } from "../utils/env.js";
import { printErrorStackTrace } from "../utils/error.js";
import { randomName } from "./_random_name.js";
import {
EvaluationResult,
Expand Down Expand Up @@ -61,8 +62,8 @@
runs?: AsyncGenerator<Run>;
evaluationResults?: AsyncGenerator<EvaluationResults>;
summaryResults?: AsyncGenerator<
(runsArray: Run[]) => AsyncGenerator<EvaluationResults, any, unknown>,

Check warning on line 65 in js/src/evaluation/_runner.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
any,

Check warning on line 66 in js/src/evaluation/_runner.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
unknown
>;
examples?: Example[];
Expand Down Expand Up @@ -141,8 +142,8 @@
_evaluationResults?: AsyncGenerator<EvaluationResults>;

_summaryResults?: AsyncGenerator<
(runsArray: Run[]) => AsyncGenerator<EvaluationResults, any, unknown>,

Check warning on line 145 in js/src/evaluation/_runner.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
any,

Check warning on line 146 in js/src/evaluation/_runner.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
unknown
>;

Expand Down Expand Up @@ -206,7 +207,7 @@
get evaluationResults(): AsyncGenerator<EvaluationResults> {
if (this._evaluationResults === undefined) {
return async function* (this: _ExperimentManager) {
for (const _ of await this.getExamples()) {

Check warning on line 210 in js/src/evaluation/_runner.ts

View workflow job for this annotation

GitHub Actions / Check linting

'_' is assigned a value but never used
yield { results: [] };
}
}.call(this);
Expand Down Expand Up @@ -538,6 +539,7 @@
console.error(
`Error running evaluator ${evaluator.evaluateRun.name} on run ${run.id}: ${e}`
);
printErrorStackTrace(e);
}
}

Expand Down Expand Up @@ -619,7 +621,7 @@
this.client._selectEvalResults(summaryEvalResult);
aggregateFeedback.push(...flattenedResults);
for (const result of flattenedResults) {
const { targetRunId, ...feedback } = result;

Check warning on line 624 in js/src/evaluation/_runner.ts

View workflow job for this annotation

GitHub Actions / Check linting

'targetRunId' is assigned a value but never used
const evaluatorInfo = feedback.evaluatorInfo;
delete feedback.evaluatorInfo;

Expand All @@ -635,6 +637,7 @@
evaluator.name
}: ${JSON.stringify(e, null, 2)}`
);
printErrorStackTrace(e);
}
}

Expand Down Expand Up @@ -798,7 +801,7 @@
return results;
}

type ForwardFn = ((...args: any[]) => Promise<any>) | ((...args: any[]) => any);

Check warning on line 804 in js/src/evaluation/_runner.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type

Check warning on line 804 in js/src/evaluation/_runner.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type

Check warning on line 804 in js/src/evaluation/_runner.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type

async function _forward(
fn: ForwardFn,
Expand Down Expand Up @@ -835,6 +838,7 @@
await wrappedFn(example.inputs);
} catch (e) {
console.error(`Error running target function: ${e}`);
printErrorStackTrace(e);
}

if (!run) {
Expand Down
23 changes: 23 additions & 0 deletions js/src/utils/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function getErrorStackTrace(e: unknown) {
if (typeof e !== "object" || e == null) return undefined;
if (!("stack" in e) || typeof e.stack !== "string") return undefined;

let stack = e.stack;

const prevLine = `${e}`;
if (stack.startsWith(prevLine)) {
stack = stack.slice(prevLine.length);
}

if (stack.startsWith("\n")) {
stack = stack.slice(1);
}

return stack;
}

export function printErrorStackTrace(e: unknown) {
const stack = getErrorStackTrace(e);
if (stack == null) return;
console.error(stack);
}
Loading