Repository navigation
Conversation
|
Ran The upload path still drops NamedTuple fields, though. |
|
@ayushmnnit I added the upload-path fix so named tuple fields are preserved during media processing. Thanks for the review 👍 |
ChinmayBitne
left a comment
There was a problem hiding this comment.
One tuple-subclass edge case worth checking in the current implementation.
| return type(obj)(*traversed_items) | ||
| except TypeError: | ||
| return obj | ||
| return traversed_items if type(obj) is tuple else obj |
There was a problem hiding this comment.
I noticed one tuple-subclass case that the new branch doesn't cover. For a tuple subclass without _fields, the function recursively resolves its elements (including fetching media) but returns the original object on line 428, discarding those resolved values.
For example:
class Payload(tuple):
pass
payload = Payload(("response", reference_string))
resolved = LangfuseMedia.resolve_media_references(
obj=payload,
langfuse_client=mock_langfuse_client,
resolve_with="base64_data_uri",
)
# Currently resolved[1] is still the original @@@langfuseMedia:...@@@ token,
# even though the media fetch was performed.The upload traversal now processes these tuple subclasses, so the two paths behave differently. Would it make sense to either support reconstructible tuple subclasses during resolution or skip traversal entirely for unsupported subclasses, to avoid unnecessary downloads? A small regression test for this would help define the intended behavior.
|
Checked 5463d65: 41 passed, and the same script now gives |
Summary
Fixes langfuse/langfuse#20462
Verification
tests/unit/test_media_manager.py::test_media_upload_retries_on_retryable_http_status PASSED [ 2%]
tests/unit/test_media_manager.py::test_media_upload_gives_up_on_non_retryable_http_status PASSED [ 5%]
tests/unit/test_media_manager.py::test_find_and_process_media_sse_done_passes_through PASSED [ 7%]
tests/unit/test_media_manager.py::test_find_and_process_media_sse_json_payload_passes_through PASSED [ 10%]
tests/unit/test_media_manager.py::test_find_and_process_media_valid_base64_uri_is_processed PASSED [ 12%]
tests/unit/test_media_manager.py::test_upload_media_sync_rejects_invalid_media
-------------------------------- live log call ---------------------------------
ERROR langfuse:media.py:166 base64_data_uri, or content_bytes and content_type, or file_path must be provided to LangfuseMedia
PASSED [ 15%]
tests/unit/test_media_manager.py::test_find_and_process_media_data_uri_without_comma_passes_through PASSED [ 17%]
tests/unit/test_media_manager.py::test_find_and_process_media_non_base64_data_uri_passes_through PASSED [ 20%]
tests/unit/test_media_manager.py::test_find_and_process_media_sse_in_nested_structure_passes_through PASSED [ 23%]
tests/unit/test_media_manager.py::test_find_and_process_media_in_tuple_queues_upload PASSED [ 25%]
tests/unit/test_media_manager.py::test_find_and_process_media_gemini_inline_data_is_processed PASSED [ 28%]
tests/unit/test_media_manager.py::test_find_and_process_media_gemini_inline_data_camel_case_is_processed PASSED [ 30%]
tests/unit/test_media_manager.py::test_find_and_process_media_gemini_inline_data_non_string_data_passes_through[image-bytes] PASSED [ 33%]
tests/unit/test_media_manager.py::test_find_and_process_media_gemini_inline_data_non_string_data_passes_through[123] PASSED [ 35%]
tests/unit/test_media_manager.py::test_find_and_process_media_gemini_inline_data_non_string_data_passes_through[inline_data_value2] PASSED [ 38%]
tests/unit/test_media_manager.py::test_media_api_calls_do_not_multiply_client_retries[429] PASSED [ 41%]
tests/unit/test_media_manager.py::test_media_api_calls_do_not_multiply_client_retries[503] PASSED [ 43%]
tests/unit/test_media_manager.py::test_media_api_calls_share_one_retry_budget_across_failure_kinds PASSED [ 46%]
tests/unit/test_media.py::test_init_with_base64_data_uri PASSED [ 48%]
tests/unit/test_media.py::test_init_with_content_bytes PASSED [ 51%]
tests/unit/test_media.py::test_init_with_invalid_input
-------------------------------- live log call ---------------------------------
ERROR langfuse:media.py:166 base64_data_uri, or content_bytes and content_type, or file_path must be provided to LangfuseMedia
ERROR langfuse:media.py:166 base64_data_uri, or content_bytes and content_type, or file_path must be provided to LangfuseMedia
ERROR langfuse:media.py:166 base64_data_uri, or content_bytes and content_type, or file_path must be provided to LangfuseMedia
PASSED [ 53%]
tests/unit/test_media.py::test_content_length PASSED [ 56%]
tests/unit/test_media.py::test_content_sha256_hash PASSED [ 58%]
tests/unit/test_media.py::test_reference_string PASSED [ 61%]
tests/unit/test_media.py::test_parse_reference_string PASSED [ 64%]
tests/unit/test_media.py::test_parse_invalid_reference_string PASSED [ 66%]
tests/unit/test_media.py::test_media_reference_is_url_expired[None-False] PASSED [ 69%]
tests/unit/test_media.py::test_media_reference_is_url_expired[not-a-date-False] PASSED [ 71%]
tests/unit/test_media.py::test_media_reference_is_url_expired[2000-01-01T00:00:00+00:00-True] PASSED [ 74%]
tests/unit/test_media.py::test_media_reference_is_url_expired[2999-01-01T00:00:00+00:00-False] PASSED [ 76%]
tests/unit/test_media.py::test_media_reference_is_url_expired[2000-01-01T00:00:00Z-True] PASSED [ 79%]
tests/unit/test_media.py::test_file_handling PASSED [ 82%]
tests/unit/test_media.py::test_nonexistent_file
-------------------------------- live log call ---------------------------------
ERROR langfuse:media.py:166 base64_data_uri, or content_bytes and content_type, or file_path must be provided to LangfuseMedia
PASSED [ 84%]
tests/unit/test_media.py::test_media_reference_fetch_uses_configured_httpx_client PASSED [ 87%]
tests/unit/test_media.py::test_media_reference_fetch_uses_explicit_client PASSED [ 89%]
tests/unit/test_media.py::test_media_reference_fetch_falls_back_to_default_with_multiple_clients
-------------------------------- live log call ---------------------------------
WARNING langfuse:resource_manager.py:102 Multiple Langfuse clients are instantiated; falling back to a default httpx client for LangfuseMediaReference fetches. Pass an explicit
clientto fetch_bytes/fetch_base64/fetch_data_uri to honor per-client transport settings.PASSED [ 92%]
tests/unit/test_media.py::test_resolve_media_references_uses_configured_httpx_client PASSED [ 94%]
tests/unit/test_media.py::test_resolve_media_references_in_tuple PASSED [ 97%]
tests/unit/test_media.py::test_init_with_urlsafe_base64_data_uri PASSED [100%]
============================== 39 passed in 0.37s ============================== — 39 passed
The PR appears safe to merge, with a non-blocking compatibility issue for callers that pass named tuples to reference resolution.
Summary
Adds tuple traversal to media discovery and reference resolution, with focused tests for both paths.
Reviews (1) · Last reviewed commit: "fix(media): traverse tuples for media pr..." · Reviewed by Greptile