Skip to content

feat: add JSON-compatible repr and to_dict for query result types#2029

Open
atharva-deopujari wants to merge 1 commit intoweaviate:mainfrom
atharva-deopujari:feat/json-repr-for-query-results
Open

feat: add JSON-compatible repr and to_dict for query result types#2029
atharva-deopujari wants to merge 1 commit intoweaviate:mainfrom
atharva-deopujari:feat/json-repr-for-query-results

Conversation

@atharva-deopujari
Copy link
Copy Markdown

Summary

Adds to_dict() and __str__() methods to query result dataclasses so that print(result) produces human-readable JSON output instead of the default dataclass repr.

Currently, printing a query result gives an unreadable blob of nested dataclass reprs. After this change:

result = collection.query.fetch_objects(limit=2)
print(result)
# Pretty-printed JSON with proper indentation

Classes Updated

  • MetadataReturn, GroupByMetadataReturn
  • Object, ObjectSingleReturn (via _Object)
  • GroupByObject
  • GenerativeSingle, GenerativeGrouped
  • GenerativeObject, GenerativeReturn
  • Group, GenerativeGroup
  • GroupByReturn, GenerativeGroupByReturn
  • QueryReturn
  • _CrossReference

Design Decisions

  • Vectors longer than 8 elements are truncated in string output to keep display manageable (e.g., [0.1, 0.2, ..., "... (1536 total)"])
  • Special types (UUID, datetime) are converted to strings
  • Nested objects with to_dict() are serialized recursively
  • Only non-None metadata fields are included to reduce noise
  • to_dict() returns raw dicts for programmatic use; __str__() wraps it in json.dumps with indentation

Fixes #881

Add to_dict() and __str__() methods to query result dataclasses so that
print(result) produces human-readable JSON output instead of the default
dataclass repr. Covers MetadataReturn, Object, QueryReturn,
GenerativeObject, GenerativeReturn, GroupByObject, GroupByReturn,
Group, GenerativeGroup, GenerativeGroupByReturn, GroupByMetadataReturn,
GenerativeSingle, GenerativeGrouped, and CrossReference.

Vectors longer than 8 elements are truncated in string output to keep
display manageable. Special types (UUID, datetime) are converted to
strings. Nested objects with to_dict() are serialized recursively.
Copy link
Copy Markdown

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

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?

@atharva-deopujari
Copy link
Copy Markdown
Author

I agree with the CLA.

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