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

core[patch]: Adds option for more verbose parsing errors #6634

Merged
merged 1 commit into from
Aug 26, 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
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"
}
]`);
});
Loading