Skip to content

Fix: support FileOutput in JSON encoding and equality comparisons - #486

Open
RealAdii wants to merge 1 commit into
replicate:mainfrom
RealAdii:fix/file-output-serialization-and-equality
Open

RealAdii wants to merge 1 commit into
replicate:mainfrom
RealAdii:fix/file-output-serialization-and-equality

Conversation

@RealAdii

Copy link
Copy Markdown

Fixes #377

Background & Issue Audit

Summary of Changes

When replicate.run(...) returns FileOutput objects, users frequently pass them into downstream predictions (e.g., image-to-image chaining) or perform equality checks against expected URLs in tests and pipeline logic. Previously:

  1. encode_json() and async_encode_json() did not recognize FileOutput, leaving the raw instance in the request dictionary and causing httpx JSON serialization to fail with TypeError: Object of type FileOutput is not JSON serializable.
  2. FileOutput lacked a custom __eq__, causing equality comparisons (fo == "https://..." or fo1 == fo2) to evaluate to False (falling back to object identity).
  3. FileOutput lacked informative __repr__ and __hash__ implementations.

Fix

  • Updated encode_json() and async_encode_json() in replicate/helpers.py to handle FileOutput:
    • When file_encoding_strategy == "base64": returns existing data URI or reads and base64-encodes the bytes.
    • By default: returns obj.url as a standard string for JSON serialization.
  • Implemented __repr__, __eq__ (supporting comparisons against both FileOutput instances and string URLs), and __hash__ on FileOutput.
  • Added test coverage in tests/test_helpers.py for equality, representation, hashing, and JSON encoding.
  • Added test coverage in tests/test_prediction.py for sync and async prediction creation with FileOutput inputs.

Validation

  • pytest tests/: 163 passed, 6 skipped.
  • ruff check: All checks passed.
  • ruff format --check: All files formatted.

…isons

Support FileOutput objects in encode_json and async_encode_json so
they can be passed directly as inputs to downstream predictions
without raising TypeError: Object of type FileOutput is not JSON
serializable. Also implement __repr__, __eq__, and __hash__ on
FileOutput for value-based comparison and logging.

Signed-off-by: RealAdii <[email protected]>
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.

replicate.run returning FileOutput objects

1 participant