Skip to content

Commit fcb71fd

Browse files
committed
make data uri parsing more robust
1 parent f11776e commit fcb71fd

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

langfuse/media.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,22 +173,25 @@ def parse_reference_string(reference_string: str) -> ParsedMediaReference:
173173
def _parse_base64_data_uri(
174174
self, data: str
175175
) -> tuple[Optional[bytes], Optional[MediaContentType]]:
176+
# Example data URI: data:image/jpeg;base64,/9j/4AAQ...
176177
try:
177178
if not data or not isinstance(data, str):
178179
raise ValueError("Data URI is not a string")
179180

180181
if not data.startswith("data:"):
181182
raise ValueError("Data URI does not start with 'data:'")
182183

183-
header, _, actual_data = data[5:].partition(",")
184+
header, actual_data = data[5:].split(",", 1)
184185
if not header or not actual_data:
185186
raise ValueError("Invalid URI")
186187

187-
is_base64 = header.endswith(";base64")
188-
if not is_base64:
188+
# Split header into parts and check for base64
189+
header_parts = header.split(";")
190+
if "base64" not in header_parts:
189191
raise ValueError("Data is not base64 encoded")
190192

191-
content_type = header[:-7]
193+
# Content type is the first part
194+
content_type = header_parts[0]
192195
if not content_type:
193196
raise ValueError("Content type is empty")
194197

0 commit comments

Comments
 (0)