Skip to content

Commit 7d6f7eb

Browse files
committed
fix(ai): keep an undefined tool result out of the json result type
1 parent 8697644 commit 7d6f7eb

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

‎packages/ai/src/schema/messages.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ export const ToolResultValue = withStatics(toolResultValueSchema, {
8383
make: (value: unknown, type: ToolResultValue["type"] = "json"): ToolResultValue => {
8484
if (isToolResultValue(value)) return value
8585
if (type === "content") return { type, value: Array.isArray(value) ? value : [] }
86+
// A json result must stay JSON-encodable; `undefined` has no encoding and would abort request lowering later.
87+
if (type === "json" && value === undefined) return { type, value: null }
8688
return { type, value }
8789
},
8890
})

‎packages/ai/test/schema.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ describe("ToolResultValue", () => {
107107
}
108108
})
109109

110+
test("normalizes an undefined json value to null", () => {
111+
expect(ToolResultValue.make(undefined)).toEqual({ type: "json", value: null })
112+
expect(ToolResultValue.make(undefined, "json")).toEqual({ type: "json", value: null })
113+
expect(ToolResultValue.make(undefined, "text")).toEqual({ type: "text", value: undefined })
114+
})
115+
110116
test("accepts canonical results with extra fields", () => {
111117
expect(ToolResultValue.is({ type: "json", value: { ok: true }, metadata: { source: "tool" } })).toBe(true)
112118
expect(

0 commit comments

Comments
 (0)