Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

chore: add local base 64 url image for send message integration (#5969)

jnjpng05b359b7f52b
diff --git a/letta/helpers/message_helper.py b/letta/helpers/message_helper.py
index b834a3056..c2e6a6304 100644
--- a/letta/helpers/message_helper.py
+++ b/letta/helpers/message_helper.py
@@ -1,5 +1,6 @@
import base64
import mimetypes
+from urllib.parse import unquote, urlparse
import httpx
@@ -61,12 +62,33 @@ def _convert_message_create_to_message(
elif isinstance(content, ImageContent):
if content.source.type == ImageSourceType.url:
# Convert URL image to Base64Image if needed
- image_response = httpx.get(content.source.url)
- image_response.raise_for_status()
- image_media_type = image_response.headers.get("content-type")
- if not image_media_type:
- image_media_type, _ = mimetypes.guess_type(content.source.url)
- image_data = base64.standard_b64encode(image_response.content).decode("utf-8")
+ url = content.source.url
+
+ # Handle file:// URLs for local filesystem access
+ if url.startswith("file://"):
+ # Parse file path from file:// URL
+ parsed = urlparse(url)
+ file_path = unquote(parsed.path)
+
+ # Read file directly from filesystem
+ with open(file_path, "rb") as f:
+ image_bytes = f.read()
+
+ # Guess media type from file extension
+ image_media_type, _ = mimetypes.guess_type(file_path)
+ if not image_media_type:
+ image_media_type = "image/jpeg" # default fallback
+ else:
+ # Handle http(s):// URLs using httpx
+ image_response = httpx.get(url)
+ image_response.raise_for_status()
+ image_bytes = image_response.content
+ image_media_type = image_response.headers.get("content-type")
+ if not image_media_type:
+ image_media_type, _ = mimetypes.guess_type(url)
+
+ # Convert to base64 (common path for both file:// and http(s)://)
+ image_data = base64.standard_b64encode(image_bytes).decode("utf-8")
content.source = Base64Image(media_type=image_media_type, data=image_data)
if content.source.type == ImageSourceType.letta and not content.source.data:
# TODO: hydrate letta image with data from db
diff --git a/tests/integration_test_send_message.py b/tests/integration_test_send_message.py
index 91322936e..fd322d27e 100644
--- a/tests/integration_test_send_message.py
+++ b/tests/integration_test_send_message.py
@@ -6,6 +6,7 @@ import time
import uuid
from contextlib import contextmanager
from http.server import BaseHTTPRequestHandler, HTTPServer
+from pathlib import Path
from typing import Any, Dict, List
from unittest.mock import patch
@@ -156,7 +157,30 @@ USER_MESSAGE_ROLL_DICE_LONG_THINKING: List[MessageCreate] = [
otid=USER_MESSAGE_OTID,
)
]
-URL_IMAGE = "https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg"
+
+
+# Load test image from local file rather than fetching from external URL.
+# Using a local file avoids network dependencies and makes tests faster and more reliable.
+def _get_test_image_file_url() -> str:
+ """Returns a file:// URL pointing to the local test image."""
+ image_path = os.path.join(os.path.dirname(__file__), "./data/Camponotus_flavomarginatus_ant.jpg")
+ # Convert to absolute path and create file:// URL
+ absolute_path = os.path.abspath(image_path)
+ return Path(absolute_path).as_uri() # Returns: file:///absolute/path/to/image.jpg
+
+
+def _load_test_image() -> str:
+ """Loads the test image from the data folder and returns it as base64."""
+ image_path = os.path.join(os.path.dirname(__file__), "./data/Camponotus_flavomarginatus_ant.jpg")
+ with open(image_path, "rb") as f:
+ return base64.standard_b64encode(f.read()).decode("utf-8")
+
+
+# Original external URL (kept for reference)
+# URL_IMAGE = "https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg"
+
+# Use local file:// URL instead of external HTTP URL
+URL_IMAGE = _get_test_image_file_url()
USER_MESSAGE_URL_IMAGE: List[MessageCreate] = [
MessageCreate(
role="user",
@@ -167,7 +191,10 @@ USER_MESSAGE_URL_IMAGE: List[MessageCreate] = [
otid=USER_MESSAGE_OTID,
)
]
-BASE64_IMAGE = base64.standard_b64encode(httpx.get(URL_IMAGE).content).decode("utf-8")
+
+
+BASE64_IMAGE = _load_test_image()
+
USER_MESSAGE_BASE64_IMAGE: List[MessageCreate] = [
MessageCreate(
role="user",