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: revert anthropic base64 image (#13751)

Rdmclin216ed80701cf6
diff --git a/packages/model-runtime/src/core/contextBuilders/anthropic.test.ts b/packages/model-runtime/src/core/contextBuilders/anthropic.test.ts
index 6a6128e3bd0..5ad886acee9 100644
--- a/packages/model-runtime/src/core/contextBuilders/anthropic.test.ts
+++ b/packages/model-runtime/src/core/contextBuilders/anthropic.test.ts
@@ -52,12 +52,16 @@ describe('anthropicHelpers', () => {
});
});
- it('should use URL-based source for image URLs by default', async () => {
+ it('should convert URL to base64 for image URLs', async () => {
vi.mocked(parseDataUri).mockReturnValueOnce({
mimeType: 'image/png',
base64: null,
type: 'url',
});
+ vi.mocked(imageUrlToBase64).mockResolvedValueOnce({
+ base64: 'convertedBase64String',
+ mimeType: 'image/jpg',
+ });
const content = {
type: 'image_url',
@@ -67,55 +71,31 @@ describe('anthropicHelpers', () => {
const result = await buildAnthropicBlock(content);
expect(parseDataUri).toHaveBeenCalledWith(content.image_url.url);
- expect(imageUrlToBase64).not.toHaveBeenCalled();
+ expect(imageUrlToBase64).toHaveBeenCalledWith(content.image_url.url);
expect(result).toEqual({
source: {
- type: 'url',
- url: 'https://example.com/image.png',
+ data: 'convertedBase64String',
+ media_type: 'image/jpg',
+ type: 'base64',
},
type: 'image',
});
});
- it('should use URL-based source for URLs without extension', async () => {
+ it('should convert URL to base64 for URLs without extension', async () => {
vi.mocked(parseDataUri).mockReturnValueOnce({
mimeType: null,
base64: null,
type: 'url',
});
-
- const content = {
- type: 'image_url',
- image_url: { url: 'https://example.com/image' },
- } as const;
-
- const result = await buildAnthropicBlock(content);
-
- expect(result).toEqual({
- source: {
- type: 'url',
- url: 'https://example.com/image',
- },
- type: 'image',
- });
- });
-
- it('should convert URL to base64 when LLM_VISION_IMAGE_USE_BASE64 is set', async () => {
- process.env.LLM_VISION_IMAGE_USE_BASE64 = '1';
-
- vi.mocked(parseDataUri).mockReturnValueOnce({
- mimeType: 'image/png',
- base64: null,
- type: 'url',
- });
vi.mocked(imageUrlToBase64).mockResolvedValueOnce({
base64: 'convertedBase64String',
- mimeType: 'image/jpg',
+ mimeType: 'image/png',
});
const content = {
type: 'image_url',
- image_url: { url: 'https://example.com/image.png' },
+ image_url: { url: 'https://example.com/image' },
} as const;
const result = await buildAnthropicBlock(content);
@@ -124,13 +104,11 @@ describe('anthropicHelpers', () => {
expect(result).toEqual({
source: {
data: 'convertedBase64String',
- media_type: 'image/jpg',
+ media_type: 'image/png',
type: 'base64',
},
type: 'image',
});
-
- delete process.env.LLM_VISION_IMAGE_USE_BASE64;
});
it('should throw an error for invalid image URLs', async () => {
@@ -167,12 +145,16 @@ describe('anthropicHelpers', () => {
expect(result).toBeUndefined();
});
- it('should pass SVG URL through to Anthropic in URL mode', async () => {
+ it('should return undefined for unsupported SVG URL after base64 conversion', async () => {
vi.mocked(parseDataUri).mockReturnValueOnce({
mimeType: null,
base64: null,
type: 'url',
});
+ vi.mocked(imageUrlToBase64).mockResolvedValueOnce({
+ base64: 'svgBase64String',
+ mimeType: 'image/svg+xml',
+ });
const content = {
type: 'image_url',
@@ -180,14 +162,8 @@ describe('anthropicHelpers', () => {
} as const;
const result = await buildAnthropicBlock(content);
- expect(imageUrlToBase64).not.toHaveBeenCalled();
- expect(result).toEqual({
- source: {
- type: 'url',
- url: 'https://example.com/image.svg',
- },
- type: 'image',
- });
+ expect(imageUrlToBase64).toHaveBeenCalledWith(content.image_url.url);
+ expect(result).toBeUndefined();
});
});
diff --git a/packages/model-runtime/src/core/contextBuilders/anthropic.ts b/packages/model-runtime/src/core/contextBuilders/anthropic.ts
index b326b24d2a0..c1b8262a157 100644
--- a/packages/model-runtime/src/core/contextBuilders/anthropic.ts
+++ b/packages/model-runtime/src/core/contextBuilders/anthropic.ts
@@ -56,28 +56,15 @@ export const buildAnthropicBlock = async (
}
if (type === 'url') {
- const shouldUseBase64 = process.env.LLM_VISION_IMAGE_USE_BASE64 === '1';
+ const { base64, mimeType } = await imageUrlToBase64(content.image_url.url);
- if (shouldUseBase64) {
- const { base64, mimeType } = await imageUrlToBase64(content.image_url.url);
-
- if (!isImageTypeSupported(mimeType)) return undefined;
-
- return {
- source: {
- data: base64 as string,
- media_type: mimeType as Anthropic.Base64ImageSource['media_type'],
- type: 'base64',
- },
- type: 'image',
- };
- }
+ if (!isImageTypeSupported(mimeType)) return undefined;
- // Default: use URL-based source (more efficient, 20 MB limit vs 5 MB for base64)
return {
source: {
- type: 'url' as const,
- url: content.image_url.url,
+ data: base64 as string,
+ media_type: mimeType as Anthropic.Base64ImageSource['media_type'],
+ type: 'base64',
},
type: 'image',
};