litellm/tests/unit/integrations/langfuse
yucheng-berri 14c4a6f1f2
fix(langfuse): upload base64 media and export its reference from the langfuse callback (#53934)
* fix(langfuse): upload base64 media and export its reference from the 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.

* fix(langfuse): bound queued media by bytes and stop trusting the storage 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.

* fix(langfuse): validate upload URLs for request-supplied hosts and queue 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.

* fix(langfuse): keep media inline while Langfuse cannot store it

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.

* fix(langfuse): re-send a media registration that does not reach Langfuse

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.

* fix(langfuse): replace base64 media in model parameters

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.

* Revert "fix(langfuse): replace base64 media in model parameters"

This reverts commit 3a4894c78e.

* fix(langfuse): leave the media storage response body unread

Only the status of the storage PUT is used, and an upload URL can answer with a body of any size.

* fix(langfuse): always export the media reference, as the Langfuse SDK does

Drops the pause that kept media inline after the media API failed. A self-hosted Langfuse needs a media bucket, as its docs state.

* fix(langfuse): read the HTTP client from the handler on every media upload

litellm closes a pooled client once its cache entry expires (one hour plus
the close grace) and the handler opens another. The uploader held the first
client, so later uploads raised 'client has been closed' and traces kept a
reference with no file. The span exporter already reads handler.client per
request.

* perf(langfuse): skip the media parse for values that only mention base64

extract_media parsed and walked any value that held the text base64 or
mime_type. It now parses only a value that holds a base64 data URI, or a
content type key next to a data key, which every payload it replaces has.

* test(langfuse): check media-free values by their output only

* fix(langfuse): stop validating the media upload URL for key and team 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.

* fix(langfuse): restart the media upload thread in a forked child

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.

* fix(langfuse): upload Gemini inline_data parts as media

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.

* fix(langfuse): size the media upload queue from an env setting and stop 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.

* fix(langfuse): give a forked child its own failure lock

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.
2026-11-27 11:29:16 -08:00
..
langfuse_expected_request_body fix(cost): preserve Bedrock cross-region pricing for Messages (#51479) 2026-11-12 15:54:56 -08:00
__init__.py
test_gemini_cached_tokens.py
test_langfuse.py refactor(types): type the dict-style accessors on the response models and their read sites (#54620) 2026-11-26 04:15:22 +00:00
test_langfuse_dynamic_credentials.py
test_langfuse_handler.py fix(otel v2): stop legacy langfuse loggers sending to refused key/team hosts (#51344) 2026-11-16 11:34:20 -08:00
test_langfuse_media.py fix(langfuse): upload base64 media and export its reference from the langfuse callback (#53934) 2026-11-27 11:29:16 -08:00
test_langfuse_prompt_management.py fix(otel v2): stop legacy langfuse loggers sending to refused key/team hosts (#51344) 2026-11-16 11:34:20 -08:00
test_langfuse_sdk.py fix(langfuse): upload base64 media and export its reference from the langfuse callback (#53934) 2026-11-27 11:29:16 -08:00