Skip to content

Commit

Permalink
core[patch]: Adds option for more verbose parsing errors (#6634)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 authored Aug 26, 2024
1 parent f35ac1b commit e9d36ff
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
23 changes: 18 additions & 5 deletions langchain-core/src/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ export interface ToolParams extends BaseLangChainParams {
* @default "content"
*/
responseFormat?: ResponseFormat;
/**
* Whether to show full details in the thrown parsing errors.
*
* @default false
*/
verboseParsingErrors?: boolean;
}

/**
Expand Down Expand Up @@ -121,6 +127,9 @@ export abstract class StructuredTool<

returnDirect = false;

// TODO: Make default in 0.3
verboseParsingErrors = false;

get lc_namespace() {
return ["langchain", "tools"];
}
Expand All @@ -139,6 +148,8 @@ export abstract class StructuredTool<
constructor(fields?: ToolParams) {
super(fields ?? {});

this.verboseParsingErrors =
fields?.verboseParsingErrors ?? this.verboseParsingErrors;
this.responseFormat = fields?.responseFormat ?? this.responseFormat;
}

Expand Down Expand Up @@ -205,11 +216,13 @@ export abstract class StructuredTool<
let parsed;
try {
parsed = await this.schema.parseAsync(arg);
} catch (e) {
throw new ToolInputParsingException(
`Received tool input did not match expected schema`,
JSON.stringify(arg)
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
let message = `Received tool input did not match expected schema`;
if (this.verboseParsingErrors) {
message = `${message}\nDetails: ${e.message}`;
}
throw new ToolInputParsingException(message, JSON.stringify(arg));
}

const config = parseCallbackConfigArg(configArg);
Expand Down
36 changes: 36 additions & 0 deletions langchain-core/src/tools/tests/tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,39 @@ test("Tool input typing is enforced", async () => {
const res3 = await weatherTool3.invoke("blah");
expect(res3).toEqual("Sunny");
});

test("Tool can throw detailed errors", async () => {
const weatherSchema = z.object({
location: z.string(),
});

const stringTool = tool(
(input) => {
return JSON.stringify(input);
},
{
name: "string_tool",
description: "A tool that appends 'a' to the input string",
schema: weatherSchema,
verboseParsingErrors: true,
}
);

await expect(
stringTool.invoke({
// @ts-expect-error Testing parsing errors
location: 8,
})
).rejects.toThrow(`Received tool input did not match expected schema
Details: [
{
"code": "invalid_type",
"expected": "string",
"received": "number",
"path": [
"location"
],
"message": "Expected string, received number"
}
]`);
});

0 comments on commit e9d36ff

Please sign in to comment.