Skip to content

Commit

Permalink
core[patch]: Prevent ConsoleCallbackHandler from throwing TypeError w…
Browse files Browse the repository at this point in the history
…hen logging (#6612)

Co-authored-by: jacoblee93 <jacoblee93@gmail.com>
  • Loading branch information
benjamincburns and jacoblee93 authored Aug 26, 2024
1 parent ea666a0 commit d859378
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions langchain-core/src/tracers/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ function tryJsonStringify(obj: unknown, fallback: string) {
}
}

function formatKVMapItem(value: unknown) {
if (typeof value === "string") {
return value.trim();
}

if (value === null || value === undefined) {
return value;
}

return tryJsonStringify(value, value.toString());
}

function elapsed(run: Run): string {
if (!run.end_time) return "";
const elapsed = run.end_time - run.start_time;
Expand Down Expand Up @@ -210,7 +222,9 @@ export class ConsoleCallbackHandler extends BaseTracer {
`${wrap(
color.green,
"[tool/start]"
)} [${crumbs}] Entering Tool run with input: "${run.inputs.input?.trim()}"`
)} [${crumbs}] Entering Tool run with input: "${formatKVMapItem(
run.inputs.input
)}"`
);
}

Expand All @@ -221,10 +235,13 @@ export class ConsoleCallbackHandler extends BaseTracer {
*/
onToolEnd(run: Run) {
const crumbs = this.getBreadcrumbs(run);

console.log(
`${wrap(color.cyan, "[tool/end]")} [${crumbs}] [${elapsed(
run
)}] Exiting Tool run with output: "${run.outputs?.output?.trim()}"`
)}] Exiting Tool run with output: "${formatKVMapItem(
run.outputs?.output
)}"`
);
}

Expand Down

0 comments on commit d859378

Please sign in to comment.