fix(media): normalize urlsafe base64 for media uploads#1766
Open
MJRuskin wants to merge 1 commit into
Open
Conversation
Comment on lines
+294
to
+296
| normalized_data = actual_data.replace("-", "+").replace("_", "/") | ||
|
|
||
| return base64.b64decode(normalized_data), cast(MediaContentType, content_type) |
Contributor
There was a problem hiding this comment.
Missing test coverage for URL-safe base64 inputs. The PR checklist acknowledges no tests were added.
tests/unit/test_media.py has no case exercising the - and _ substitution path, so a future regression (e.g. re-introduction of b64decode without normalisation) would go undetected. Consider adding a unit test that passes a URL-safe encoded data URI and asserts the decoded bytes match the expected output.
Prompt To Fix With AI
This is a comment left during a code review.
Path: langfuse/media.py
Line: 294-296
Comment:
**Missing test coverage for URL-safe base64 inputs.** The PR checklist acknowledges no tests were added. `tests/unit/test_media.py` has no case exercising the `-` and `_` substitution path, so a future regression (e.g. re-introduction of `b64decode` without normalisation) would go undetected. Consider adding a unit test that passes a URL-safe encoded data URI and asserts the decoded bytes match the expected output.
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
langfuse/langfuse#15179
Fixes compatibility issues with openinference package, which uses urlsafe base64 in certain output fields.
Normalizes the base64 string during the parse function.
Type of change
Verification
List the main commands you ran:
Checklist
code_review.md..env.templateif needed.Greptile Summary
This PR fixes a compatibility bug where media uploads would fail when the base64 payload used the URL-safe alphabet (
-and_instead of+and/), as produced by theopeninferencepackage. The fix normalizes the incoming base64 string before decoding.actual_data.replace("-", "+").replace("_", "/")is applied beforebase64.b64decode, converting URL-safe base64 to standard base64. This is functionally correct; an equally valid (and more idiomatic) alternative isbase64.urlsafe_b64decode.=; ifopeninferenceever omits padding the call will still raise and be swallowed by the outerexceptblock.Confidence Score: 4/5
Safe to merge — the change is a one-line normalization that fixes a real compatibility gap and degrades gracefully on failure.
The normalization logic is correct and the error path is already guarded by the outer try/except. The only gaps are a missed opportunity to use the stdlib urlsafe_b64decode and the absence of unit tests for the new code path.
langfuse/media.py — worth adding a unit test in tests/unit/test_media.py that covers URL-safe encoded inputs.
Sequence Diagram
%%{init: {'theme': 'neutral'}}%% sequenceDiagram participant Caller participant LangfuseMedia participant base64 Caller->>LangfuseMedia: _parse_base64_data_uri(data_uri) LangfuseMedia->>LangfuseMedia: "validate 'data:' prefix & split header/body" LangfuseMedia->>LangfuseMedia: check 'base64' in header parts Note over LangfuseMedia: NEW: normalize URL-safe chars<br/>replace('-','+').replace('_','/') LangfuseMedia->>base64: b64decode(normalized_data) base64-->>LangfuseMedia: raw bytes LangfuseMedia-->>Caller: (bytes, content_type)%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%% sequenceDiagram participant Caller participant LangfuseMedia participant base64 Caller->>LangfuseMedia: _parse_base64_data_uri(data_uri) LangfuseMedia->>LangfuseMedia: "validate 'data:' prefix & split header/body" LangfuseMedia->>LangfuseMedia: check 'base64' in header parts Note over LangfuseMedia: NEW: normalize URL-safe chars<br/>replace('-','+').replace('_','/') LangfuseMedia->>base64: b64decode(normalized_data) base64-->>LangfuseMedia: raw bytes LangfuseMedia-->>Caller: (bytes, content_type)Prompt To Fix All With AI
Reviews (1): Last reviewed commit: "fix(media): normalize urlsafe base64 for..." | Re-trigger Greptile