How to capture and use the exception error on a Runnable With Retry Chain? #5065
Replies: 1 comment 1 reply
-
To handle exceptions in a Runnable step within a Retry Chain and adapt the retry logic based on the error, you can use the async handleChainError(
error: unknown,
runId: string,
_parentRunId?: string,
_tags?: string[],
kwargs?: { inputs?: Record<string, unknown> }
): Promise<Run> {
const run = this.runMap.get(runId);
if (!run) {
throw new Error("No chain run to end.");
}
run.end_time = Date.now();
run.error = this.stringifyError(error);
run.events.push({
name: "error",
time: new Date(run.end_time).toISOString(),
});
// Implement your error analysis and decide if and how to retry
if (shouldRetryBasedOnError(error)) {
const modifiedInputs = modifyInputsBasedOnError(run.inputs, error);
// Adjust retry parameters if needed and retry the operation
retryChainRunWithNewParameters(runId, modifiedInputs);
}
await this.onChainError?.(run);
await this._endTrace(run);
return run;
} In this code, Sources
|
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
Dear community,
Suppose I have a Runnable step in my chain that may throw an error.
RunnableRetry would retry that step up to
stopAfterAttempt
attempts, which is fine.But how can I capture that exception message to feed it back to the retry loop in a way that the next iteration would be aware of that error and try to fix it?
A use case would be a SQL query run that throws an error. We could capture that error and ask the chain to rewrite that sql query fixing the error before retrying.
Any sample code of how to do this would be much appreciated.
System Info
[email protected] | MIT | deps: 17 | versions: 264
Beta Was this translation helpful? Give feedback.
All reactions