Skip to content

Add Object.to_dict() and CrossReference.__repr__ for JSON-esque output - #2134

Open
shaikn6 wants to merge 1 commit into
weaviate:mainfrom
shaikn6:feat/object-to-dict-and-crossref-repr
Open

Add Object.to_dict() and CrossReference.__repr__ for JSON-esque output#2134
shaikn6 wants to merge 1 commit into
weaviate:mainfrom
shaikn6:feat/object-to-dict-and-crossref-repr

Conversation

@shaikn6

@shaikn6 shaikn6 commented Aug 13, 2026

Copy link
Copy Markdown

Closes #881.

What

  • _Object.to_dict() (inherited by Object, ObjectSingleReturn, GroupByObject) converts a query result into a fully JSON-serializable dict — handling datetime → isoformat, UUID → str, GeoCoordinate/PhoneNumber → JSON, and recursively expanding cross-referenced objects into their own to_dict() output.
  • _CrossReference.__repr__ now shows its objects (CrossReference(objects=[...])) instead of the default <... object at 0x...> memory address.

Why

Per the issue and the existing comment on it, printing/serializing query results — especially ones with cross-references — currently either falls back to Python's default repr or requires hand-rolling a converter. to_dict() gives an explicit, opt-in way to get a JSON-serializable representation; the __repr__ fix makes ad-hoc print()/logging readable without calling anything.

Scope notes

  • Kept to_dict() as an explicit method rather than overriding Object.__repr__/__str__, so existing scripts/logs that already print objects aren't affected — only CrossReference's repr changes.
  • CollectionConfig already had its own to_dict() (added separately, for REST-payload serialization) — this PR doesn't touch that, it fills the gap on the query-result side (Object/CrossReference).
  • No cycle-guard on the recursive reference expansion — Weaviate v4 query results aren't cyclic by default, so this wasn't added to avoid speculative complexity. Documented in the docstring.

Testing

  • 4 new unit tests in test/collection/test_object_to_dict.py — all passing
  • Full test/collection/ suite: 326 passed, 0 regressions
  • Full test suite: 416 passed; 3 pre-existing unrelated failures in test/test_timeout.py reproduced identically on a clean checkout (not caused by this change)
  • ruff check, ruff format --check, and flake8 (matching this repo's pre-commit config) all clean

Closes weaviate#881.

- _Object.to_dict() converts a query result (Object/ObjectSingleReturn/
  GroupByObject) into a JSON-serializable dict, handling datetime,
  UUID, GeoCoordinate, and PhoneNumber field types, and recursively
  expanding cross-referenced objects.
- _CrossReference.__repr__ shows its objects instead of the default
  memory-address repr, so printing an object with cross-references is
  readable out of the box.

Includes unit tests covering plain objects, datetime/UUID conversion,
recursive cross-reference expansion, and the new repr.

@orca-security-eu orca-security-eu Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Infrastructure as Code high 0   medium 0   low 0   info 0 View in Orca
Passed Passed SAST high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Vulnerabilities high 0   medium 0   low 0   info 0 View in Orca

@weaviate-git-bot

Copy link
Copy Markdown

To avoid any confusion in the future about your contribution to Weaviate, we work with a Contributor License Agreement. If you agree, you can simply add a comment to this PR that you agree with the CLA so that we can merge.

beep boop - the Weaviate bot 👋🤖

PS:
Are you already a member of the Weaviate Forum?

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.

Allow JSON-esque outputs through str/repr

2 participants