fix(ai): report throwing Standard Schema validation as a tool error

This commit is contained in:
Aiden Cline
2026-10-31 17:01:39 -05:00
Unverified
parent 6b6982460f
commit 32f457a332
2 changed files with 37 additions and 4 deletions
+7 -4
View File
@@ -279,16 +279,19 @@ const standardJsonSchema = (schema: StandardSchemaV1): JsonSchema.JsonSchema =>
return body
}
// A throwing or rejecting `validate` is an input error the model can see, not a defect.
const validateStandard = (schema: StandardSchemaV1, value: unknown) =>
Effect.suspend(() => {
const result = schema["~standard"].validate(value)
return result instanceof Promise ? Effect.promise(() => result) : Effect.succeed(result)
}).pipe(
Effect.try({ try: () => schema["~standard"].validate(value), catch: toError }).pipe(
Effect.flatMap((result) =>
result instanceof Promise ? Effect.tryPromise({ try: () => result, catch: toError }) : Effect.succeed(result),
),
Effect.flatMap((result) =>
result.issues === undefined ? Effect.succeed(result.value) : Effect.fail(new Error(formatIssues(result.issues))),
),
)
const toError = (cause: unknown) => (cause instanceof Error ? cause : new Error(String(cause)))
const formatIssues = (issues: ReadonlyArray<StandardSchemaV1.Issue>) =>
issues
.map((issue) => {
+30
View File
@@ -67,6 +67,36 @@ describe("Standard Schema tool input", () => {
})
})
test("returns a throwing or rejecting validate as a tool error", async () => {
const schema = (validate: () => never) => ({
"~standard": {
version: 1 as const,
vendor: "test",
validate,
jsonSchema: { input: () => ({ type: "object" }), output: () => ({ type: "object" }) },
},
})
const results = await Promise.all(
[
schema(() => {
throw new Error("sync boom")
}),
schema(() => Promise.reject(new Error("async boom")) as never),
].map((input) =>
Effect.runPromise(
ToolRuntime.dispatch(
[tool({ name: "boom", description: "Boom.", input, execute: () => Effect.succeed("ok") })],
call("boom", {}),
),
),
),
)
expect(results.map((result) => result.result)).toEqual([
{ type: "error", value: "Invalid tool input: sync boom" },
{ type: "error", value: "Invalid tool input: async boom" },
])
})
test("fails clearly when the schema has no JSON Schema export", () => {
expect(() =>
tool({ name: "weather", description: "Get the weather.", input: v.object({ city: v.string() }) }),