Skip to content

[MAX] Add file-backed media responses for OpenResponses pixel generation#6341

Open
pei0033 wants to merge 9 commits intomodular:mainfrom
SqueezeBits:add/file-reponse
Open

[MAX] Add file-backed media responses for OpenResponses pixel generation#6341
pei0033 wants to merge 9 commits intomodular:mainfrom
SqueezeBits:add/file-reponse

Conversation

@pei0033
Copy link
Copy Markdown
Contributor

@pei0033 pei0033 commented Apr 2, 2026

Summary

This PR improves OpenResponses-based pixel-generation serving by adding file-backed media responses and response-format selection for generated outputs.

Specifically, it:

  • adds file-backed image/video response handling for /v1/responses
  • supports response_format: "url" and response_format: "b64_json" for generated media
  • validates that the requested model matches the currently served model and returns 404 on mismatch
  • adds video-output handling in the shared pixel-generation pipeline so video responses can be serialized correctly

Example local flow validated during development:

  1. Start a server:

    MAX_SERVE_API_TYPES='["responses"]' \
    ./bazelw run //max/python/max/entrypoints:pipelines -- serve \
      --model-path black-forest-labs/FLUX.2-klein-4B \
      --task pixel_generation \
      --port 8000 \
      --devices gpu \
      --prefer-module-v3
  2. Send a T2I request:

    cat >/tmp/flux_t2i_request.json <<'EOF'
    {
      "model": "black-forest-labs/FLUX.2-klein-4B",
      "input": "A studio portrait of a tabby cat with dramatic lighting.",
      "seed": 42,
      "provider_options": {
        "image": {
          "guidance_scale": 4.0,
          "output_format": "png",
          "response_format": "url",
          "width": 512,
          "height": 512,
          "steps": 4
        }
      }
    }
    EOF
    
    curl -sS http://127.0.0.1:8000/v1/responses \
      -H 'Content-Type: application/json' \
      --data @/tmp/flux_t2i_request.json \
      > /tmp/flux_t2i_response.json

With response_format: "url", the response includes an image_url that can
be fetched from /v1/images/{image_id}/content. With
response_format: "b64_json", the response includes inline image_data.

Testing

  • ./bazelw test //max/tests/tests/serve:test_openresponses_routes

//max/tests/tests/serve:test_openresponses_routes specifically verifies that:

  • basic /v1/responses requests still succeed
  • requests are rejected when the requested model does not match the served model
  • video responses can be returned as downloadable file-backed URLs
  • video responses can be returned as inline base64 payloads when response_format: "b64_json" is requested

I also manually verified:

  • T2I with black-forest-labs/FLUX.2-klein-4B
  • response_format: "url" returns image_url
  • response_format: "b64_json" returns inline image_data
  • requesting a different model name than the served model returns 404

Checklist

  • PR is small and focused — consider splitting larger changes into a
    sequence of smaller PRs
  • I ran ./bazelw run format to format my changes
  • I added or updated tests to cover my changes
  • If AI tools assisted with this contribution, I have included an
    Assisted-by: trailer in my commit message or this PR description
    (see AI Tool Use Policy)

Assisted-by: OpenAI Codex

@pei0033 pei0033 requested a review from a team as a code owner April 2, 2026 11:09
@pei0033 pei0033 changed the title Add/file reponse [MAX] Add file-backed media responses for OpenResponses pixel generation Apr 2, 2026
@pei0033
Copy link
Copy Markdown
Contributor Author

pei0033 commented Apr 6, 2026

Added cache eviction for local file-backed generated media.

Workflow is now:

  1. Encode output in memory
  2. Measure payload size
  3. Recompute current cache usage
  4. Evict oldest generated artifacts until the new payload fits
  5. Write the file and register it in the media store

The cache limit is configurable with MAX_SERVE_GENERATED_MEDIA_STORAGE_MB.

@pei0033 pei0033 force-pushed the add/file-reponse branch from 7776fa2 to d8cf15b Compare April 6, 2026 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant