Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
Improve MIME type detection for image URLs (#6531)
Seayon561a61e7fe20
diff --git a/api/core/model_runtime/model_providers/anthropic/llm/llm.py b/api/core/model_runtime/model_providers/anthropic/llm/llm.pyindex 19ce401999c..81be1a06a7c 100644--- a/api/core/model_runtime/model_providers/anthropic/llm/llm.py+++ b/api/core/model_runtime/model_providers/anthropic/llm/llm.py@@ -1,6 +1,6 @@import base64+import ioimport json-import mimetypesfrom collections.abc import Generatorfrom typing import Optional, Union, cast@@ -18,6 +18,7 @@ from anthropic.types import ()from anthropic.types.beta.tools import ToolsBetaMessagefrom httpx import Timeout+from PIL import Imagefrom core.model_runtime.callbacks.base_callback import Callbackfrom core.model_runtime.entities.llm_entities import LLMResult, LLMResultChunk, LLMResultChunkDelta@@ -462,7 +463,8 @@ class AnthropicLargeLanguageModel(LargeLanguageModel):# fetch image data from urltry:image_content = requests.get(message_content.data).content- mime_type, _ = mimetypes.guess_type(message_content.data)+ with Image.open(io.BytesIO(image_content)) as img:+ mime_type = f"image/{img.format.lower()}"base64_data = base64.b64encode(image_content).decode('utf-8')except Exception as ex:raise ValueError(f"Failed to fetch image data from url {message_content.data}, {ex}")diff --git a/api/core/model_runtime/model_providers/bedrock/llm/llm.py b/api/core/model_runtime/model_providers/bedrock/llm/llm.pyindex 335fa493cde..3f7266f6002 100644--- a/api/core/model_runtime/model_providers/bedrock/llm/llm.py+++ b/api/core/model_runtime/model_providers/bedrock/llm/llm.py@@ -1,8 +1,8 @@# standard importimport base64+import ioimport jsonimport logging-import mimetypesfrom collections.abc import Generatorfrom typing import Optional, Union, cast@@ -17,6 +17,7 @@ from botocore.exceptions import (ServiceNotInRegionError,UnknownServiceError,)+from PIL.Image import Image# local importfrom core.model_runtime.entities.llm_entities import LLMResult, LLMResultChunk, LLMResultChunkDelta@@ -381,9 +382,8 @@ class BedrockLargeLanguageModel(LargeLanguageModel):try:url = message_content.dataimage_content = requests.get(url).content- if '?' in url:- url = url.split('?')[0]- mime_type, _ = mimetypes.guess_type(url)+ with Image.open(io.BytesIO(image_content)) as img:+ mime_type = f"image/{img.format.lower()}"base64_data = base64.b64encode(image_content).decode('utf-8')except Exception as ex:raise ValueError(f"Failed to fetch image data from url {message_content.data}, {ex}")diff --git a/api/core/model_runtime/model_providers/google/llm/llm.py b/api/core/model_runtime/model_providers/google/llm/llm.pyindex ebcd0af35b2..84241fb6c87 100644--- a/api/core/model_runtime/model_providers/google/llm/llm.py+++ b/api/core/model_runtime/model_providers/google/llm/llm.py@@ -1,7 +1,7 @@import base64+import ioimport jsonimport logging-import mimetypesfrom collections.abc import Generatorfrom typing import Optional, Union, cast@@ -12,6 +12,7 @@ import google.generativeai.client as clientimport requestsfrom google.generativeai.types import ContentType, GenerateContentResponse, HarmBlockThreshold, HarmCategoryfrom google.generativeai.types.content_types import to_part+from PIL import Imagefrom core.model_runtime.entities.llm_entities import LLMResult, LLMResultChunk, LLMResultChunkDeltafrom core.model_runtime.entities.message_entities import (@@ -371,7 +372,8 @@ class GoogleLargeLanguageModel(LargeLanguageModel):# fetch image data from urltry:image_content = requests.get(message_content.data).content- mime_type, _ = mimetypes.guess_type(message_content.data)+ with Image.open(io.BytesIO(image_content)) as img:+ mime_type = f"image/{img.format.lower()}"base64_data = base64.b64encode(image_content).decode('utf-8')except Exception as ex:raise ValueError(f"Failed to fetch image data from url {message_content.data}, {ex}")diff --git a/api/core/model_runtime/model_providers/vertex_ai/llm/llm.py b/api/core/model_runtime/model_providers/vertex_ai/llm/llm.pyindex 8901549110e..1a7368a2cf8 100644--- a/api/core/model_runtime/model_providers/vertex_ai/llm/llm.py+++ b/api/core/model_runtime/model_providers/vertex_ai/llm/llm.py@@ -1,4 +1,5 @@import base64+import ioimport jsonimport loggingfrom collections.abc import Generator@@ -18,6 +19,7 @@ from anthropic.types import ()from google.cloud import aiplatformfrom google.oauth2 import service_account+from PIL import Imagefrom vertexai.generative_models import HarmBlockThreshold, HarmCategoryfrom core.model_runtime.entities.llm_entities import LLMResult, LLMResultChunk, LLMResultChunkDelta, LLMUsage@@ -332,7 +334,8 @@ class VertexAiLargeLanguageModel(LargeLanguageModel):# fetch image data from urltry:image_content = requests.get(message_content.data).content- mime_type, _ = mimetypes.guess_type(message_content.data)+ with Image.open(io.BytesIO(image_content)) as img:+ mime_type = f"image/{img.format.lower()}"base64_data = base64.b64encode(image_content).decode('utf-8')except Exception as ex:raise ValueError(f"Failed to fetch image data from url {message_content.data}, {ex}")