Skip to content

fix(artifacts): keep JSON value types in GCS artifact custom_metadata - #8402

Open
sumedh-aerram wants to merge 9 commits into
google:mainfrom
sumedh-aerram:fix/gcs-artifact-metadata-types
Open

sumedh-aerram wants to merge 9 commits into
google:mainfrom
sumedh-aerram:fix/gcs-artifact-metadata-types

Conversation

@sumedh-aerram

@sumedh-aerram sumedh-aerram commented Nov 27, 2026 •

Copy link
Copy Markdown

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

Fixes #8401

Problem: GcsArtifactService applies str() to custom_metadata values. Integers, booleans, lists, and nested objects therefore change type when read through either artifact-version API. False becomes a truthy string.

Solution: GCS metadata values can only be strings, so the caller's custom_metadata is stored as one JSON string under the reserved blob metadata key adkCustomMetadata. Both version readers (get_artifact_version, list_artifact_versions) decode it.

  • Legacy blobs: blobs without the key keep today's behavior. Their values come back as plain strings, so old "3"/"true" values are not guessed into int/bool. The reserved key is decoded only when it is the blob's sole user key (after ADK's bookkeeping keys are stripped) and holds a JSON object. A legacy blob where the caller used adkCustomMetadata next to other keys, even with a JSON object value, returns every key as a plain string. Decoding never raises.
  • Empty metadata: when there is no caller metadata (None, {}, or only reserved keys), the reserved key isn't written, so those blobs are stored byte-for-byte as today.
  • FsspecArtifactService: it reads the same objects on gs://, so it now decodes the reserved key through the same shared helper (artifact_util._decode_gcs_custom_metadata), and its reads of GCS-written artifacts match GcsArtifactService. Like GcsArtifactService, it drops a caller-supplied adkCustomMetadata key on save. Its own writes still store str(v) per key, so typed values it saves still come back as strings; that is left for a follow-up.
  • Values JSON can't encode (datetime, bytes, sets, custom objects, and NaN/Infinity via allow_nan=False) raise InputValidationError (a ValueError) at save time. No blob is written. Callers can convert explicitly, for example with dt.isoformat().
  • JSON round trip: tuples come back as lists, and non-string dict keys come back as strings.
  • Size limit: GCS caps an object's custom metadata at 8 KiB (keys plus values). Before upload, the service sums the UTF-8 byte length of every key and value it sends, including its own bookkeeping keys, and raises InputValidationError over 8192 bytes. Nothing is truncated. Non-ASCII characters are stored unescaped (ensure_ascii=False), so é counts as 2 bytes, not 6.
  • Compatibility note: for new writes, tools that read per-key GCS object metadata directly see one JSON string under adkCustomMetadata instead of one entry per key.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

pytest tests/unittests/artifacts tests/unittests/integrations/fsspec -q: 2505 passed, 29 xfailed at e71ff30; the full unit suite shows no new failures, and an emulator run at the same commit passed 18 scenarios plus GCS↔fsspec reads in both directions.

Cases cover the following:

  • Typed round trip on the in-memory, file, and mocked GCS services, for session and user scopes, through both version readers.
  • Legacy strings stay strings. Old and new blobs can be mixed in one version list.
  • A legacy reserved key next to other keys reads as legacy strings, whether it holds non-JSON, non-object, or JSON object values ("caller value", "", "3", "null", "[1]", "{", '{"x": 1}', "{}").
  • Saving with no caller metadata (None, {}, reserved keys only) writes no adkCustomMetadata key.
  • datetime, bytes, object, and set values, and NaN, Infinity, and -Infinity (including nested), are rejected.
  • 8192 bytes saves and 8193 is rejected, for ASCII and for 2-byte UTF-8 values.
  • Tuples come back as lists and int keys as strings.
  • fsspec reads GCS-written typed and legacy metadata the same way as GcsArtifactService.

Pre-commit and mypy pass on the changed files.

Manual End-to-End (E2E) Tests:

Emulator only, no real GCS bucket. I used fake-gcs-server v1.56.1 with the real google-cloud-storage client, through STORAGE_EMULATOR_HOST and anonymous credentials. Typed values round-tripped. Legacy and mixed old/new versions read correctly. datetime and bytes were rejected without writing a blob. At the 8 KiB boundary, 8192 bytes saved and 8193 bytes was rejected by the service. The emulator does not enforce the real GCS limit, so the boundary itself has not been checked against GCS. The emulator run was before the fsspec/legacy-key, UTF-8, sole-key, empty-metadata, and NaN follow-up commits, which are covered by the unit tests above. A real-bucket round trip and the 8192/8193 boundary check are pending.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end. (Not against a real bucket yet; end-to-end runs used fake-gcs-server with the real client, see above.)
  • Any dependent changes have been merged and published in downstream modules.

Additional context

No public signatures or schemas change.

Store new custom metadata in a JSON envelope and decode it in both version readers. Preserve legacy string-valued metadata without inferring its original types.

Fixes google#8401
…ore upload

custom_metadata is stored as one JSON document under the adkCustomMetadata
blob metadata key. Values json.dumps cannot encode (datetime, bytes, sets,
custom objects) now raise InputValidationError naming the supported types,
and metadata over the GCS 8 KiB custom metadata limit raises before any
blob is written instead of failing in the upload or being truncated.
FsspecArtifactService reading gs:// objects written by GcsArtifactService
returned the raw adkCustomMetadata JSON envelope. Both services now decode
it through one shared helper in artifact_util, and fsspec drops the
reserved key on save as GcsArtifactService does.

Decoding never raises: if a legacy blob already holds a caller value under
adkCustomMetadata that is not a JSON object, every key is returned as a
plain string.

Store non-ASCII metadata unescaped (ensure_ascii=False) so the 8 KiB check
counts its real UTF-8 size, and document that tuples come back as lists
and non-string keys as strings.
A lone surrogate such as "\ud800" passes json.dumps with
ensure_ascii=False but cannot be encoded as UTF-8, so the size check
raised UnicodeEncodeError. Raise InputValidationError before anything is
written, and cover too-deeply nested legacy values on read.
A legacy blob where the caller stored a JSON object under
adkCustomMetadata next to other keys decoded to just that object and
dropped the rest. Decode the reserved key only when it is the blob's
sole user key; otherwise return every key as a plain string.

Skip the reserved key when there is no caller metadata, so those blobs
are stored exactly as before, and pass allow_nan=False so NaN and
infinity raise InputValidationError before anything is written.
…oss modules

decode_gcs_custom_metadata and GCS_CUSTOM_METADATA_KEY in artifact_util are
used by GcsArtifactService and the fsspec integration, so per the visibility
rule from 62a488d they no longer carry a leading underscore.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GcsArtifactService converts JSON-native custom_metadata values to strings

1 participant