From 7a31b5c0f76ce1c06befacc09b47b4fc3c71c408 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Sat, 12 Sep 2026 23:43:25 -0500 Subject: [PATCH] fix(ai): normalize Bedrock document labels (#48756) --- packages/ai/src/protocols/bedrock-converse.ts | 34 ++-- .../ai/src/protocols/utils/bedrock-media.ts | 36 +++- .../ai/test/provider/bedrock-converse.test.ts | 167 ++++++++++++++++-- 3 files changed, 209 insertions(+), 28 deletions(-) diff --git a/packages/ai/src/protocols/bedrock-converse.ts b/packages/ai/src/protocols/bedrock-converse.ts index 7eca07f6f3d..65b0ceb2a4b 100644 --- a/packages/ai/src/protocols/bedrock-converse.ts +++ b/packages/ai/src/protocols/bedrock-converse.ts @@ -288,7 +288,10 @@ const lowerToolCall = (part: ToolCallPart): BedrockToolUseBlock => ({ }, }) -const lowerToolResultContent = Effect.fn("BedrockConverse.lowerToolResultContent")(function* (part: ToolResultPart) { +const lowerToolResultContent = Effect.fn("BedrockConverse.lowerToolResultContent")(function* ( + part: ToolResultPart, + documentNames: Set, +) { if (part.result.type === "text" || part.result.type === "error") return [{ text: ProviderShared.toolResultText(part) }] if (part.result.type === "json") return [{ json: part.result.value }] @@ -299,22 +302,28 @@ const lowerToolResultContent = Effect.fn("BedrockConverse.lowerToolResultContent content.push({ text: item.text }) continue } - const media = yield* BedrockMedia.lower({ - type: "media", - mediaType: item.mime, - data: item.uri, - filename: item.name, - }) - content.push(media) + const media = yield* BedrockMedia.lower( + { + type: "media", + mediaType: item.mime, + data: item.uri, + filename: item.name, + }, + documentNames, + ) + content.push(...media) } return content }) -const lowerToolResult = Effect.fn("BedrockConverse.lowerToolResult")(function* (part: ToolResultPart) { +const lowerToolResult = Effect.fn("BedrockConverse.lowerToolResult")(function* ( + part: ToolResultPart, + documentNames: Set, +) { return { toolResult: { toolUseId: part.id, - content: yield* lowerToolResultContent(part), + content: yield* lowerToolResultContent(part, documentNames), status: part.result.type === "error" ? "error" : "success", }, } satisfies BedrockToolResultBlock @@ -325,6 +334,7 @@ const lowerMessages = Effect.fn("BedrockConverse.lowerMessages")(function* ( breakpoints: BedrockCache.Breakpoints, ) { const messages: BedrockMessage[] = [] + const documentNames = new Set() const providerMetadataKey = request.model.route.providerMetadataKey ?? String(request.model.provider) for (const message of request.messages) { @@ -348,7 +358,7 @@ const lowerMessages = Effect.fn("BedrockConverse.lowerMessages")(function* ( continue } if (part.type === "media") { - content.push(yield* BedrockMedia.lower(part)) + content.push(...(yield* BedrockMedia.lower(part, documentNames))) continue } } @@ -401,7 +411,7 @@ const lowerMessages = Effect.fn("BedrockConverse.lowerMessages")(function* ( for (const part of message.content) { if (!ProviderShared.supportsContent(part, ["tool-result"])) return yield* ProviderShared.unsupportedContent("Bedrock Converse", "tool", ["tool-result"]) - content.push(yield* lowerToolResult(part)) + content.push(yield* lowerToolResult(part, documentNames)) const cachePoint = BedrockCache.block(breakpoints, part.cache) if (cachePoint) content.push(cachePoint) } diff --git a/packages/ai/src/protocols/utils/bedrock-media.ts b/packages/ai/src/protocols/utils/bedrock-media.ts index d2a6ab64311..36ff554cc91 100644 --- a/packages/ai/src/protocols/utils/bedrock-media.ts +++ b/packages/ai/src/protocols/utils/bedrock-media.ts @@ -57,6 +57,25 @@ const documentBlock = (name: string, format: DocumentFormat, bytes: string): Doc }, }) +function documentName(filename: string | undefined, names: Set) { + const base = + (filename ?? "") + .replace(/\.[^.]*$/, "") + .replace(/[^a-zA-Z0-9 ()[\]-]/g, " ") + .replace(/\s+/g, " ") + .trim() + .slice(0, 200) + .trim() || "document" + let name = base + // Converse requires labels to be unique across the entire request, including tool results. + for (let index = 2; names.has(name); index++) { + const suffix = ` ${index}` + name = `${base.slice(0, 200 - suffix.length).trimEnd()}${suffix}` + } + names.add(name) + return name +} + const mediaBase64 = Effect.fn("BedrockMedia.mediaBase64")(function* (part: MediaPart) { const media = ProviderShared.normalizeMedia(part) const bytes = yield* Effect.fromResult(Encoding.decodeBase64(media.base64)).pipe( @@ -72,19 +91,26 @@ const mediaBase64 = Effect.fn("BedrockMedia.mediaBase64")(function* (part: Media // document block. Image MIME types not in `IMAGE_FORMATS` (e.g. `image/svg+xml`) // get an image-specific error so the caller knows it's a format-support issue, // not a kind-detection issue. -export const lower = Effect.fn("BedrockMedia.lower")(function* (part: MediaPart) { +export const lower = Effect.fn("BedrockMedia.lower")(function* (part: MediaPart, documentNames: Set) { const mime = part.mediaType.toLowerCase() const imageFormat = IMAGE_FORMATS[mime as keyof typeof IMAGE_FORMATS] if (imageFormat) { - return { image: { format: imageFormat, source: { bytes: yield* mediaBase64(part) } } } satisfies ImageBlock + return [{ image: { format: imageFormat, source: { bytes: yield* mediaBase64(part) } } } satisfies ImageBlock] } if (mime.startsWith("image/")) return yield* ProviderShared.invalidRequest(`Bedrock Converse does not support image media type ${part.mediaType}`) const documentFormat = DOCUMENT_FORMATS[mime as keyof typeof DOCUMENT_FORMATS] if (documentFormat) { - if (!part.filename) - return yield* ProviderShared.invalidRequest("Bedrock Converse document media requires a filename") - return documentBlock(part.filename, documentFormat, yield* mediaBase64(part)) + const name = documentName(part.filename, documentNames) + const block = documentBlock(name, documentFormat, yield* mediaBase64(part)) + return part.filename !== undefined && part.filename !== name + ? [ + { + text: `Attached file ${ProviderShared.encodeJson(part.filename)} has document label ${ProviderShared.encodeJson(name)}.`, + }, + block, + ] + : [block] } return yield* ProviderShared.invalidRequest(`Bedrock Converse does not support media type ${part.mediaType}`) }) diff --git a/packages/ai/test/provider/bedrock-converse.test.ts b/packages/ai/test/provider/bedrock-converse.test.ts index 2679334945f..e19e34b3d8b 100644 --- a/packages/ai/test/provider/bedrock-converse.test.ts +++ b/packages/ai/test/provider/bedrock-converse.test.ts @@ -1765,29 +1765,141 @@ describe("Bedrock Converse route", () => { role: "user", content: [ { text: "Summarize these documents." }, - { document: { format: "pdf", name: "report.pdf", source: { bytes: "UERGREFUQQ==" } } }, - { document: { format: "csv", name: "data.csv", source: { bytes: "Q1NWREFUQQ==" } } }, + { text: 'Attached file "report.pdf" has document label "report".' }, + { document: { format: "pdf", name: "report", source: { bytes: "UERGREFUQQ==" } } }, + { text: 'Attached file "data.csv" has document label "data".' }, + { document: { format: "csv", name: "data", source: { bytes: "Q1NWREFUQQ==" } } }, ], }, ], }) }), ) + ;[ + { + label: "filename punctuation", + filename: "report_v1.2?.pdf", + expected: "report v1 2", + duplicate: "report v1 2 2", + }, + { + label: "repeated whitespace", + filename: " Quarterly\t \n report.txt", + expected: "Quarterly report", + duplicate: "Quarterly report 2", + }, + { + label: "allowed characters", + filename: "Report - Final (v2) [2026]", + expected: "Report - Final (v2) [2026]", + duplicate: "Report - Final (v2) [2026] 2", + }, + { label: "accented filename", filename: "résumé.pdf", expected: "r sum", duplicate: "r sum 2" }, + { label: "non-Latin filename", filename: "報告書.pdf", expected: "document", duplicate: "document 2" }, + { label: "missing filename", filename: undefined, expected: "document", duplicate: "document 2" }, + { label: "empty filename", filename: "", expected: "document", duplicate: "document 2" }, + { label: "blank filename", filename: " \t\n", expected: "document", duplicate: "document 2" }, + { label: "extension-only filename", filename: ".pdf", expected: "document", duplicate: "document 2" }, + { label: "symbols-only filename", filename: "@@@.pdf", expected: "document", duplicate: "document 2" }, + { + label: "overlong filename", + filename: `${"a".repeat(201)}.txt`, + expected: "a".repeat(200), + duplicate: `${"a".repeat(198)} 2`, + }, + { + label: "maximum-length label", + filename: "a".repeat(200), + expected: "a".repeat(200), + duplicate: `${"a".repeat(198)} 2`, + }, + { + label: "whitespace at truncation", + filename: `${"a".repeat(199)} b.txt`, + expected: "a".repeat(199), + duplicate: `${"a".repeat(198)} 2`, + }, + ].forEach((item) => { + it.effect(`normalizes ${item.label} in user and tool-result documents`, () => + Effect.gen(function* () { + const request = LLM.request({ + model, + cache: "none", + messages: [ + Message.user([ + { type: "text", text: "Read this document" }, + { type: "media", mediaType: "application/pdf", data: "UERGREFUQQ==", filename: item.filename }, + ]), + Message.assistant([ToolCallPart.make({ id: "call_read", name: "read", input: {} })]), + Message.tool({ + id: "call_read", + name: "read", + result: { + type: "content", + value: [ + { type: "text", text: "Read successfully" }, + { + type: "file", + uri: "data:application/pdf;base64,UERGREFUQQ==", + mime: "application/pdf", + name: item.filename, + }, + ], + }, + }), + ], + }) + const original = JSON.stringify(request.messages) + const first = yield* compileRequest(request) + const second = yield* compileRequest(request) + const expected = { format: "pdf", name: item.expected, source: { bytes: "UERGREFUQQ==" } } - it.effect("requires names for document media", () => + expect(first.body.messages[0].content.find((part) => "document" in part)?.document).toEqual(expected) + expect( + first.body.messages[2].content[0].toolResult.content.find((part) => "document" in part)?.document, + ).toEqual({ + ...expected, + name: item.duplicate, + }) + expect(second.body).toEqual(first.body) + expect(JSON.stringify(request.messages)).toBe(original) + }), + ) + }) + + it.effect("keeps colliding document labels distinct within a request", () => Effect.gen(function* () { - const error = yield* compileRequest( + const prepared = yield* compileRequest( LLM.request({ model, - messages: [Message.user({ type: "media", mediaType: "application/pdf", data: "UERGREFUQQ==" })], + cache: "none", + messages: [ + Message.user([ + { type: "text", text: "Read these documents" }, + ...["report_v1.txt", "report#v1.txt", "report v1 2.txt", "report v1.txt"].map((filename) => ({ + type: "media" as const, + mediaType: "text/plain", + data: "SGVsbG8=", + filename, + })), + ]), + ], }), - ).pipe(Effect.flip) - - expect(error.message).toContain("document media requires a filename") + ) + expect(prepared.body.messages[0].content.slice(1)).toEqual([ + { text: 'Attached file "report_v1.txt" has document label "report v1".' }, + { document: { format: "txt", name: "report v1", source: { bytes: "SGVsbG8=" } } }, + { text: 'Attached file "report#v1.txt" has document label "report v1 2".' }, + { document: { format: "txt", name: "report v1 2", source: { bytes: "SGVsbG8=" } } }, + { text: 'Attached file "report v1 2.txt" has document label "report v1 2 2".' }, + { document: { format: "txt", name: "report v1 2 2", source: { bytes: "SGVsbG8=" } } }, + { text: 'Attached file "report v1.txt" has document label "report v1 3".' }, + { document: { format: "txt", name: "report v1 3", source: { bytes: "SGVsbG8=" } } }, + ]) }), ) - it.effect("passes named document-only messages through for provider validation", () => + it.effect("annotates renamed document-only messages with their original filename", () => Effect.gen(function* () { const prepared = yield* compileRequest( LLM.request({ @@ -1807,12 +1919,44 @@ describe("Bedrock Converse route", () => { expect(prepared.body.messages).toEqual([ { role: "user", - content: [{ document: { format: "pdf", name: "report.pdf", source: { bytes: "UERGREFUQQ==" } } }], + content: [ + { text: 'Attached file "report.pdf" has document label "report".' }, + { document: { format: "pdf", name: "report", source: { bytes: "UERGREFUQQ==" } } }, + ], }, ]) }), ) + it.effect("quotes original filenames in annotations and omits redundant mappings", () => + Effect.gen(function* () { + const prepared = yield* compileRequest( + LLM.request({ + model, + cache: "none", + messages: [ + Message.user([ + { type: "text", text: "Read these documents" }, + ...["report", undefined, 'report "final"\n.pdf'].map((filename) => ({ + type: "media" as const, + mediaType: "text/plain", + data: "SGVsbG8=", + filename, + })), + ]), + ], + }), + ) + expect(prepared.body.messages[0].content).toEqual([ + { text: "Read these documents" }, + { document: { format: "txt", name: "report", source: { bytes: "SGVsbG8=" } } }, + { document: { format: "txt", name: "document", source: { bytes: "SGVsbG8=" } } }, + { text: 'Attached file "report \\"final\\"\\n.pdf" has document label "report final".' }, + { document: { format: "txt", name: "report final", source: { bytes: "SGVsbG8=" } } }, + ]) + }), + ) + it.effect("lowers document media in tool results", () => Effect.gen(function* () { const prepared = yield* compileRequest( @@ -1832,7 +1976,7 @@ describe("Bedrock Converse route", () => { type: "file", uri: "data:application/pdf;base64,UERGREFUQQ==", mime: "application/pdf", - name: "report", + name: "report.pdf", }, ], }, @@ -1855,6 +1999,7 @@ describe("Bedrock Converse route", () => { status: "success", content: [ { text: "Read successfully" }, + { text: 'Attached file "report.pdf" has document label "report".' }, { document: { format: "pdf", name: "report", source: { bytes: "UERGREFUQQ==" } } }, ], },