Repository navigation
Conversation
The backward-compat validator converting additional_kwargs['images'] into image blocks left the key behind, holding live ImageDocument objects. Because of that: - the canonical pydantic round-trip ChatMessage.model_validate(msg.model_dump()) crashed with AttributeError: 'dict' object has no attribute 'resolve_image', since serialized entries are plain dicts, so a SimpleChatStore could be persisted but never reloaded; - re-validating an already-converted message appended the image block again. Remove the key once converted so the migration is one-time and the round-trip reconstructs purely from blocks.
|
There is a lossy serialized-input inverse distinct from the two existing round-trip cases. At this head, Suggested independent negative/control: def test_serialized_legacy_image_without_existing_block_is_not_silently_lost(png_1px_b64):
doc = ImageDocument(image=png_1px_b64)
incoming = {
"role": "user",
"blocks": [],
"additional_kwargs": {"images": [doc.model_dump()]},
}
msg = ChatMessage.model_validate(incoming)
assert any(
isinstance(block, ImageBlock) and block.image == png_1px_b64
for block in msg.blocks
)Current-source prediction: the new Whether to support a dictionary-only input is a compatibility choice; if it isn't supported, it may be safer to reject that input explicitly rather than silently lose image bytes during a successful parse. Could you either accept/reconstruct valid serialized This is a source-predicted regression probe, not a claim that I ran the installed LlamaIndex chat-store suite or observed real user data loss. |
…g them Serialized documents that reconstruct into a resolvable image (path/url, or data kept by an explicit include) are now converted instead of being skipped; anything that cannot be reconstructed into a non-empty image is kept in additional_kwargs unless it is the already-migrated leftover of a persisted message (blocks already hold its image block).
|
Good catch on the silent-discard boundary, and thank you for spelling out the controls. Pushed
The validator now, for dict entries:
New tests: a |
Description
The backward-compat validator
legacy_additional_kwargs_image(llama-index-core/llama_index/core/base/llms/types.py:1182) migrates a legacyadditional_kwargs["images"]list intoImageBlocks, but it left theimageskey in place and unconditionally calleddoc.resolve_image(...)on every entry. That breaks theChatMessagein two ways:model_dump()->model_validate()(the canonical pydantic round-trip) crashes: the dump serializes the leftoverImageDocumentvalues into plain dicts, and re-validation calls.resolve_image()on adict->AttributeError: 'dict' object has no attribute 'resolve_image'. The same crash hitsSimpleChatStore.from_persist_path()when reloading a store that was persisted with such a message, since the persisted JSON also carries the serialized documents.ImageBlocks are duplicated on every re-validation.This PR makes the migration idempotent and safe for serialized payloads:
ImageDocumententries are converted. Entries that are already serialized (plain dicts, as found in chat stores persisted before this change) are skipped; theImageBlocks they were migrated into round-trip insideblocks, so no data is lost.imageskey is removed fromadditional_kwargsonce consumed, so the migration runs exactly once.No code in the repo reads
additional_kwargs["images"]after construction (the validator is the only consumer), so dropping the key has no other behavioral impact.New Package?
Did I fill in the
tool.llamahubsection in thepyproject.tomland provide a detailed README.md for my new integration or package?Version Bump?
Did I bump the version in the
pyproject.tomlfile of the package I am updating? (Except for thellama-index-corepackage)Type of Change
Please delete options that are not relevant.
How Has This Been Tested?
Your pull-request will likely not be merged unless it is covered by some form of impactful unit testing.
Added tests (all fail on
main, pass with this change):tests/base/llms/test_types.py::test_legacy_image_additional_kwargs_roundtrip-model_dump()->model_validate()round-trip keeps the blocks and does not crashtests/base/llms/test_types.py::test_legacy_image_additional_kwargs_migration_is_idempotent- theimageskey is consumed once and re-validating the message does not duplicate image blockstests/base/llms/test_types.py::test_legacy_image_additional_kwargs_skips_serialized_documents- a message validated from its serialized form (documents already migrated intoblocks, serialized documents still inadditional_kwargs, i.e. the pre-fix persisted format) restores without crashingtests/storage/chat_store/test_simple_chat_store.py::test_persist_legacy_images_message- a message built with the legacy kwarg survivesSimpleChatStore.persist()->from_persist_path()Commands and results (from the repo root, with
llama-index-coreinstalled editable):Type-checking the touched module (repo-pinned mypy 1.11.0 with the package config):
The
make lintrecipe (git ls-files | xargs pre-commit run --files) was also run across every tracked file in the repo: all hooks (ruff, ruff-format, mypy, codespell, prettier, black-docs, ...) passed.Suggested Checklist:
uv run make format; uv run make lintto appease the lint gods