diff --git a/src/rius/_media.py b/src/rius/_media.py index 9478b3e..099cc0e 100644 --- a/src/rius/_media.py +++ b/src/rius/_media.py @@ -239,20 +239,45 @@ def replacement(mime: str | None, byte_size: int | None) -> str: _REPLACEMENT_BETWEEN, _REPLACEMENT_TAIL = _rest.split("") -def is_replacement(value: str) -> bool: - """Already a complete replacement: never stripped again, never context. +#: 2^53 - 1 has 16 digits, so a run with more significant digits is past it. +_MAX_BYTE_SIZE_DIGITS = 16 - The format with a mime that is unknown or token/token and a byte_size of - ASCII digits or unknown; anything that only looks like one is a token. + +def parse_replacement(value: str) -> tuple[str | None, int | None] | None: + """The mime and byte_size a complete replacement names, each None when + unknown; None when ``value`` is no complete replacement (see + ``is_replacement``). + + The byte_size is the digit run read as a decimal number, leading zeros of + any length allowed. A run with more significant digits than 2^53 - 1 has + is None and is never converted: ``int()`` refuses a string over 4300 + digits, and this runs inside the export chain. The caller still bounds a + shorter one to 2^53 - 1, as for a byte_size the part has. """ head, between, tail = _REPLACEMENT_HEAD, _REPLACEMENT_BETWEEN, _REPLACEMENT_TAIL if len(value) < len(head) + len(between) + len(tail): - return False + return None if not (value.startswith(head) and value.endswith(tail)): - return False + return None mime, sep, size = value[len(head) : len(value) - len(tail)].rpartition(between) digits = bool(size) and all("0" <= ch <= "9" for ch in size) - return bool(sep) and (mime == UNKNOWN or is_mime(mime)) and (digits or size == UNKNOWN) + if not (sep and (mime == UNKNOWN or is_mime(mime)) and (digits or size == UNKNOWN)): + return None + byte_size = None + if digits: + significant = size.lstrip("0") or "0" + if len(significant) <= _MAX_BYTE_SIZE_DIGITS: + byte_size = int(significant) + return (None if mime == UNKNOWN else mime), byte_size + + +def is_replacement(value: str) -> bool: + """Already a complete replacement: never stripped again, never context. + + The format with a mime that is unknown or token/token and a byte_size of + ASCII digits or unknown; anything that only looks like one is a token. + """ + return parse_replacement(value) is not None # --- the strip --------------------------------------------------------------- @@ -1445,7 +1470,9 @@ def native_part(part: Part) -> Part | None: * A blob carrying content becomes a stripped blob: sized from it, ``not_captured``, its modality from its MIME type first (some producers - label every file an image), else the one it has, else document. + label every file an image), else the one it has, else document. Content + that is a complete replacement gives its size, and its mime when the + part has no valid MIME type. * Any other part keeps a modality it has, else takes one from its MIME type, else (a uri) from its filename extension, else document. A blob without content keeps a valid size and stripped_reason. @@ -1457,6 +1484,14 @@ def native_part(part: Part) -> Part | None: part_type = part.get("type") mime = part.get("mime_type") if part_type == "blob" and isinstance(part.get("content"), str): + replaced = parse_replacement(part["content"]) + if replaced is not None: + # Bytes a strip already measured (an OTLP bytes value, a byte + # object): the size the base64 of the same bytes would give, and + # the part's own MIME type before the replacement's. + replaced_mime, size = replaced + kept = mime if is_mime(mime) else replaced_mime + return blob_part(own or "document", kept, safe_size(size), NOT_CAPTURED) return base64_blob(part["content"], own or "document", mime) if part_type == "uri" and is_data_url(part.get("uri")): return data_url_blob(part["uri"], own or "document", mime) diff --git a/tests/conformance/_recording.py b/tests/conformance/_recording.py index 15c7248..cdfe0d4 100644 --- a/tests/conformance/_recording.py +++ b/tests/conformance/_recording.py @@ -1,4 +1,4 @@ -# Vendored from rius-semconv ab957f21453782a0e47350f3b6f964533785cf17 fixtures/_recording.py by +# Vendored from rius-semconv d826d04f4dbb87171ff763b65515a4b6355d81c5 fixtures/_recording.py by # scripts/sync-semconv-fixtures.sh. Do not edit: change it in rius-semconv. """Shared helpers for the recorders under fixtures//record.py. diff --git a/tests/conformance/_replay.py b/tests/conformance/_replay.py index 074047d..8091313 100644 --- a/tests/conformance/_replay.py +++ b/tests/conformance/_replay.py @@ -1,4 +1,4 @@ -# Vendored from rius-semconv ab957f21453782a0e47350f3b6f964533785cf17 fixtures/_replay.py by +# Vendored from rius-semconv d826d04f4dbb87171ff763b65515a4b6355d81c5 fixtures/_replay.py by # scripts/sync-semconv-fixtures.sh. Do not edit: change it in rius-semconv. """Turn a stored recording back into OpenTelemetry ReadableSpans (Python). diff --git a/tests/fixtures/semconv/SOURCE b/tests/fixtures/semconv/SOURCE index 63631e0..05606d1 100644 --- a/tests/fixtures/semconv/SOURCE +++ b/tests/fixtures/semconv/SOURCE @@ -1 +1 @@ -rius-semconv ab957f21453782a0e47350f3b6f964533785cf17 +rius-semconv d826d04f4dbb87171ff763b65515a4b6355d81c5 diff --git a/tests/fixtures/semconv/media_parts.yaml b/tests/fixtures/semconv/media_parts.yaml index 3e66333..ecc5fac 100644 --- a/tests/fixtures/semconv/media_parts.yaml +++ b/tests/fixtures/semconv/media_parts.yaml @@ -1453,7 +1453,7 @@ part_shapes: - 'Every native media part in a message key (blob, uri or file) is always rebuilt into its canonical shape: members in the pinned order, extra members dropped, and every member that is unknown omitted. The only exception is a uri part whose uri is not a string, or a file part whose file_id is not a string: it is left as it came. Any string is a usable uri or file_id, the empty string included. An object of type file that holds a base64 or a url that is a string is no native file part but a LangChain v1 file block, read by the v1 rule below: so {"type":"file","file_id":5,"url":"https://example.com/a.wav"} is a uri, while {"type":"file","file_id":5} is a native file part left as it came. Every other normalized media part carries a modality. generators/media_parts.py native_part(data, part) is the reference: each example''s native part, and the part in each in_place example, is its test.' - 'Parts are rebuilt only in a message-key value that parses as JSON and nests at most 1000 levels deep. Arrays and objects count together from the value''s root, which is level 1: the message array, a message, its parts array and a tool call part are 4 levels. A value nested deeper (a tool call whose arguments add 997 more arrays makes 1001 levels) is left as it came, every part in it included; at 1000 levels its parts are rebuilt. The implementation counts the depth itself and never relies on its parser''s or its language''s recursion limit. The strip still runs on a value left this way.' - 'The modality that the part already has is kept, when it is one of modalities (any other value is ignored), except for a blob whose content is a string and a uri holding a data URL (below). Otherwise it is taken from the MIME type. Failing that, a provider item whose kind names a modality takes it from the kind: an image, image_url, input_image or image_generation_call item is image, an input_audio item audio, a document item document. A LangChain v1 block of type image, audio or video is that modality; a block of type file names none, so by a url that is not a data URL it takes the modality from the extension of the URL''s path (see extensions), and otherwise it is document. A text-plain block by base64 names none either and is document (one by url or file_id makes no part). A Bedrock Converse image, video, audio or document block is that modality. An OpenAI file or input_file item names none; its modality comes from the extension of its filename member alone (never from a file_url), and with no filename, or an empty one, it is document. A Gemini file_data, a native uri and a Vercel file part by URL take it from the extension of their URI''s path (see extensions), the Vercel part whether its URL is a plain string or v7''s {"type":"url"} data; a Vercel image part is an image. Otherwise it is document. So an Anthropic document by URL ending in .png is a document, an OpenAI input_file named a.png is an image, and an input_file by a file_url ending in .png with no filename is a document; a LangChain v1 image block by a URL ending in .bin is an image, and a v1 file block by a URL ending in .wav is audio. A native file part with no valid modality and no MIME type gets document; no member of it is read for an extension. Any other part with no MIME type and no other hint also gets document.' - - 'A blob whose `content` is a string is rebuilt with stripped_reason not_captured and a byte_size taken from the content under the size rule of the strip (omitted when the size is unknown). Its modality comes from its MIME type first, then from the modality it has, then document: a PDF blob labelled image is a document. A content string that is itself a data URL follows the data URL rules below, with the part''s mime_type as the hint and the same modality order. A blob in a message key never carries content.' + - 'A blob whose `content` is a string is rebuilt with stripped_reason not_captured and a byte_size taken from the content under the size rule of the strip (omitted when the size is unknown). A content string that is a complete replacement (see raw_payloads idempotence), such as the marker `[stripped unknown bytes]` an OTLP bytes value or an SDK byte object becomes (raw_payloads byte_objects), names bytes a strip already measured: its byte_size is the replacement''s, read as a decimal number (leading zeros allowed) and omitted when unknown or larger than 2^53 - 1, and its MIME type is the part''s mime_type when that is valid, else the replacement''s when that is not unknown. Every complete replacement is read this way, not only the bytes marker: each is a strip''s record of bytes it removed. So a blob whose content is base64, with its MIME type under mime_type or with none, makes the same part whether it is rebuilt before or after a strip. That is all this promises. A blob whose MIME type is under another key the strip reads (mimeType, media_type, mediaType or output_format), or whose content is a data URL, can make a different part after a strip: the strip reads more keys and replaces a data URL whole, while the part rules read only mime_type and size a data URL from its run. A string that is not a complete replacement (a near one such as `[stripped image/png lots bytes]`, or one inside other text) is sized as base64 like any other. Its modality comes from its MIME type first, then from the modality it has, then document: a PDF blob labelled image is a document. A content string that is itself a data URL follows the data URL rules below, with the part''s mime_type as the hint and the same modality order. A blob in a message key never carries content.' - 'A blob with no string `content` keeps a stripped_reason that is one of stripped_reasons. Otherwise its stripped_reason is redacted_upstream when it has no content member or a null one, and not_captured when its content is any other value. It keeps a byte_size that is a whole, non-negative number no larger than 2^53 - 1 (8, 8.0 and 8e0 alike, written 8); any other byte_size is omitted.' - 'A uri part, or a provider URI, whose URI is a data URL is never a uri: it becomes a blob under the data URL rules below. A data URL is a value that starts with data:, or with data%3A (rule (b)''s percent_scheme: a data URL percent-encoded whole, as encodeURIComponent writes it), in any ASCII case. Nor is a native uri part or a provider URI whose value holds data: anywhere else, in any ASCII case (a value led by whitespace, or a link with a data URL after it, wrapped, percent-encoded or not): it becomes a blob with the stripped_reason not_captured and no byte_size, its MIME type kept when valid, so no byte of the data URL survives. The provider URIs are an OpenAI image_url''s url and input_image''s image_url, an Anthropic image or document by a url source, an OpenAI input_file''s file_url, a Gemini file_data''s file_uri, a Vercel file or image part by URL (a plain string that starts with a URL scheme, or v7''s {"type":"url"} data), and the image url OpenInference flattens; a LangChain v1 url is read the same way (below). Any other value stays a uri as it came, a link that holds data%3A and no data: included (a proxy link with an encoded data URL in its query): the strip then replaces the encoded data URL inside it where it sits, as rule (b) does wherever it runs. A native uri part takes that blob''s modality as a blob whose content is a string does: from the MIME type first, then the modality it has, then document. A provider URI takes it from the MIME type the item gives (an Anthropic source''s media_type, a Gemini file_data''s mime_type, a Vercel part''s mediaType, the image media_type OpenInference flattens beside its url), then from the item''s kind (an image_url, an input_image and a Vercel image part are image, an Anthropic image or document that kind, an input_file the modality its filename names), then document: such a value has no path to read an extension from. The test is one search for [dD][aA][tT][aA]: with no trimming and no word boundary, so a link with data: in its path (https://e/metadata:x.png) becomes this blob too: a lost link, never a leak. generators/media_parts.py provider_uri_part(data, item) is the reference for a provider URI, and each example''s provider item is its test.' - 'A URL of any scheme other than data: is a uri, its link kept as written: http, https, gs, s3, file and any other scheme alike. Only a data: URL, or inline data, becomes a blob, and a native uri, a provider URI or a LangChain v1 url that holds data: anywhere else (above and below). Where one value may hold either a URL or bare base64 (a Vercel file part''s data or image part''s image, as a plain string or as v7''s {"type":"data"} data), it is a URL when it starts with a scheme: an ASCII letter, then any ASCII letters, digits, `+`, `-` or `.`, then a colon. Base64 holds no colon, so the two never meet; anything else is base64, but a value that starts with data%3A, which is a data URL (above). A value v7 tags {"type":"url"} is a URL whatever it holds. The strip reads these values the same way: rule (e) leaves a value that starts with a scheme to rule (b) (raw_payloads, rule (e)''s url_scheme and keeps_urls), except a value holding data: that rule (b)''s pattern does not match in full (a percent-encoded data URL, a link with a data URL after it), which rule (e) replaces whole, as the part rules make it a blob.' @@ -1486,6 +1486,10 @@ part_shapes: from: 'The string base64 makes the object a v1 file block, and as a usable reference it wins over the __REDACTED__ file_id, so it is a blob of 3 bytes.' input: '[ {"role": "user", "parts": [ {"type": "file", "file_id": "__REDACTED__", "base64": "QUJD"} ] } ]' output: '[ {"role": "user", "parts": [ {"type":"blob","modality":"document","byte_size":3,"stripped_reason":"not_captured"} ] } ]' + - name: blob_whose_content_is_the_bytes_marker + from: 'A spaced message value holding a native blob whose content is the bytes marker: the part takes the size the marker names.' + input: '[ {"role": "user", "parts": [ {"type": "blob", "modality": "image", "mime_type": "image/png", "content": "[stripped unknown 16 bytes]"} ] } ]' + output: '[ {"role": "user", "parts": [ {"type":"blob","modality":"image","mime_type":"image/png","byte_size":16,"stripped_reason":"not_captured"} ] } ]' examples: - name: blob_with_a_known_size from: 'A base64 data URL of image/png, 8 bytes decoded.' @@ -1511,6 +1515,54 @@ part_shapes: from: 'A native blob part with the mime_type image/png and a content member holding data:;base64, which names no mime type and has an empty run: the part''s mime_type is the hint.' native: '{"type":"blob","mime_type":"image/png","content":"data:;base64,"}' part: '{"type":"blob","modality":"image","mime_type":"image/png","byte_size":0,"stripped_reason":"not_captured"}' + - name: blob_with_content_that_is_the_bytes_marker + from: 'A native blob part whose content is the marker [stripped unknown 16 bytes], as a bytes value inside a structured message becomes before the part is rebuilt: the same 16 bytes sent as base64 make the same part.' + native: '{"type":"blob","modality":"image","mime_type":"image/png","content":"[stripped unknown 16 bytes]"}' + part: '{"type":"blob","modality":"image","mime_type":"image/png","byte_size":16,"stripped_reason":"not_captured"}' + - name: blob_with_content_that_is_a_replacement_with_a_mime_and_no_hint + from: 'A native blob part whose content is the complete replacement [stripped image/jpeg 10 bytes] and that has neither a MIME type nor a modality: the replacement''s mime is the MIME type, and gives the modality.' + native: '{"type":"blob","content":"[stripped image/jpeg 10 bytes]"}' + part: '{"type":"blob","modality":"image","mime_type":"image/jpeg","byte_size":10,"stripped_reason":"not_captured"}' + - name: blob_with_content_that_is_a_replacement_and_its_own_mime_type + from: 'A native blob part with the mime_type application/pdf whose content is the complete replacement [stripped image/png 8 bytes]: the part''s own valid mime_type wins over the replacement''s.' + native: '{"type":"blob","mime_type":"application/pdf","content":"[stripped image/png 8 bytes]"}' + part: '{"type":"blob","modality":"document","mime_type":"application/pdf","byte_size":8,"stripped_reason":"not_captured"}' + - name: blob_with_content_that_is_a_replacement_of_unknown_size + from: 'A native blob part with the modality audio whose content is the complete replacement [stripped unknown unknown bytes]: its size and mime are unknown, so both are omitted.' + native: '{"type":"blob","modality":"audio","content":"[stripped unknown unknown bytes]"}' + part: '{"type":"blob","modality":"audio","stripped_reason":"not_captured"}' + - name: blob_with_content_that_is_a_replacement_too_large_to_keep + from: 'A native blob part whose content is the complete replacement [stripped unknown 9007199254740992 bytes]: a size larger than 2^53 - 1 is omitted, as for a byte_size the part has.' + native: '{"type":"blob","content":"[stripped unknown 9007199254740992 bytes]"}' + part: '{"type":"blob","modality":"document","stripped_reason":"not_captured"}' + - name: blob_with_content_that_is_a_replacement_with_leading_zeros + from: 'A native blob part whose content is the complete replacement [stripped image/png 0008 bytes]: its size is the decimal number 8.' + native: '{"type":"blob","content":"[stripped image/png 0008 bytes]"}' + part: '{"type":"blob","modality":"image","mime_type":"image/png","byte_size":8,"stripped_reason":"not_captured"}' + - name: blob_with_content_that_is_a_replacement_with_thousands_of_leading_zeros + from: 'A native blob part whose content is a complete replacement whose size is 5000 zeros then 8: leading zeros of any length are allowed, so its size is 8.' + native: '{"type":"blob","content":"[stripped unknown 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008 bytes]"}' + part: '{"type":"blob","modality":"document","byte_size":8,"stripped_reason":"not_captured"}' + - name: blob_with_content_that_is_a_replacement_whose_size_overflows_int64 + from: 'A native blob part whose content is the complete replacement [stripped image/png 99999999999999999999 bytes]: a size past any 64-bit integer is larger than 2^53 - 1, so it is omitted, and the replacement''s mime is still read.' + native: '{"type":"blob","content":"[stripped image/png 99999999999999999999 bytes]"}' + part: '{"type":"blob","modality":"image","mime_type":"image/png","stripped_reason":"not_captured"}' + - name: blob_with_content_that_is_a_replacement_with_thousands_of_digits + from: 'A native blob part whose content is a complete replacement whose size is 5000 nines: far larger than 2^53 - 1, so it is omitted, and reading it never fails.' + native: '{"type":"blob","content":"[stripped unknown 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 bytes]"}' + part: '{"type":"blob","modality":"document","stripped_reason":"not_captured"}' + - name: blob_with_content_that_only_looks_like_a_replacement + from: 'A native blob part with the mime_type image/png whose content is [stripped image/png lots bytes]: not a complete replacement, so it is sized as base64, which it is not, and has no byte_size.' + native: '{"type":"blob","mime_type":"image/png","content":"[stripped image/png lots bytes]"}' + part: '{"type":"blob","modality":"image","mime_type":"image/png","stripped_reason":"not_captured"}' + - name: blob_with_content_that_is_a_replacement_with_an_invalid_mime + from: 'A native blob part whose content is [stripped a b 3 bytes]: a b is no valid mime, so it is not a complete replacement, and its 3 is never read.' + native: '{"type":"blob","content":"[stripped a b 3 bytes]"}' + part: '{"type":"blob","modality":"document","stripped_reason":"not_captured"}' + - name: blob_with_content_that_holds_a_replacement_in_text + from: 'A native blob part whose content is the text see [stripped unknown 16 bytes]: a replacement inside other text is not a complete replacement, so the content is sized as base64, which it is not, and has no byte_size.' + native: '{"type":"blob","content":"see [stripped unknown 16 bytes]"}' + part: '{"type":"blob","modality":"document","stripped_reason":"not_captured"}' - name: blob_with_no_content_and_no_stripped_reason from: 'A native blob part with the modality audio and neither content nor stripped_reason.' native: '{"type":"blob","modality":"audio"}' diff --git a/tests/test_byte_objects.py b/tests/test_byte_objects.py index a76a75f..972ba0b 100644 --- a/tests/test_byte_objects.py +++ b/tests/test_byte_objects.py @@ -519,3 +519,95 @@ def test_model_parameters_holding_byte_objects(exported_spans: InMemorySpanExpor assert written assert not any(_leaks(str(v)) for v in written.values()) assert all(MARKER in str(v) for v in written.values()), written + + +def test_a_blob_of_bytes_in_messages_is_stored_as_its_base64_twin() -> None: + # The SDK's serializer writes the bytes as the marker and strips the + # base64 to a replacement, and the exporter rebuilds each blob with the + # size its replacement names: both forms keep byte_size and store alike. + def messages(content: Any) -> str: + blob = {"type": "blob", "modality": "image", "mime_type": "image/png", "content": content} + return serialize([{"role": "user", "parts": [blob]}]) + + inner = InMemorySpanExporter() + client = init(span_exporter=inner, set_global=False) + for name, content in (("bytes", PNG), ("base64", "iVBORw0KGgo=")): + span = client.get_tracer().start_span(name) + span.set_attribute("gen_ai.input.messages", messages(content)) + span.end() + client.flush() + stored = { + s.name: (s.attributes or {})["gen_ai.input.messages"] for s in inner.get_finished_spans() + } + assert stored["bytes"] == stored["base64"] + [part] = json.loads(str(stored["bytes"]))[0]["parts"] + assert part == { + "type": "blob", + "modality": "image", + "mime_type": "image/png", + "byte_size": 8, + "stripped_reason": "not_captured", + } + + +#: A complete replacement whose size has more digits than Python's int() +#: converts from a string (4300, leading zeros included). +_LONG_REPLACEMENT = "[stripped unknown " + "9" * 4301 + " bytes]" + + +@pytest.mark.parametrize( + ("key", "value", "stored"), + [ + pytest.param( + "gen_ai.input.messages", + json.dumps( + [{"role": "user", "parts": [{"type": "blob", "content": _LONG_REPLACEMENT}]}] + ), + # Rebuilt with no byte_size: the size is past 2^53 - 1. + '"parts": [{"type":"blob","modality":"document","stripped_reason":"not_captured"}]', + id="blob-content", + ), + pytest.param( + "output", + json.dumps({"mime_type": _LONG_REPLACEMENT, "data": "QUJD"}), + _LONG_REPLACEMENT, + id="mime-type", + ), + pytest.param( + "output", + json.dumps({"type": "image", "source_type": "base64", "data": _LONG_REPLACEMENT}), + _LONG_REPLACEMENT, + id="data", + ), + ], +) +def test_a_replacement_with_a_long_digit_run_is_exported(key: str, value: str, stored: str) -> None: + # int() refuses such a run, and the strip asks whether each of these + # strings is a replacement: the export must still deliver the span. + inner = InMemorySpanExporter() + client = init(span_exporter=inner, set_global=False) + span = client.get_tracer().start_span("long") + span.set_attribute(key, value) + span.end() + client.flush() + [exported] = inner.get_finished_spans() + assert stored in str((exported.attributes or {})[key]) + + +def test_a_replacement_with_thousands_of_leading_zeros_names_its_size() -> None: + content = "[stripped unknown " + "0" * 5000 + "8 bytes]" + value = json.dumps([{"role": "user", "parts": [{"type": "blob", "content": content}]}]) + inner = InMemorySpanExporter() + client = init(span_exporter=inner, set_global=False) + span = client.get_tracer().start_span("zeros") + span.set_attribute("gen_ai.input.messages", value) + span.end() + client.flush() + [exported] = inner.get_finished_spans() + [part] = json.loads(str((exported.attributes or {})["gen_ai.input.messages"]))[0]["parts"] + assert part == { + "type": "blob", + "modality": "document", + "byte_size": 8, + "stripped_reason": "not_captured", + } diff --git a/tests/test_media_parts.py b/tests/test_media_parts.py index 5c9c9e1..09ce4eb 100644 --- a/tests/test_media_parts.py +++ b/tests/test_media_parts.py @@ -116,6 +116,59 @@ def test_a_part_with_canonical_values_is_canonical(part: dict[str, Any]) -> None }, "blob_with_no_content_and_no_stripped_reason": {"type": "blob", "modality": "audio"}, "blob_with_content_that_is_not_a_string": {"type": "blob", "content": 5, "byte_size": 8.0}, + "blob_with_content_that_is_the_bytes_marker": { + "type": "blob", + "modality": "image", + "mime_type": "image/png", + "content": "[stripped unknown 16 bytes]", + }, + "blob_with_content_that_is_a_replacement_with_a_mime_and_no_hint": { + "type": "blob", + "content": "[stripped image/jpeg 10 bytes]", + }, + "blob_with_content_that_is_a_replacement_and_its_own_mime_type": { + "type": "blob", + "mime_type": "application/pdf", + "content": "[stripped image/png 8 bytes]", + }, + "blob_with_content_that_is_a_replacement_of_unknown_size": { + "type": "blob", + "modality": "audio", + "content": "[stripped unknown unknown bytes]", + }, + "blob_with_content_that_is_a_replacement_too_large_to_keep": { + "type": "blob", + "content": "[stripped unknown 9007199254740992 bytes]", + }, + "blob_with_content_that_is_a_replacement_with_leading_zeros": { + "type": "blob", + "content": "[stripped image/png 0008 bytes]", + }, + "blob_with_content_that_is_a_replacement_with_thousands_of_leading_zeros": { + "type": "blob", + "content": "[stripped unknown " + "0" * 5000 + "8 bytes]", + }, + "blob_with_content_that_is_a_replacement_whose_size_overflows_int64": { + "type": "blob", + "content": "[stripped image/png 99999999999999999999 bytes]", + }, + "blob_with_content_that_is_a_replacement_with_thousands_of_digits": { + "type": "blob", + "content": "[stripped unknown " + "9" * 5000 + " bytes]", + }, + "blob_with_content_that_only_looks_like_a_replacement": { + "type": "blob", + "mime_type": "image/png", + "content": "[stripped image/png lots bytes]", + }, + "blob_with_content_that_is_a_replacement_with_an_invalid_mime": { + "type": "blob", + "content": "[stripped a b 3 bytes]", + }, + "blob_with_content_that_holds_a_replacement_in_text": { + "type": "blob", + "content": "see [stripped unknown 16 bytes]", + }, "uri_with_no_mime_type_and_no_modality": { "type": "uri", "uri": "https://example.com/download",