fix(metadata): harden discriminator heuristics, iTXt chunks, scalar fallback#268
Merged
Conversation
…allback
Two real fragility items from the code review, plus a small adjacent
cleanup. The "defensive int/float coercion in InvokeMetadataView" item
the reviewer also flagged turned out to be a non-issue — see the
"intentionally not changed" section below.
## 1. ``GenerationMetadataAdapter`` discriminator heuristics
For pre-discriminator payloads (no ``metadata_version`` key), the
adapter has to infer the schema version. The previous logic had three
real issues:
* **``canvas_v2_metadata`` shadowed ``app_version``.** A v3 image that
happened to carry a ``canvas_v2_metadata`` key was forced to v5,
regardless of what ``app_version`` said.
* **``v3.`` prefix not handled.** The v2 branch accepted both bare and
``v``-prefixed strings (``["v1.", "2.", "v2."]``); the v3 branch only
matched ``3.``, so ``v3.4.0`` fell into the catch-all "future = v5"
arm.
* **Logic spread across four nested branches** made it hard to reason
about.
Refactored into a ``_infer_metadata_version`` staticmethod with a
clear priority: ``app_version`` first (it's authoritative when
present), structural fingerprints (``canvas_v2_metadata`` →
v5, ``model_weights`` → v2) as the fallback. Both bare and
``v``-prefixed strings are accepted for v1/v2 and v3.
Seven unit tests cover the heuristic in isolation (no full schema
construction needed — the heuristic just picks a version).
## 2. PNG ``iTXt`` / ``zTXt`` chunks in ``metadata_extraction.py``
Pillow returns different shapes for the three PNG text-chunk types:
* ``tEXt`` → ``str``
* ``zTXt`` → ``str`` (decompressed) in recent Pillow, ``bytes`` historically
* ``iTXt`` → ``(text, lang, translated_keyword)`` tuple in some versions
Without normalization, an ``iTXt`` chunk handed ``json.loads`` a tuple
(TypeError on the next line) and the entire metadata block was lost
— the image rendered with the EXIF fallback or nothing at all.
Adds ``_normalize_text_chunk`` that funnels tuple → first-element,
bytes → utf-8 string, then ``str(...)``. The three JSON extractors all
flow through it.
Adjacent cleanups in the same file:
* ``except (json.JSONDecodeError, Exception)`` (the broader class
subsumes the narrower) → just ``Exception``.
* Four ``print("Warning: ...")`` calls → ``logger.warning(...)`` so
they respect log configuration.
## 3. Scalar-table fallback for unparseable payloads
``invoke_formatter`` used to render an opaque ``"Unknown invoke
metadata format"`` placeholder when the discriminated union rejected
a payload (unknown future version, malformed shape, etc.). Now it
extracts whatever top-level **scalar** fields are present and renders
them via the existing ``_scalar_table`` — the user still sees the
seed / model / prompt etc. The placeholder remains for payloads with
zero scalar fields.
## Intentionally not changed
The original review item on ``int(self.seed)`` / ``float(lora.weight)``
in the view turned out to be moot: the Pydantic schemas declare these
as ``int`` and ``float`` respectively, which the v2 validator
strict-rejects non-numeric strings against at parse time. By the time
the view sees a value, it's already coerced. Adding defensive
``try/except`` would be unreachable code, so I left those call sites
alone.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
5 tasks
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.
Two real fragility items, plus a small adjacent cleanup
The "defensive int/float coercion in `InvokeMetadataView`" item the reviewer also flagged turned out to be a non-issue — see "intentionally not changed" below.
1. `GenerationMetadataAdapter` discriminator heuristics
For pre-discriminator payloads (no `metadata_version` key), the adapter has to infer the schema version. The previous logic had three real issues:
v\-prefixed builds fell into the catch-all "future = v5" branchRefactored into a `_infer_metadata_version` staticmethod with a clear priority:
Seven unit tests cover the heuristic in isolation (no full schema construction needed — the heuristic just picks a version, doesn't validate).
2. PNG `iTXt` / `zTXt` chunks in `metadata_extraction.py`
Pillow returns different shapes for the three PNG text-chunk types:
Without normalization, an `iTXt` chunk handed `json.loads` a tuple (TypeError on the next line) and the entire metadata block was silently lost — the image rendered with the EXIF fallback or nothing at all.
Adds `_normalize_text_chunk` that funnels tuple → first-element, bytes → utf-8, then `str(...)`. The three JSON extractors all flow through it. Adjacent cleanups in the same file:
3. Scalar-table fallback for unparseable payloads
`invoke_formatter` used to render an opaque `"Unknown invoke metadata format"` placeholder when the discriminated union rejected a payload (unknown future `metadata_version`, malformed shape, etc.). Now it extracts whatever top-level scalar fields are present and renders them via the existing `_scalar_table` — the user still sees the seed / model / prompt etc. The placeholder remains for payloads with zero scalar fields.
Intentionally not changed
The original review item on `int(self.seed)` / `float(lora.weight)` in the view turned out to be moot: the Pydantic schemas declare these as `int` and `float` respectively, which the v2 validator strict-rejects non-numeric strings against at parse time. By the time the view sees a value, it's already coerced. Adding defensive `try/except` would be unreachable code, so I left those call sites alone.
Test plan
Net +235 lines across 5 files; 88 of those are the new tests.
🤖 Generated with Claude Code