Repository navigation
fix(langfuse): upload base64 media and export its reference from the langfuse callback - #53934
Conversation
…langfuse callback The v2 SDK replaced base64 payloads in a trace's input, output and metadata with Langfuse media references and uploaded the bytes before ingestion. The v4 export channel sends spans through litellm's own exporter, which had no such step, so images reached Langfuse inline and multiplied the stored trace size on servers that do not extract them. The export channel now replaces data URIs and the Anthropic and Vertex typed blocks with the same references and uploads the bytes through the Langfuse media REST API on one background thread per channel.
|
bugbot run |
1 similar comment
|
bugbot run |
There was a problem hiding this comment.
Devin Review found 4 potential issues.
5 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)
[Medium impact] The PR appears safe to merge, with both numbered findings fixed and no new actionable issues found.
|
…age response Media the uploader has no room for now stays inline in the span, the cap counts decoded bytes, and a failed upload makes flush report failure. The storage PUT no longer follows redirects and Langfuse is told the storage status without the storage response body. A data URI followed by text is left as it is, trace level media is registered without an observation, and queued uploads get five seconds to finish at shutdown.
|
bugbot run |
…eue an attribute's media as a whole A Langfuse host that comes with a key's or team's credentials now has the upload URLs it returns checked with validate_url, so it cannot point the upload at an internal address. A host the operator configured keeps naming its own storage. The media of one attribute is queued all together or not at all, so an attribute that stays inline no longer leaves part of its media uploading.
|
bugbot run |
A Langfuse with no media route or bucket, or one whose upload URL is refused, answers the same way for every payload. After the first such answer the uploader turns media away, so the exporter keeps it inline, and one upload per pause checks whether Langfuse takes media again.
|
bugbot run |
The span export keeps trying through a short Langfuse outage, so a trace could arrive with a reference whose single registration attempt had failed. Registration now waits out the same delays.
|
bugbot run |
A call rejected before the provider, such as an unknown model or a failed key check, is logged with its request body as the model parameters, which left the image inline in those traces.
|
bugbot run |
|
bugbot run |
|
bugbot run |
…hosts The Langfuse host on a key or team is not validated by default, so checking only the upload URL it returns added no protection. The check resolved DNS on the proxy and refused storage that is private or reachable only through an egress proxy, which left the trace with a media reference and no file.
…media_upload # Conflicts: # tests/code_coverage_tests/recursive_detector.py
A fork copies the upload queue but none of the parent's threads, so a child forked after the first logged call wrote media references that nothing uploaded. The child now gets its own queue and thread, and the parent keeps uploading what it had queued.
|
bugbot run |
The Langfuse SDK replaces Gemini inline_data and inlineData parts with a media reference. The callback now does the same, so a Gemini-native request with an image no longer stores its base64 inline.
|
bugbot run |
…op keeping media inline when it is full LANGFUSE_MEDIA_QUEUE_MAX_BYTES sets how many decoded bytes wait for upload per export channel, 100 MB when unset. Media that does not fit is no longer kept inline in the span: the span carries the reference, the upload is skipped with a warning and counted as failed, as the Langfuse SDK does when its queue is full.
|
bugbot run |
A lock a parent thread held at the fork stays locked in the child. The queue test now holds the first upload until the second has been turned away.
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit eaf02c8. Configure here.
… request's logged input (#54928) Since #53934 the Langfuse callback replaces a base64 image in the exported input with its media reference, so a refused request that carries an image no longer logs the base64 itself. The image endpoints now expect the reference and not the base64, and the text endpoints keep expecting the prompt inline Co-authored-by: mateo <277851410+mateo-berri@users.noreply.github.com> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Handwritten 1-3 sentence description
The
langfusecallback uploads base64 images to Langfuse media storage again and leaves a short reference in the trace, where it has stored the whole image inline since v1.104.0.Details
TLDR
Problem this solves:
langfusecallback stopped uploading base64 images to Langfuse media storageHow it solves it:
inline_dataparts, as the current Langfuse SDK doesUser Flow
Before: a proxy admin on self-hosted Langfuse sees every image request stored with the full base64 image inside the trace
success_callback: ["langfuse"]inlitellm_settingsand pointsLANGFUSE_HOSTat their Langfuseimage_urlwhoseurlisdata:image/png;base64,...data:image/png;base64,...string, tens of kilobytes per imageAfter: the same request stores a short media reference and the image itself sits in Langfuse media storage
@@@langfuseMedia:type=image/png|id=...|source=base64_data_uri@@@in place of the imageAffected release
Regression since v1.104.0-rc.1, the first version carrying the Langfuse SDK v4 migration (#36741). On SDK v2 the SDK did this replacement and upload before ingestion.
Behavior changes
langfusecallback sends media references again instead of inline base64, for image, audio, PDF and the other content types Langfuse storesPOST /api/public/media, the returned storage URL, andPATCH /api/public/media/{id}on the Langfuse host, from a background threadLANGFUSE_S3_MEDIA_UPLOAD_*), as those docs state. Without one every trace keeps a reference with no file, where main keeps the image inline0de408ebef9. The image is skipped with a warning and counted as a failed upload, which is what the Langfuse SDK does. Before that commit the image stayed inline. Approved by yucheng-berri on 2026-11-26LANGFUSE_MEDIA_QUEUE_MAX_BYTESsets how many bytes of decoded media may wait for upload per Langfuse credential set in each worker, 100 MB when unset. Approved by yucheng-berri on 2026-11-26inline_dataorinlineDatapart is uploaded and itsdatareplaced by the reference, added in94df9454160. Main keeps it inline and SDK v2 did too, the current Langfuse SDK replaces itgenerateContentroute logs the prompt text only on main and here, so this applies to a part that reaches the trace another way, such as requestmetadatalangfuse_oteland the OTel v2 Langfuse path are untouched, they have always sent media inlineRegression tests:
tests/unit/integrations/langfuse/test_langfuse_media.py, the media tests at the end oftests/unit/integrations/langfuse/test_langfuse_sdk.py, andtests/integration/observability/test_langfuse_delivery.py::test_langfuse_callback_uploads_a_base64_image_and_exports_only_its_media_reference.Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or moreDelays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
One audit run on a Linux VM, all of it at
e0fd39a3dda. Before is main'slangfuse.pyandlangfuse_sdk.pyat the merge baseb7a83256e24in the same tree, After ise0fd39a3dda. Both sides ran a real proxy with 4 workers and Postgres, real OpenAI (gpt-4o-mini), Bedrock Claude Haiku 4.5 and Gemini 2.5 Flash calls, and a self-hosted Langfuse 3.227.1 (langfuse/langfuseandlangfuse/langfuse-workerwith ClickHouse, Postgres, Redis and a MinIO media bucket). A recording proxy sits between LiteLLM and Langfuse, forwards every request unchanged and logs method, path, size and status, so the wire numbers below are what LiteLLM sent. Nothing in the product path is mocked.Shared setup, env vars
OPENAI_API_KEY,LANGFUSE_PUBLIC_KEY,LANGFUSE_SECRET_KEY,LITELLM_MASTER_KEY, andLANGFUSE_HOSTpointing at the recording proxy.Before (b7a8325)
data:image/png;base64,...string, which the Langfuse UI shows as a PNG chipAfter (e0fd39a)
POST /api/public/media(201), uploads the 27,817 byte image to the returned storage URL, sendsPATCH /api/public/media/YOOnzDEwPBHodP5SlZNhslwithuploadHttpStatus: 200, and exports a trace with 4,269 bytes of span attributes@@@langfuseMedia:type=image/png|id=YOOnzDEwPBHodP5SlZNhsl|source=base64_data_uri@@@in place of the imageGET /api/public/media/YOOnzDEwPBHodP5SlZNhslreturns the file, byte for byte the image that was sentThe screenshots are the same request shapes sent with a readable test image. They were retaken on
eaf02c8da2eagainst main, except the Bedrock Claude one, which is frome0fd39a3dda.Self-hosted Langfuse 4.85.0, before and after:
The audited requests on the LiteLLM Logs page:
Audit matrix, base b7a8325 against head e0fd39a
Every row ran on both sides. "Same" means status, span count, span attribute keys, stored fields, metadata keys and the input with the image masked are equal.
/v1/chat/completions,/v1/messages,/v1/responses, by curl, OpenAI SDK and Anthropic SDK, sync and async, streaming and not, on OpenAI, Bedrock Claude and Geminicallbacks: ["langfuse"]in place ofsuccess_callbackimage_url.urlsent as an int, empty, 5 KB of textimage_url.urlsent as a list holding a data URI (400 on both sides)c420bc31643, the head before the client fix, stops storing media at minute 85 withCannot send a request, as the client has been closed/v1/messages,/v1/responsesand a request with an invalid proxy key, against Langfuse Cloud 4.86.0, self-hosted 3.227.1 and self-hosted 4.85.0langfuse4.7.0 and 4.18.0 installed with the head tree, pooled client evicted, then one uploadextract_mediaat9bf6e604107and ate0fd39a3dda, with and without media, in four JSON layoutsA 1.1 MB media-free tool result with 12,000 rows, one of them
"encoding": "base64", took 33.7 ms to scan at9bf6e604107and takes 0.96 ms ate0fd39a3dda.The VM was shared and under load during the run, so latency numbers are for comparing the two sides only.
pytest tests/unit/integrations/langfusegives274 passedate0fd39a3dda. The tests check that media-free values come back unchanged, the scan time is measured above and not pinned by a test.Not run: S3 or GCS in place of MinIO, audio input, a proxy with Redis, and the full matrix against Langfuse 4.x and Langfuse Cloud.
Type
🐛 Bug Fix
Caveats (if any)
Medium
POST /api/public/mediaitself failed, which is what a missing media bucket causesmodel parameters, on main and hereLANGFUSE_MEDIA_QUEUE_MAX_BYTES, 100 MB of decoded media by defaulttests/documentation_tests/test_env_keys.py66bd514a538gives the forked child its own upload queue and thread. Before it, the child wrote references that nothing uploaded66bd514a538against main with 4 workers. The 12 cells on Langfuse Cloud, self-hosted 3.227.1 and 4.85.0 store a reference and a byte-identical file, and the 19 endpoint cells, the sad and edge cells, the worker kill and the stop after a burst match main apart from the image. The chaos bursts, the no bucket leg and the long uptime leg were not run again at this commit94df9454160against main with 4 workers, the full matrix except the long uptime leg. Every cell matches main apart from the image, including both chaos rounds and the no bucket leg. The Bedrock Claude cells returned 403 on both sides from an expired test credential, so they covered the failure trace onlyinline_datapart and one with aninlineDatapart inmetadata, run on a Mac against Langfuse Cloud.94df9454160stores the reference and the file, main stores the base64eaf02c8da2eagainst main with 4 workers, the same matrix plus the Geminimetadatarequests and a full queue leg. Every cell matches main apart from the image. WithLANGFUSE_MEDIA_QUEUE_MAX_BYTES=1000the trace holds the reference, no upload is sent and the proxy logs the full queue warning, while main keeps the base64. The Bedrock Claude cells returned 403 on both sides again and the long uptime leg was not run484ba0f1705removed thevalidate_urlcheck on it. The key or team Langfuse host is not validated by default, so the check added no protection, and it refused storage that is private or reachable only through an egress proxy66bd514a538on self-hosted Langfuse 3.227.1 with the media bucket on127.0.0.1. The four key and team host cells store a reference and a byte-identical file, wheree0fd39a3ddaleft a reference with no file and main keeps the image inlineLow
Final Attestation
Note
Medium Risk
Changes observability export behavior and adds async HTTP to operator-supplied storage URLs; failed or full-queue uploads can leave traces with references but no stored file, and proxy shutdown/flush timing now depends on media upload completion.
Overview
Restores Langfuse SDK v2-style handling of multimodal traces: base64 payloads in observation input/output/metadata are swapped for
@@@langfuseMedia:...@@@references before OTLP export, while decoded bytes are registered and uploaded via Langfuse’s media REST API (POST /api/public/media, storagePUT,PATCHconfirmation) on a per-credential background thread.Adds
litellm/integrations/langfuse/langfuse_media.pywithextract_media(data URIs, Anthropic/Vertex typed blocks, Geminiinline_data), bounded queue sizing viaLANGFUSE_MEDIA_QUEUE_MAX_BYTES(default 100 MB), fork-safe upload restarts, and no redirect following on storage PUTs.langfuse_sdk.pywraps the span exporter in_MediaExtractingExporter, builds aMediaUploaderfor live channels (skipped in mock mode), andLangfuseTracing.flushnow waits for pending/failed uploads as well as trace batches.Regression and integration tests cover extraction edge cases, upload retries, queue limits, and end-to-end proxy → Langfuse delivery without inline base64 in exported spans.
Reviewed by Cursor Bugbot for commit eaf02c8. Bugbot is set up for automated code reviews on this repo. Configure here.