Skip to content

fix(sam3): stop IndexError on single-point visual prompts in serialize_prompt#2423

Merged
bigbitbus merged 4 commits into
mainfrom
fix/sam3-serialize-prompt-single-point-indexerror
Jun 9, 2026
Merged

fix(sam3): stop IndexError on single-point visual prompts in serialize_prompt#2423
bigbitbus merged 4 commits into
mainfrom
fix/sam3-serialize-prompt-single-point-indexerror

Conversation

@bigbitbus

@bigbitbus bigbitbus commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Summary

SAM3 interactive Smart Select returns a 500 (IndexError: list index out of range) whenever a visual prompt contains exactly one point — e.g. a single smart-select click, or converting a tiny (<5px) box to a polygon. This surfaces in the app as "Smart Select didn't respond. Try again in a moment." because the deterministic 500 exhausts the client's 3 retries.

The crash is in serialize_prompt() (the mask-input cache-key builder), not in the model.

Root cause

segment_with_visual_prompts() passes per-image point coordinates shaped (num_prompts, num_points, 2) to serialize_prompt(). But serialize_prompt iterated only the leading (prompt) axis, treating each prompt's entire point list as if it were one [x, y] pair:

for coord, label in zip(coords_list, labels_list):   # coords_list is (P, N, 2)
    "y": coord[1] if isinstance(coord, (list, tuple)) else 0,   # <- IndexError

For a single point the array is (1, 1, 2), so the first coord is [[x, y]] — a length-1 list — and coord[1] raises IndexError. With 2+ points it doesn't crash but silently hashes garbage (x/y become lists), so the cache key was wrong all along. Only the non-preview path hits it (serialize_prompt runs only when mask-input caching is enabled), matching the symptom.

The older inference/models/sam3/visual_segmentation.py never had this — it has no serialize_prompt step and feeds (P, N, 2) straight into predict_inst. The bug came in with the pytorch rewrite's low-res-logits mask cache.

Fix

Iterate prompts then points, matching the SAM2 serialize_prompt reference and _predict_for_single_image, which both consume (P, N, 2) correctly. Also fixes the silently-wrong cache keys for multi-point prompts.

Tests

Added unit tests in test_sam3_client_hashes.py (test_serialize_prompt_single_point_does_not_raise, test_serialize_prompt_multiple_points_flattens_in_order). Verified the pre-fix loop raises IndexError on (1,1,2) and the fix produces the expected output. Validated on ck8s-stg via a source-overlay test image — single-point Smart Select now returns POST /sam3/visual_segment 200.

Release (included in this PR)

inference-models is consumed by the server image as a pinned PyPI wheel, so this fix ships only once a new wheel is published. This PR therefore also:

  • bumps inference_models/pyproject.toml + uv.lock to 0.29.1 (the publish workflow uses skip-existing: true, so a bump is required to upload), and
  • raises the pins in requirements.{gpu,cpu,vino,jetson}.txt to ~=0.29.1 so the server image cannot resolve the unfixed 0.29.0.

Post-merge: run the "Publish Inference Models Wheels to PyPi" workflow (or cut a release) to publish 0.29.1, then rebuild/redeploy the inference server image.

Notes / follow-up (not in this PR)

InferenceModelsSAM3InteractiveAdapter.segment_image dropped the legacy empty-prompt fallback (if not any(args.values()): ...) that both the old path and _predict_for_single_image still have. Not the trigger here, but worth restoring for parity.

🤖 Generated with Claude Code

…e_prompt

serialize_prompt() received point coordinates shaped
(num_prompts, num_points, 2) but iterated only the leading prompt axis,
treating each prompt's whole point list as if it were one [x, y] pair. For
a single-point prompt (shape (1, 1, 2)) the lone element was [[x, y]] — a
length-1 list — so `coord[1]` raised "IndexError: list index out of range"
and the SAM3 /visual_segment endpoint 500'd.

This is the server-side cause of "Smart Select didn't respond" when
converting a tiny box to a polygon or committing a single smart-select
click (the mask-input cache path that calls serialize_prompt only runs for
non-preview requests, so hover previews were unaffected).

Iterate prompts then points, matching the SAM2 serialize_prompt reference
and _predict_for_single_image, which both consume the (P, N, 2) shape
correctly. Add unit tests covering the single-point and multi-point cases.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Bump inference-models 0.29.0 -> 0.29.1 so the single-point serialize_prompt
IndexError fix ships as a published wheel, and raise the consumer pins in
requirements.{gpu,cpu,vino,jetson}.txt to ~=0.29.1 so the server image cannot
resolve the unfixed 0.29.0.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@bigbitbus bigbitbus marked this pull request as ready for review June 9, 2026 14:53
…published

Pinning ~=0.29.1 broke integration tests because 0.29.1 is not on PyPI yet
(it is published from this change post-merge). ~=0.29.0 already allows 0.29.1
(>=0.29.0,<0.30.0), so the server image still resolves the fixed wheel once
published, while CI stays green now. The floor can be raised to ~=0.29.1 in a
follow-up after the wheel exists.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@bigbitbus bigbitbus merged commit a2c6eef into main Jun 9, 2026
44 of 47 checks passed
@bigbitbus bigbitbus deleted the fix/sam3-serialize-prompt-single-point-indexerror branch June 9, 2026 15:41
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.

2 participants