Skip to content
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
10 changes: 9 additions & 1 deletion packages/providers/src/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,14 @@ function formatValidationPath(error: TLocalizedValidationError): string {
return path || "root";
}

function formatValidationMessage(error: TLocalizedValidationError): string {
if (error.keyword === "additionalProperties") {
const names = (error.params as { additionalProperties?: string[] }).additionalProperties;
if (names && names.length > 0) return `${error.message}: ${names.join(", ")}`;
}
return error.message;
}

/**
* Finds a tool by name and validates the tool call arguments against its TypeBox schema
* @param tools Array of tool definitions
Expand Down Expand Up @@ -341,7 +349,7 @@ export function validateToolArguments(tool: Tool, toolCall: ToolCall): any {
const errors =
validator
.Errors(args)
.map((error) => ` - ${formatValidationPath(error)}: ${error.message}`)
.map((error) => ` - ${formatValidationPath(error)}: ${formatValidationMessage(error)}`)
.join("\n") || "Unknown validation error";

const errorMessage = `Validation failed for tool "${toolCall.name}":\n${errors}\n\nReceived arguments:\n${JSON.stringify(toolCall.arguments, null, 2)}`;
Expand Down
21 changes: 21 additions & 0 deletions packages/providers/test/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,25 @@ describe("validateToolArguments", () => {
expect(() => validateToolArguments(tool, toolCall)).toThrow("Validation failed");
}
});

it("names the unexpected properties in additionalProperties errors", () => {
const tool: Tool = {
name: "echo",
description: "Echo tool",
parameters: Type.Object(
{
items: Type.Array(Type.Object({ id: Type.String() }, { additionalProperties: false })),
},
{ additionalProperties: false },
),
};
const toolCall: ToolCall = {
type: "toolCall",
id: "tool-1",
name: "echo",
arguments: { items: [{ id: "a" }, { id: "b", note: "x" }] },
};

expect(() => validateToolArguments(tool, toolCall)).toThrow("items.1: must not have additional properties: note");
});
});