Repository navigation
fix(artifacts): keep JSON value types in GCS artifact custom_metadata - #8402
Open
sumedh-aerram wants to merge 9 commits into
Open
sumedh-aerram wants to merge 9 commits into
sumedh-aerram wants to merge 9 commits into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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."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 usedadkCustomMetadatanext to other keys, even with a JSON object value, returns every key as a plain string. Decoding never raises.None,{}, or only reserved keys), the reserved key isn't written, so those blobs are stored byte-for-byte as today.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-suppliedadkCustomMetadatakey on save. Its own writes still storestr(v)per key, so typed values it saves still come back as strings; that is left for a follow-up.allow_nan=False) raiseInputValidationError(aValueError) at save time. No blob is written. Callers can convert explicitly, for example withdt.isoformat().InputValidationErrorover 8192 bytes. Nothing is truncated. Non-ASCII characters are stored unescaped (ensure_ascii=False), soécounts as 2 bytes, not 6.adkCustomMetadatainstead of one entry per key.Testing Plan
Unit Tests:
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:
"caller value","","3","null","[1]","{",'{"x": 1}',"{}").None,{}, reserved keys only) writes noadkCustomMetadatakey.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-storageclient, throughSTORAGE_EMULATOR_HOSTand 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
Additional context
No public signatures or schemas change.