Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: Support base64 image from markdown image syntax (#9054)

sxjerud013a166ce31
diff --git a/packages/model-runtime/src/utils/streams/openai/openai.test.ts b/packages/model-runtime/src/utils/streams/openai/openai.test.ts
index 5bb9926f80c..42131ade110 100644
--- a/packages/model-runtime/src/utils/streams/openai/openai.test.ts
+++ b/packages/model-runtime/src/utils/streams/openai/openai.test.ts
@@ -163,6 +163,61 @@ describe('OpenAIStream', () => {
);
});
+ it('should emit base64_image and strip markdown data:image from text', async () => {
+ const data = [
+ {
+ id: 'img-1',
+ choices: [
+ { index: 0, delta: { role: 'assistant', content: '这是一张图片: ' } },
+ ],
+ },
+ {
+ id: 'img-1',
+ choices: [
+ {
+ index: 0,
+ delta: {
+ content:
+ '![image](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAAB3D1E1AA==)',
+ },
+ },
+ ],
+ },
+ { id: 'img-1', choices: [{ index: 0, delta: {}, finish_reason: 'stop' }] },
+ ];
+
+ const mockOpenAIStream = new ReadableStream({
+ start(controller) {
+ data.forEach((c) => controller.enqueue(c));
+ controller.close();
+ },
+ });
+
+ const protocolStream = OpenAIStream(mockOpenAIStream);
+
+ const decoder = new TextDecoder();
+ const chunks: string[] = [];
+
+ // @ts-ignore
+ for await (const chunk of protocolStream) {
+ chunks.push(decoder.decode(chunk, { stream: true }));
+ }
+
+ expect(chunks).toEqual(
+ [
+ 'id: img-1',
+ 'event: text',
+ `data: "这是一张图片: "\n`,
+ 'id: img-1',
+ 'event: base64_image',
+ `data: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAAB3D1E1AA=="\n`,
+ 'id: img-1',
+ 'event: stop',
+ `data: "stop"\n`,
+ ].map((i) => `${i}\n`),
+ );
+ });
+
it('should handle content with tool_calls but is an empty object', async () => {
// data: {"id":"chatcmpl-A7pokGUqSov0JuMkhiHhWU9GRtAgJ", "object":"chat.completion.chunk", "created":1726430846, "model":"gpt-4o-2024-05-13", "choices":[{"index":0, "delta":{"content":" today", "role":"", "tool_calls":[]}, "finish_reason":"", "logprobs":""}], "prompt_annotations":[{"prompt_index":0, "content_filter_results":null}]}
const mockOpenAIStream = new ReadableStream({
@@ -2311,4 +2366,86 @@ describe('OpenAIStream', () => {
expect(chunks).toEqual(['id: 6\n', 'event: base64_image\n', `data: "${base64}"\n\n`]);
});
+
+ it('should handle finish_reason with markdown image in content', async () => {
+ const base64 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAADwf7zUAAAgAElEQVR4nFy9a5okSY4jCFBU3SOr53HdvcZeYW/YVZnhZqpCYn+AVIuZ7PqqKyPczfQhQgIgSOH/+//9PxRVu7QzX5nvqveVP5mv+3rf+XPt985b2NIVgVgK1jr0da7zrAiegWPhPBABLi1GILhCEMkFnCuOFRFxHN/r/CbOym/om/h1X+d1H/v667rP9328r9g3VNblpoXsAwsnTtnWp0kQ40siih6NixuHlN9Rt7ehv1mbW2dkg1ef03J9zQQpQg5yc/XllveG4wa4arKtSr0NwSCdGEJVNeKlkDZMov695YaQ5NVK3fmjn4OrE9N/U04C0EqT/2HCBxrf9pJe1L2nPBjqhKEq1TEi1Q/OXiIq+IrqX2fUb+qF+2kF10k/4ScwIXidU6/T6vGkA/bSR/fZ7Ok8yOd0s+27CnP8PH3cijINdbAcAAAAASUVORK5CYII=';
+ const mockOpenAIStream = new ReadableStream({
+ start(controller) {
+ controller.enqueue({
+ id: 'chatcmpl-test',
+ choices: [
+ {
+ index: 0,
+ delta: { content: `这有一张图片: ![image](${base64})` },
+ finish_reason: 'stop',
+ },
+ ],
+ });
+
+ controller.close();
+ },
+ });
+
+ const protocolStream = OpenAIStream(mockOpenAIStream);
+
+ const decoder = new TextDecoder();
+ const chunks = [];
+
+ // @ts-ignore
+ for await (const chunk of protocolStream) {
+ chunks.push(decoder.decode(chunk, { stream: true }));
+ }
+
+ expect(chunks).toEqual([
+ 'id: chatcmpl-test\n',
+ 'event: text\n',
+ `data: "这有一张图片:"\n\n`,
+ 'id: chatcmpl-test\n',
+ 'event: base64_image\n',
+ `data: "${base64}"\n\n`,
+ ]);
+ });
+
+ it('should handle finish_reason with multiple markdown images in content', async () => {
+ const base64_1 = 'data:image/png;base64,first';
+ const base64_2 = 'data:image/jpeg;base64,second';
+ const mockOpenAIStream = new ReadableStream({
+ start(controller) {
+ controller.enqueue({
+ id: 'chatcmpl-multi',
+ choices: [
+ {
+ index: 0,
+ delta: { content: `![img1](${base64_1}) and ![img2](${base64_2})` },
+ finish_reason: 'stop',
+ },
+ ],
+ });
+
+ controller.close();
+ },
+ });
+
+ const protocolStream = OpenAIStream(mockOpenAIStream);
+
+ const decoder = new TextDecoder();
+ const chunks = [];
+
+ // @ts-ignore
+ for await (const chunk of protocolStream) {
+ chunks.push(decoder.decode(chunk, { stream: true }));
+ }
+
+ expect(chunks).toEqual([
+ 'id: chatcmpl-multi\n',
+ 'event: text\n',
+ `data: "and"\n\n`, // Remove all markdown base64 image segments
+ 'id: chatcmpl-multi\n',
+ 'event: base64_image\n',
+ `data: "${base64_1}"\n\n`,
+ 'id: chatcmpl-multi\n',
+ 'event: base64_image\n',
+ `data: "${base64_2}"\n\n`,
+ ]);
+ });
});
diff --git a/packages/model-runtime/src/utils/streams/openai/openai.ts b/packages/model-runtime/src/utils/streams/openai/openai.ts
index 1a28de7e837..42180768f11 100644
--- a/packages/model-runtime/src/utils/streams/openai/openai.ts
+++ b/packages/model-runtime/src/utils/streams/openai/openai.ts
@@ -20,6 +20,28 @@ import {
generateToolCallId,
} from '../protocol';
+// Process markdown base64 images: extract URLs and clean text in one pass
+const processMarkdownBase64Images = (text: string): { cleanedText: string, urls: string[]; } => {
+ if (!text) return { cleanedText: text, urls: [] };
+
+ const urls: string[] = [];
+ const mdRegex = /!\[[^\]]*]\(\s*(data:image\/[\d+.A-Za-z-]+;base64,[^\s)]+)\s*\)/g;
+ let cleanedText = text;
+ let m: RegExpExecArray | null;
+
+ // Reset regex lastIndex to ensure we start from the beginning
+ mdRegex.lastIndex = 0;
+
+ while ((m = mdRegex.exec(text)) !== null) {
+ if (m[1]) urls.push(m[1]);
+ }
+
+ // Remove all markdown base64 image segments
+ cleanedText = text.replaceAll(mdRegex, '').trim();
+
+ return { cleanedText, urls };
+};
+
const transformOpenAIStream = (
chunk: OpenAI.ChatCompletionChunk,
streamContext: StreamContext,
@@ -137,7 +159,19 @@ const transformOpenAIStream = (
return { data: null, id: chunk.id, type: 'text' };
}
- return { data: item.delta.content, id: chunk.id, type: 'text' };
+
+ const text = item.delta.content as string;
+ const { urls: images, cleanedText: cleaned } = processMarkdownBase64Images(text);
+ if (images.length > 0) {
+ const arr: StreamProtocolChunk[] = [];
+ if (cleaned) arr.push({ data: cleaned, id: chunk.id, type: 'text' });
+ arr.push(
+ ...images.map((url: string) => ({ data: url, id: chunk.id, type: 'base64_image' as const })),
+ );
+ return arr;
+ }
+
+ return { data: text, id: chunk.id, type: 'text' };
}
// OpenAI Search Preview 模型返回引用源
@@ -284,7 +318,7 @@ const transformOpenAIStream = (
if (citations) {
streamContext.returnedCitation = true;
- return [
+ const baseChunks: StreamProtocolChunk[] = [
{
data: {
citations: (citations as any[])
@@ -303,6 +337,20 @@ const transformOpenAIStream = (
type: streamContext?.thinkingInContent ? 'reasoning' : 'text',
},
];
+ return baseChunks;
+ }
+ }
+
+ // 非思考模式下,额外解析 markdown 中的 base64 图片,按顺序输出 text -> base64_image
+ if (!streamContext?.thinkingInContent) {
+ const { urls, cleanedText: cleaned } = processMarkdownBase64Images(thinkingContent);
+ if (urls.length > 0) {
+ const arr: StreamProtocolChunk[] = [];
+ if (cleaned) arr.push({ data: cleaned, id: chunk.id, type: 'text' });
+ arr.push(
+ ...urls.map((url: string) => ({ data: url, id: chunk.id, type: 'base64_image' as const })),
+ );
+ return arr;
}
}